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(join): start each clip's picture at 0 so a late first frame leaves no hole (#338)

Shogo Kajiyamae7c93ce3ec3f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e87e152..3c789a4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,7 +4,9 @@
## Unreleased
-(nothing yet)
+### Fixed
+
+- **`join.py` no longer leaves a one-frame hole where a clip's picture starts after 0.** A cut part often has its first video packet a little after its first audio packet (0.08 s against 0.058 s here). Every clip's sound chain already started at 0 (`asetpts=PTS-STARTPTS`), but its picture chain kept the offset, so the concat filter (and `xfade`'s offsets) placed it one frame late: a `--transition none` join of a 2.53 s and a 3.0 s clip at 30 fps came out 166 frames over 5.567 s (29.82 fps, `verified: false`). video-editing-skill's CONCAT correctly refused the file. This was introduced in 2.4.1 (#302), when the plain cut started trimming every clip to `clip_length`. The picture now starts at 0 like its sound: 166 frames over 5.533 s at exactly 30 fps.
## 2.10.3
diff --git a/scripts/join.py b/scripts/join.py
index a38ffe2..1547a9d 100755
--- a/scripts/join.py
+++ b/scripts/join.py
@@ -509,7 +509,7 @@ def main() -> int:
if len(set(chans)) > 1:
info(f"channel counts differ ({', '.join(str(c) for c in chans)}); every clip becomes {layout}")
for i in range(n):
- parts.append(f"[{i}:v]{convert[i]}{geo(i)},setsar=1,fps={fps:g},format={pixfmt},settb=AVTB[v{i}]")
+ parts.append(f"[{i}:v]{convert[i]}{geo(i)},setsar=1,setpts=PTS-STARTPTS,fps={fps:g},format={pixfmt},settb=AVTB[v{i}]")
parts.append(f"[{audio_src[i]}]aformat=sample_rates=48000:channel_layouts={layout},asetpts=PTS-STARTPTS[a{i}]")
# pad the shorter stream to the clip's length: the audio with silence, the picture by holding
diff --git a/tests/test_editing.py b/tests/test_editing.py
index 63d10c0..a9c8c8e 100644
--- a/tests/test_editing.py
+++ b/tests/test_editing.py
@@ -1123,6 +1123,28 @@ class EditingTests(MediaFixtures):
self.assertEqual(self._pts_steps(out), {0.0333})
self.assertEqual(len(self._gray_frames(out)), 2 * 90)
+ def test_join_none_starts_a_late_picture_at_its_cut(self):
+ """A clip whose picture starts after 0 (a cut part whose first video packet sits at 0.08 s): the
+ audio chains started at 0 but the video chain kept that offset, and the concat filter placed it
+ after the previous clip -- a one-frame hole and 29.82 fps where 30 was asked (video-editing-skill's
+ CONCAT refused the file). Each clip's picture now starts at 0 like its sound."""
+ d = OUT / "join_none_late"
+ d.mkdir(exist_ok=True)
+ a = self._moving_clip(d / "a.mp4", 3, 3, 700)
+ b = self._moving_clip(d / "b.mp4", 3, 3, 1900)
+ late = d / "b_late.mp4"
+ subprocess.run(["ffmpeg", "-v", "error", "-y", "-itsoffset", "0.08", "-i", str(b), "-i", str(b),
+ "-map", "0:v", "-map", "1:a", "-c", "copy", str(late)], check=True)
+ start = subprocess.run(["ffprobe", "-v", "error", "-select_streams", "v:0", "-show_entries", "stream=start_time",
+ "-of", "csv=p=0", str(late)], stdout=subprocess.PIPE, text=True, check=True).stdout
+ self.assertAlmostEqual(float(start), 0.08, places=2, msg="the premise: the picture starts at 0.08 s")
+ out = d / "late.mp4"
+ doc = json.loads(script("join.py", a, late, "--transition", "none", "--preset", "ultrafast", "--json",
+ "-o", out).stdout)
+ self.assertTrue(doc["verified"], doc["verification"])
+ self.assertEqual(self._pts_steps(out), {0.0333}, "no hole at the cut")
+ self.assertEqual(len(self._gray_frames(out)), 2 * 90)
+
def test_join_dissolve_of_short_parts_keeps_the_frame_count(self):
"""Two 1 s parts at 30 fps whose AAC sound runs 20 ms past the picture (a cut part) dissolved
over 0.3 s: 30 + 30 - 9 = 51 frames. Offsetting by the container length added frames and