Skip to content

Keep picture and sound together when a render seeks a range join - #435

Merged
dillonstreator merged 2 commits into
mainfrom
claude/fix-pause-trim-av-sync-a72716
Dec 2, 2026
Merged

dillonstreator merged 2 commits into
mainfrom
claude/fix-pause-trim-av-sync-a72716

Conversation

@dillonstreator

Copy link
Copy Markdown
Member

Problem

Clips rendered from a video's ranges could come out with video and audio streams of different lengths. On a clip without cuts the picture ran 1.5–2.7 s past the sound. On a clip with pause trims the picture came out shorter than the sound and ran ahead of the speech.

Cause

Pause trimming was not the cause. A ranged project's source.mp4 is a stream-copy join, and its avcC holds only the first part's H.264 parameters. When a range does not start the video, the first part is the black filler, so the stored parameters are Constrained Baseline. YouTube's High-profile encodes use open GOPs: about one keyframe in three is a recovery point with no SPS/PPS of its own.

The render's input -ss starts decoding at the nearest sync frame. When that frame is a recovery point, the decoder uses the filler's parameters and gets garbage until the next IDR, up to ~8 s later. Three things follow:

  • The video starts late.
  • -t counts from the first good frame, so a straight clip's video ends late.
  • In a cut clip, every piece loses that time, so the picture runs ahead of the sound.

The face, speaker and text samplers seek the same way and time their frames by count, so they were offset too.

Repro on such a source: ffmpeg -ss 4937.85 -t 24.52 -i source.mp4 … gives video 26.37 s and audio 24.52 s, with a burst of h264 decode errors.

Fix

  • Joins (pipeline/sparse.ts): each part (piece, re-encoded piece, filler) is remuxed with h264_mp4toannexb,dump_extra=freq=keyframe before concat. Every keyframe then carries its own parameters, and any sync frame decodes on its own.
  • Seeks (media.ts seekVideoArgs): seek 10 s early, then use -itsoffset to move the clock back. The decoder meets an IDR first, while ffmpeg's accurate seek still starts the output at the requested time and -t still counts from it.
    • Used by the render and by the face, speaker and text-range samplers.
    • This repairs sources joined before this change, with no re-download. Measured IDR spacing in the affected sources is up to ~8 s.
  • jumpCutGraph is extracted from the render's cut branch unchanged, so the eval runs the render's own graph.

Tests and evals

Unit tests (test/sparse-source.test.ts): an open-GOP High-profile piece joined after a Baseline filler.

  • A seek onto a recovery point must decode the piece's own picture in a new join.
  • With seekVideoArgs, it must also decode correctly in a join made the old way, and the picture and sound of a cut must match in length.
  • The first test fails without the fix (mean frame difference 123 vs < 1).

New av-sync eval suite: builds the join, the seek and the jump-cut graph through production code. Per case:

  • video and audio stream durations must be within 0.1 s;
  • for each piece between cuts, a 1 s probe is found in the source by picture and by sound, and the two offsets must agree within 0.1 s.

Cases:

  • one synthetic, offline: an open-GOP range after filler;
  • the four reported clips, by link and range: two with pause trims (7 and 2 cuts), two without.
case before: video / audio (s) before: picture vs sound after: length gap after: picture vs sound
open-gop-after-filler fails to decode n/a 0.033 0.056
pauses, 7 cuts 20.20 / 22.37 2.156 0.005 0.090
pauses, 2 cuts 22.07 / 24.73 2.023 0.061 0.090
no cuts, 52 s 53.80 / 52.24 0.023 0.027 0.023
no cuts, 24.5 s 26.37 / 24.52 0.007 0.013 0.007
inSync 0 / 5 5 / 5

The "before" run used this branch with sparse.ts and seekVideoArgs reverted to main's behaviour.

About the residual offsets: 0.023 s is the AAC encoder's priming delay in the probe, not drift. The 0.090 s readings are single pieces with a 2-frame picture lead that doesn't build up across cuts. This is below what a viewer notices and outside this fix.

Verification on a running instance

  1. Built this branch (main including Follow the talker in a forced face crop over two people #433) and deployed it to a local instance once its queue was idle.
  2. Re-rendered the 4 reported clips and 9 older clips that had a constant ~1 s offset (rendered before Fix clips from range projects playing picture behind sound #429 landed there).
  3. All 13 now have video and audio within 0.1 s: worst 0.061 s; the reported four are 0.005 / 0.061 / 0.027 / 0.013 s.

Checks: tsc, pnpm lint, pnpm guards pass.

Existing clips with the old mismatch need a re-render; the source files themselves don't need to be re-downloaded.

A clip rendered from a video's ranges could come out with its picture
and sound of different lengths: picture 1.5-2.7 s longer than the sound
on a straight clip, or shorter and ahead of the speech on a clip with
trimmed pauses.

The joined source stores one set of H.264 parameters, the first part's,
which is the black filler's Baseline set when a range does not start the
video. YouTube's open GOPs make some keyframes recovery points that
carry no parameter sets of their own. The render's input seek started
the decoder on such a keyframe, decoded it with the filler's parameters,
and got garbage until the next IDR a few seconds later. The video began
late, `-t` counted from the first good frame so it ran past the clip's
end, and in a cut clip every piece after the first lost that time, so
the picture ran ahead of the sound.

- Every part of a join now carries its own SPS/PPS on each keyframe
  (h264_mp4toannexb + dump_extra), so any sync frame decodes on its own.
- seekVideoArgs (media.ts) seeks 10 s early and moves the clock back
  with -itsoffset: the decoder meets an IDR first, while ffmpeg's
  accurate seek still starts the output at the time asked for. The
  render and the face, speaker and text samplers use it, so joins made
  before this change render and scan correctly too.

Tests build a join with a Baseline filler before an open-GOP High
profile piece and check the picture after a seek onto a recovery point,
for a new join and with seekVideoArgs on a join made the old way.

Signed-off-by: dillonstreator <dillonstreator@gmail.com>
…ther

The av-sync suite joins a range the way a ranged project does
(buildSparseSource, black filler before it), reads the clip with the
render's seek (seekVideoArgs) and cuts it with the render's own graph,
now the exported jumpCutGraph (a refactor, no change to the graph).
Each case checks that the video and audio streams end within 0.1 s, and
that every piece between jump cuts is in step with its sound: a 1 s probe
from its middle is found in the source by picture and by sound, and the
two offsets must agree within 0.1 s.

Cases: a synthetic open-GOP range after a Baseline filler (offline) and
four clips whose renders had picture and sound 1.5-2.7 s apart, two with
pause trims and two without, by link and range (downloaded into the eval
cache).

Before the fix all five fail: the synthetic case does not decode, and the
reported clips come out 20.20/22.37, 22.07/24.73, 53.80/52.24 and
26.37/24.52 s with pictures up to 2.16 s off their sound. After it all
five pass: worst length gap 0.061 s, worst picture-to-sound offset
0.090 s.

Signed-off-by: dillonstreator <dillonstreator@gmail.com>
@vercel

vercel Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
reelsaw-website Ready Ready Preview Dec 2, 2026 6:11pm UTC

Request Review

@dillonstreator
dillonstreator merged commit 5e3a0af into main Dec 2, 2026
2 checks passed
@dillonstreator dillonstreator mentioned this pull request Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant