Skip to content

Local video editor for coding agents: 42 FFmpeg tools with a machine-readable contract and an MCP server, for Claude Code, Cursor and Codex. Cut, captions, reframe, loudness, platform exports. No cloud, no API keys.

fix(audio): a music bed never shortens the video; pad the mix to the source duration (#165)

Shogo Kajiyama80b58f98099f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 363cbd2..223f357 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,7 @@
- Every tool takes `--timeout SECONDS` (default 1800, `FFMPEG_SKILL_TIMEOUT`): a single ffmpeg run past the limit is killed, its partial output removed, and the failure reported as `kind: timeout` (exit 124) instead of hanging the caller.
- Every tool takes `--overwrite`. An output path that already exists (and was not written by this run) now prints a warning; `FFMPEG_SKILL_NO_OVERWRITE=1` makes it a refusal today, and 2.0 will refuse by default.
+- `audio.py --music` (with or without `--duck`/`--music-loop`) no longer shortens the video: the mixed track is padded/trimmed to the source duration and a video-keeping output never uses `-shortest` (#164).
## 1.4.0
diff --git a/scripts/audio.py b/scripts/audio.py
index 82af75c..9d6a8a3 100755
--- a/scripts/audio.py
+++ b/scripts/audio.py
@@ -196,18 +196,29 @@ def main() -> int:
post.append(f"afade=t=in:st=0:d={args.fade_in:g}")
if args.fade_out and dur:
post.append(f"afade=t=out:st={max(0.0, dur - args.fade_out):.3f}:d={args.fade_out:g}")
- if args.replace and dur:
+ # The audio track is conformed to the source duration whenever it is known: padded with
+ # silence if the graph came out short, trimmed if long. A mixed track can come out a few
+ # hundredths short (amix's dropout_transition, a looped bed's atrim boundary), and with
+ # -shortest below that used to shorten the *video* to match: 12.00 s in, 11.925 s out, four
+ # frames of a stream-copied picture gone (#164). Padding the audio, not cutting the picture,
+ # is the only correct answer for a tool whose contract says the video is never touched.
+ keep_video = has_video and not audio_out
+ if dur and (args.replace or args.music or keep_video):
post.append(f"apad,atrim=0:{dur:.3f}")
if post:
graph.append(f"[{last}]{','.join(post)}[out]")
last = "out"
cmd = ffmpeg_base() + inputs + ["-filter_complex", ";".join(graph), "-map", f"[{last}]"]
- if has_video and not audio_out:
+ if keep_video:
cmd += ["-map", "0:v:0", "-c:v", "copy"]
elif has_video:
cmd += ["-vn"] # audio extension: the picture is dropped, not copied into a container that cannot hold it
- cmd += audio_codec_for(output, args.bitrate) + ["-shortest", output]
+ cmd += audio_codec_for(output, args.bitrate)
+ if not keep_video:
+ # audio-only outputs: a looped music bed is infinite, -shortest ends the run with the main track
+ cmd.append("-shortest")
+ cmd.append(output)
run(cmd)
r = probe(output, role="output")
a = r["audio"]
diff --git a/tests/test_all.py b/tests/test_all.py
index 19749d1..eea12bb 100644
--- a/tests/test_all.py
+++ b/tests/test_all.py
@@ -1542,6 +1542,27 @@ class FFmpegSkillTests(unittest.TestCase):
proc = script("export.py", self.hdr, "--preset", "x", "-o", out)
self.assertIn("HDR", proc.stderr)
+ def test_audio_music_bed_never_shortens_the_video(self):
+ """#164: a looped music bed under --duck came out 11.925 s from a 12.00 s source, and
+ -shortest then cut the stream-copied *video* to match: four frames gone from a tool whose
+ contract says the picture is never touched. The audio is now padded/trimmed to the source
+ duration and a video-keeping output never uses -shortest. Every music mode, plus --replace
+ with a shorter track, must keep the source's duration and frame count exactly."""
+ src_dur = probe(str(self.src))["duration"]
+ src_frames = self._frame_count(self.src)
+ for tag, flags in (("loop_duck", ["--music", str(self.mic), "--duck", "--music-loop"]),
+ ("loop", ["--music", str(self.mic), "--music-loop"]),
+ ("plain", ["--music", str(self.mic)]),
+ ("replace_short", ["--replace", str(OUT / "c_mic_short.wav")])):
+ if tag == "replace_short":
+ sh("ffmpeg", "-y", "-hide_banner", "-loglevel", "error", "-i", str(self.mic), "-t", "3", str(OUT / "c_mic_short.wav"))
+ out = OUT / f"music_len_{tag}.mp4"
+ script("audio.py", self.src, *flags, "-o", out)
+ m = probe(str(out))
+ self.assertAlmostEqual(m["duration"], src_dur, msg=tag, delta=0.02)
+ self.assertEqual(self._frame_count(out), src_frames, f"{tag}: the picture lost or gained frames")
+ self.assertAlmostEqual(m["audio"].get("duration") or m["duration"], src_dur, msg=tag, delta=0.05)
+
def test_audio_downmix_voice_and_ducking(self):
out = OUT / "downmix.mp4"
script("audio.py", self.surround, "--downmix", "--voice", "-o", out)