Your phone, on your command. Gotcha is world's first fully on-device AI copilot for Android that turns natural language into real device actions — safely.
fix(media): redirect .mp3 output to .m4a instead of dead-ending at ffmpeg
Durganshue446afaa14f2
diff --git a/app/src/main/assets/skills/media_editing.json b/app/src/main/assets/skills/media_editing.jsonindex fbbab2c..0e09fa5 100644--- a/app/src/main/assets/skills/media_editing.json+++ b/app/src/main/assets/skills/media_editing.json@@ -28,7 +28,8 @@"faster","slower","join",- "merge"+ "merge",+ "mp3"],- "instructions": "Never upload a video or audio file to a web converter, and never automate a video editor's UI to trim a clip. media_edit does it locally: the file never leaves the device, and there is no watermark, length limit or account.\n\nCall operation='info' FIRST whenever you need a timecode. It reports duration, tracks, resolution, rotation and codecs without decoding anything, so it is cheap. Never guess a duration from the file name or size, and never trim against a duration the user quoted from memory — check it.\n\nWhat it does:\n1. 'info' — what the file contains. Also the way to answer \"how long is this?\" or \"does this video have sound?\".\n2. 'trim' — keeps the window between 'start' and 'end' and drops the rest. Give at least one of them; 'start' alone runs to the end of the file, 'end' alone starts from the beginning.\n3. 'extract_audio' — writes the audio track as its own .m4a. This is \"rip the audio\", \"just the sound\", \"turn this video into a podcast episode\".\n4. 'remove_audio' — writes the video with the audio dropped. This is \"mute it\", \"remove the background noise\" (which it does by removing ALL sound — say so, because that is usually not what they hoped for).\n5. 'compress' — re-encodes smaller at 'height' (default 720). This is \"make it small enough to email/WhatsApp\". Compressing a file that is already that small or smaller is refused rather than silently degrading it.\n6. 'speed' — re-times by 'speed', between 0.25 and 4.0. Audio and video are re-timed together, so they stay in sync. 2.0 halves the duration.\n7. 'concat' — joins 'inputs' end to end, in exactly the order given. Confirm the order with the user when it is not obvious; a join in the wrong order looks like data loss to them. All inputs must be video, or all audio-only — mixing them is refused.\n\nTimecodes take several forms: '1:23' (m:ss), '1:02:03' (h:mm:ss), '90s', '1m30s', '500ms', or plain seconds like '12.5'. A colon field holding 60 or more seconds is refused as ambiguous — write '1:30' or '90s', not '1:90'. Use 'end' as the end timecode to mean the end of the file.\n\nAll four operations work identically on video and on audio-only files. Trimming an .m4a voice memo and trimming a 4K video are the same call. The two exceptions are refused with an explanation: extracting audio from a file that has no audio track, and removing the audio from a file that is audio-only.\n\nCost differs sharply between the two groups, and it is worth telling the user which they are getting. trim, extract_audio and remove_audio COPY the streams: they finish in seconds even on a long video and lose no quality whatsoever. compress, speed and concat RE-ENCODE: they run the whole file through the codec, which takes minutes and loses a little quality each time. The result message says which happened — pass that on, because repeated re-encoding visibly degrades a file and the user cannot see it coming.\n\nBecause re-encoding is slow, inputs longer than 30 minutes are refused for compress, speed and concat, and any export that overruns ten minutes is abandoned. When you hit either, do not retry the same call — trim the file to the part that is actually wanted first, and explain why you are doing that.\n\nOutput rules that matter. Every operation writes a NEW file and leaves the input untouched. The output path CANNOT be the same as the input — unlike pdf_edit there is no in-place mode, because the encoder writes the target while still reading the source, which would destroy both. Pick a clearly derived name (VID_0001-trimmed.mp4, interview-audio.m4a) and tell the user where you put it. An existing output fails unless you pass overwrite=true; do not reflexively retry with overwrite after that error, since the file in the way may be one the user still wants. The output must end in .mp4 (video) or .m4a (audio only) — the muxer always writes an MP4 container, so any other extension would be a mislabelled file.\n\nThe hard limits, which you must state plainly rather than work around:\n• It CANNOT add background music or any second audio track, overlay text, a watermark or an image, fade audio in or out, adjust volume, or change the bitrate directly. These are not implemented. Do not fake any of them by chaining other tools.\n• It CANNOT open DRM-protected media — anything bought or downloaded from a store. That is a property of the file, not a missing feature, and no retry will work.\n• It CANNOT edit what is inside a frame: no removing an object, no blurring a face, no colour grading, no stabilisation.\n• Cutting a middle section OUT of one file is possible, but only as two steps: trim the part before and the part after into separate files, then concat them. Say that is what you are doing, and clean up the intermediate files afterwards with delete_file.\nWhen asked for one of these, say so directly and offer what does work.\n\nIf a file will not open at all, it may be in a container Android's codecs cannot read (MKV, OGG, FLV are the usual ones). ffmpeg through run_termux_command is the fallback, but it requires Termux installed with the ffmpeg package, so offer it as an option rather than assuming it is there.\n\nFinding the file: phone video lives in /sdcard/DCIM/Camera, screen recordings in /sdcard/Movies, voice memos and music in /sdcard/Music or /sdcard/Recordings, downloads in /sdcard/Download, and WhatsApp media under /sdcard/Android/media/com.whatsapp. Use glob (e.g. '/sdcard/DCIM/Camera/*.mp4') rather than asking the user for a full path they will not know. When several match, use info on the likely ones and confirm which they meant before writing anything — trimming the wrong video looks like data loss."+ "instructions": "Never upload a video or audio file to a web converter, and never automate a video editor's UI to trim a clip. media_edit does it locally: the file never leaves the device, and there is no watermark, length limit or account.\n\nCall operation='info' FIRST whenever you need a timecode. It reports duration, tracks, resolution, rotation and codecs without decoding anything, so it is cheap. Never guess a duration from the file name or size, and never trim against a duration the user quoted from memory — check it.\n\nWhat it does:\n1. 'info' — what the file contains. Also the way to answer \"how long is this?\" or \"does this video have sound?\".\n2. 'trim' — keeps the window between 'start' and 'end' and drops the rest. Give at least one of them; 'start' alone runs to the end of the file, 'end' alone starts from the beginning.\n3. 'extract_audio' — writes the audio track as its own .m4a. This is \"rip the audio\", \"just the sound\", \"turn this video into a podcast episode\".\n4. 'remove_audio' — writes the video with the audio dropped. This is \"mute it\", \"remove the background noise\" (which it does by removing ALL sound — say so, because that is usually not what they hoped for).\n5. 'compress' — re-encodes smaller at 'height' (default 720). This is \"make it small enough to email/WhatsApp\". Compressing a file that is already that small or smaller is refused rather than silently degrading it.\n6. 'speed' — re-times by 'speed', between 0.25 and 4.0. Audio and video are re-timed together, so they stay in sync. 2.0 halves the duration.\n7. 'concat' — joins 'inputs' end to end, in exactly the order given. Confirm the order with the user when it is not obvious; a join in the wrong order looks like data loss to them. All inputs must be video, or all audio-only — mixing them is refused.\n\nTimecodes take several forms: '1:23' (m:ss), '1:02:03' (h:mm:ss), '90s', '1m30s', '500ms', or plain seconds like '12.5'. A colon field holding 60 or more seconds is refused as ambiguous — write '1:30' or '90s', not '1:90'. Use 'end' as the end timecode to mean the end of the file.\n\nAll four operations work identically on video and on audio-only files. Trimming an .m4a voice memo and trimming a 4K video are the same call. The two exceptions are refused with an explanation: extracting audio from a file that has no audio track, and removing the audio from a file that is audio-only.\n\nCost differs sharply between the two groups, and it is worth telling the user which they are getting. trim, extract_audio and remove_audio COPY the streams: they finish in seconds even on a long video and lose no quality whatsoever. compress, speed and concat RE-ENCODE: they run the whole file through the codec, which takes minutes and loses a little quality each time. The result message says which happened — pass that on, because repeated re-encoding visibly degrades a file and the user cannot see it coming.\n\nBecause re-encoding is slow, inputs longer than 30 minutes are refused for compress, speed and concat, and any export that overruns ten minutes is abandoned. When you hit either, do not retry the same call — trim the file to the part that is actually wanted first, and explain why you are doing that.\n\nOutput rules that matter. Every operation writes a NEW file and leaves the input untouched. The output path CANNOT be the same as the input — unlike pdf_edit there is no in-place mode, because the encoder writes the target while still reading the source, which would destroy both. Pick a clearly derived name (VID_0001-trimmed.mp4, interview-audio.m4a) and tell the user where you put it. An existing output fails unless you pass overwrite=true; do not reflexively retry with overwrite after that error, since the file in the way may be one the user still wants. The output must end in .mp4 (video) or .m4a (audio only) — the muxer always writes an MP4 container, so any other extension would be a mislabelled file.\n\nMP3 output is not possible, and this comes up constantly because \"extract the mp3\" is simply how people ask for an audio file. Android ships no MP3 encoder and its muxer only writes AAC audio, so no on-device tool can produce one — this is a property of the platform, not a gap in Gotcha. Write '.m4a' instead and say what you did: \"I saved it as an .m4a rather than an MP3, because MP3 can't be created on-device — it plays everywhere MP3 does and is a bit smaller for the same quality.\" Do NOT detour to ffmpeg over a file extension; that costs a Termux dependency the user may not have installed, to produce a file no more useful than the .m4a. Reach for ffmpeg via run_termux_command only when the user says they specifically need MP3 for something that accepts nothing else.\n\nThe hard limits, which you must state plainly rather than work around:\n• It CANNOT add background music or any second audio track, overlay text, a watermark or an image, fade audio in or out, adjust volume, or change the bitrate directly. These are not implemented. Do not fake any of them by chaining other tools.\n• It CANNOT open DRM-protected media — anything bought or downloaded from a store. That is a property of the file, not a missing feature, and no retry will work.\n• It CANNOT edit what is inside a frame: no removing an object, no blurring a face, no colour grading, no stabilisation.\n• Cutting a middle section OUT of one file is possible, but only as two steps: trim the part before and the part after into separate files, then concat them. Say that is what you are doing, and clean up the intermediate files afterwards with delete_file.\nWhen asked for one of these, say so directly and offer what does work.\n\nIf a file will not open at all, it may be in a container Android's codecs cannot read (MKV, OGG, FLV are the usual ones). ffmpeg through run_termux_command is the fallback, but it requires Termux installed with the ffmpeg package, so offer it as an option rather than assuming it is there.\n\nFinding the file: phone video lives in /sdcard/DCIM/Camera, screen recordings in /sdcard/Movies, voice memos and music in /sdcard/Music or /sdcard/Recordings, downloads in /sdcard/Download, and WhatsApp media under /sdcard/Android/media/com.whatsapp. Use glob (e.g. '/sdcard/DCIM/Camera/*.mp4') rather than asking the user for a full path they will not know. When several match, use info on the likely ones and confirm which they meant before writing anything — trimming the wrong video looks like data loss."}diff --git a/app/src/main/java/com/gotcha/tools/MediaEditTool.kt b/app/src/main/java/com/gotcha/tools/MediaEditTool.ktindex c1c4ae0..f392c20 100644--- a/app/src/main/java/com/gotcha/tools/MediaEditTool.kt+++ b/app/src/main/java/com/gotcha/tools/MediaEditTool.kt@@ -65,8 +65,22 @@ class MediaEditTool(private val context: Context) {* named, so anything else would be a mislabelled file that some players* refuse and the user cannot diagnose. .m4a is the audio-only spelling of* the same container, which is why it is allowed alongside .mp4.+ *+ * MP3 is not on this list and cannot be added. Two independent blockers:+ * media3's FrameworkMuxer accepts only audio/mp4a-latm, audio/3gpp and+ * audio/amr-wb as output audio formats, and Android ships no MP3 *encoder*+ * at all (only a decoder). Writing MP3 would mean bundling something like+ * LAME through the NDK, outside the media3 pipeline entirely.*/val OUTPUT_EXTENSIONS = listOf("mp4", "m4a")++ /**+ * Extensions a user is likely to name when they mean "an audio file",+ * MP3 above all. They get pointed at .m4a rather than at ffmpeg, because+ * "extract the mp3" almost always means "give me the audio", and AAC in an+ * .m4a plays on everything the asker owns.+ */+ private val AUDIO_EXTENSIONS = setOf("mp3", "wav", "ogg", "flac", "aac", "opus", "wma", "m4b")}/**@@ -413,11 +427,7 @@ class MediaEditTool(private val context: Context) {}val extension = file.extension.lowercase()if (extension !in OUTPUT_EXTENSIONS) {- return fail(- "Output '$path' ends in '.$extension', but this tool always writes an MP4 container — " +- "name it .mp4 for video or .m4a for audio-only. Converting to ${extension.ifEmpty { "that format" }} " +- "needs ffmpeg via run_termux_command."- )+ return fail(unsupportedContainerMessage(path, extension))}if (file.exists() && !overwrite) {return fail(@@ -433,6 +443,32 @@ class MediaEditTool(private val context: Context) {return Result.success(file)}+ /**+ * Explains an unwritable output container, and — for an audio extension —+ * redirects rather than dead-ends.+ *+ * "Extract the mp3" is how people ask for an audio file; they mean the audio,+ * not the codec. Sending the model to ffmpeg over a filename would trade a+ * two-second stream copy for a Termux dependency the user may not even have+ * installed, to produce a file that plays no more widely than the .m4a would.+ * So MP3 gets a substitution to offer, and only a genuine MP3 requirement is+ * worth the detour.+ */+ private fun unsupportedContainerMessage(path: String, extension: String): String {+ val named = extension.ifEmpty { "no extension" }+ return if (extension in AUDIO_EXTENSIONS) {+ "Output '$path' ends in '.$extension', which this tool cannot write — Android has no MP3 encoder and " ++ "its muxer only writes AAC audio, so no on-device tool can produce one. Write '.m4a' instead: it " ++ "holds AAC, is smaller than MP3 at the same quality, and plays on phones, browsers, cars and " ++ "desktops alike. Do that and tell the user you saved an .m4a because MP3 cannot be made on-device — " ++ "for almost every purpose it is the same thing. Only if they specifically need MP3 for a device that " ++ "accepts nothing else is ffmpeg via run_termux_command worth the detour, and that needs Termux installed."+ } else {+ "Output '$path' has $named, but this tool always writes an MP4 container — name it '.mp4' for video or " ++ "'.m4a' for audio-only. Converting to another container needs ffmpeg via run_termux_command."+ }+ }+/*** The platform's own messages here are unusable by a model — a DRM-protected* file reports "Failed to instantiate extractor", which reads like a bug indiff --git a/app/src/test/java/com/gotcha/tools/MediaEditToolTest.kt b/app/src/test/java/com/gotcha/tools/MediaEditToolTest.ktindex 68609c3..fe57380 100644--- a/app/src/test/java/com/gotcha/tools/MediaEditToolTest.kt+++ b/app/src/test/java/com/gotcha/tools/MediaEditToolTest.kt@@ -163,13 +163,40 @@ class MediaEditToolTest {@Testfun `an output container the muxer cannot write is refused`() {- listOf("out.mp3", "out.wav", "out.mkv", "out").forEach { name ->+ listOf("out.mkv", "out.avi", "out").forEach { name ->val result = edit("trim", input = placeholder("a.mp4").path, output = File(dir, name).path, start = "1")assertFalse(name, result.success)assertTrue(name, result.message.contains(".mp4") && result.message.contains(".m4a"))}}+ @Test+ fun `an audio extension is redirected to m4a rather than to ffmpeg`() {+ // "Extract the mp3" means "give me the audio". Sending the model to Termux+ // over a filename would cost a dependency the user may not have, to produce+ // a file that plays no more widely than the .m4a it could have written.+ listOf("out.mp3", "out.wav", "out.flac", "out.ogg").forEach { name ->+ val result = edit(+ "extract_audio",+ input = placeholder("a.mp4").path,+ output = File(dir, name).path+ )+ assertFalse(name, result.success)+ assertTrue(name, result.message.contains(".m4a"))+ assertTrue("$name should explain why", result.message.contains("no MP3 encoder"))+ }+ }++ @Test+ fun `the mp3 redirect still names ffmpeg for a genuine mp3 requirement`() {+ val result = edit(+ "extract_audio",+ input = placeholder("a.mp4").path,+ output = File(dir, "out.mp3").path+ )+ assertTrue(result.message.contains("run_termux_command"))+ }+// ---- re-encoding operations ----@Testdiff --git a/app/src/test/resources/feature-test-coverage.json b/app/src/test/resources/feature-test-coverage.jsonindex 1a0d7c9..2cee43c 100644--- a/app/src/test/resources/feature-test-coverage.json+++ b/app/src/test/resources/feature-test-coverage.json@@ -423,7 +423,7 @@"com.gotcha.tools.MediaEditToolTest","com.gotcha.tools.MediaTimeSpecTest"],- "notes": "timecode parsing and trim-window arithmetic on the JVM (m:ss, h:mm:ss, unit and plain-seconds forms, ambiguous-field and backwards-window refusals, clamping an over-long end), plus every guard that runs before a codec is touched: unknown operation, missing arguments, trim with no bounds, refusal to write back over the input or over any concat input, existing-output refusal, directory output, the MP4/M4A container check, speed bounds and the pointless speed=1.0 re-encode, non-positive compress height, and concat's input-count limits. The export itself is device-only — Media3 Transformer drives MediaCodec, which is native and has no Robolectric shadow, so the output of trim, extract_audio, remove_audio, compress, speed and concat must be verified by hand on hardware"+ "notes": "timecode parsing and trim-window arithmetic on the JVM (m:ss, h:mm:ss, unit and plain-seconds forms, ambiguous-field and backwards-window refusals, clamping an over-long end), plus every guard that runs before a codec is touched: unknown operation, missing arguments, trim with no bounds, refusal to write back over the input or over any concat input, existing-output refusal, directory output, the MP4/M4A container check and the .mp3 redirect to .m4a, speed bounds and the pointless speed=1.0 re-encode, non-positive compress height, and concat's input-count limits. The export itself is device-only — Media3 Transformer drives MediaCodec, which is native and has no Robolectric shadow, so the output of trim, extract_audio, remove_audio, compress, speed and concat must be verified by hand on hardware"},{"tool": "glob",diff --git a/docs/FEATURE_TEST_COVERAGE.md b/docs/FEATURE_TEST_COVERAGE.mdindex 04fa701..c8b2371 100644--- a/docs/FEATURE_TEST_COVERAGE.md+++ b/docs/FEATURE_TEST_COVERAGE.md@@ -65,7 +65,7 @@ Every tool the assistant can call is listed below, together with how it is verif| `lock_screen` | `MANUAL_ONLY` | Needs an active Device Admin registration, which cannot be granted non-interactively. || `mark_email_read` | `UNIT` | `EmailToolsTest` — read/unread toggle routed to the right backend || `media_control` | `UNIT` | `MediaSelectionTest` — action parsing and active-session selection |-| `media_edit` | `ROBOLECTRIC` | `MediaEditToolTest`, `MediaTimeSpecTest` — timecode parsing and trim-window arithmetic on the JVM (m:ss, h:mm:ss, unit and plain-seconds forms, ambiguous-field and backwards-window refusals, clamping an over-long end), plus every guard that runs before a codec is touched: unknown operation, missing arguments, trim with no bounds, refusal to write back over the input or over any concat input, existing-output refusal, directory output, the MP4/M4A container check, speed bounds and the pointless speed=1.0 re-encode, non-positive compress height, and concat's input-count limits. The export itself is device-only — Media3 Transformer drives MediaCodec, which is native and has no Robolectric shadow, so the output of trim, extract_audio, remove_audio, compress, speed and concat must be verified by hand on hardware |+| `media_edit` | `ROBOLECTRIC` | `MediaEditToolTest`, `MediaTimeSpecTest` — timecode parsing and trim-window arithmetic on the JVM (m:ss, h:mm:ss, unit and plain-seconds forms, ambiguous-field and backwards-window refusals, clamping an over-long end), plus every guard that runs before a codec is touched: unknown operation, missing arguments, trim with no bounds, refusal to write back over the input or over any concat input, existing-output refusal, directory output, the MP4/M4A container check and the .mp3 redirect to .m4a, speed bounds and the pointless speed=1.0 re-encode, non-positive compress height, and concat's input-count limits. The export itself is device-only — Media3 Transformer drives MediaCodec, which is native and has no Robolectric shadow, so the output of trim, extract_audio, remove_audio, compress, speed and concat must be verified by hand on hardware || `notion_append_to_page` | `UNIT` | `NotionToolsTest` — block append request shape || `notion_create_page` | `UNIT` | `NotionToolsTest` — parent/title handling and request shape || `pause_audio_recording` | `MANUAL_ONLY` | Needs real device hardware; the emulator has no faithful stand-in for this sensor/radio. |