Sub topic 5
Keeping audio and video in sync after cuts
This chapter assumes you know how a timeline, clips, and the playhead work. It also assumes you can cut a segment from a lecture and run a basic ffmpeg command. Here you will learn why a cut can leave the picture and the voice out of step, how to prevent it, and how to check the result.
What "in sync" means
A video file stores audio and video as separate streams. Each stream has its own timestamps, called presentation timestamps (PTS). A PTS tells the player when to show a frame or play a block of audio samples. The player lines up the two streams by comparing these timestamps.
Sync breaks when the timestamps of the two streams no longer describe the same moment. Viewers notice lip-sync errors at fairly small offsets. A commonly cited guideline puts the limit at about 45 ms when audio leads the picture and about 125 ms when audio lags. Lecture footage with a visible face is the most sensitive case. Screen recordings with only a narrator are more forgiving, but a cursor click that lands after the "click here" narration still feels wrong.
Why cuts cause drift
When you remove a segment, three things can go wrong.
- Only one stream is cut. If you delete the video from 72 s to 80 s but leave the audio, the audio is now 8 seconds ahead of the picture. This is the most common beginner error.
- The cut points differ between streams. Video is divided into frames, and audio into samples. At 30 frames per second, one frame lasts about 33 ms. At 48,000 samples per second, one sample lasts about 0.02 ms. A cut at an arbitrary time lands between frames in video but almost exactly where you asked in audio. The streams can then differ by up to one frame at each cut.
- Timestamps keep their gap. After a cut, the frames and samples after the gap still carry their old timestamps. A player sees an 8-second hole. It may freeze, play silence, or skip.
Small errors from item 2 add up. Ten cuts that each leave a 15 ms mismatch can add up to 150 ms of drift by the end of the lecture.
Cutting both streams with one rule
The safest approach is to apply the same time range to both streams and then renumber the timestamps so the remaining parts play without a gap. This ffmpeg command removes 72 s to 80 s:
ffmpeg -i lecture.mp4 \
-vf "select='not(between(t,72,80))',setpts=N/FRAME_RATE/TB" \
-af "aselect='not(between(t,72,80))',asetpts=N/SR/TB" \
lecture_cut.mp4Each filter has a job:
selectandaselectkeep only the frames or samples outside the range. Both use the samebetween(t,72,80)expression, so the streams are cut at the same times.setpts=N/FRAME_RATE/TBgives each surviving frame a new timestamp based on its position,N, in the output. Frame 0 gets 0, frame 1 gets one frame duration, and so on. The gap disappears.asetpts=N/SR/TBdoes the same for audio. HereNcounts the samples already output andSRis the sample rate.
This command re-encodes both streams, so ffmpeg can place the cut on exact frames. It does not depend on keyframes (the full frames that other frames are built from). The codecs chapter explains why that matters for accuracy.
An equivalent form for several cuts
select expressions get long when you have many cuts. A second approach keeps the good parts and joins them. It uses trim, atrim, and concat:
ffmpeg -i lecture.mp4 -filter_complex \
"[0:v]trim=0:72,setpts=PTS-STARTPTS[v1];\
[0:a]atrim=0:72,asetpts=PTS-STARTPTS[a1];\
[0:v]trim=start=80,setpts=PTS-STARTPTS[v2];\
[0:a]atrim=start=80,asetpts=PTS-STARTPTS[a2];\
[v1][a1][v2][a2]concat=n=2:v=1:a=1[v][a]" \
-map "[v]" -map "[a]" lecture_cut.mp4Each kept part is trimmed on both streams and its timestamps are reset to start at zero with PTS-STARTPTS. The concat filter takes the parts as video and audio pairs. It then joins them and pads or trims the audio so each part's audio length matches its video length. This is why concat is the better choice when you have many cuts.
Frame boundaries and sample boundaries
Even with both streams cut at "72.0 s", the picture can only change on a frame boundary. If the frame rate is 30 fps, frame boundaries fall at multiples of 1/30 s, such as 72.0 s and 72.033 s. A cut at 72.02 s sits inside a frame. With select, a frame is kept or dropped as a whole based on its own timestamp. The audio, though, is cut at close to 72.02 s. The result is a mismatch of up to one frame.
You can avoid this in two ways:
- Choose cut points that fall on frame boundaries. In an editor, snap the playhead to frames. In a script, round your times to multiples of the frame duration.
- Cut at a natural pause in the speech, where a small offset is inaudible and invisible.
Variable frame rate
Screen recorders and phone cameras often produce variable frame rate (VFR) video. The recorder writes a frame only when something changes or when the device can capture one. Frame spacing is then uneven. setpts=N/FRAME_RATE/TB assumes frames are evenly spaced. On a VFR source it forces an even spacing, which can make the video run faster or slower than the audio.
If your lecture comes from a screen recorder, check first:
ffprobe -v error -select_streams v:0 \
-show_entries stream=r_frame_rate,avg_frame_rate \
-of default=noprint_wrappers=1 lecture.mp4If r_frame_rate and avg_frame_rate differ noticeably, the file is probably VFR. Convert it to a constant frame rate before cutting, or use the trim form with PTS-STARTPTS, which keeps the original timing of each frame.
ffmpeg -i lecture.mp4 -vf "fps=30" -c:v libx264 -c:a copy lecture_cfr.mp4Common failure modes
| Symptom | Likely cause | Fix |
|---|---|---|
| Audio runs several seconds ahead after a cut | Only one stream was cut | Apply the same range to both streams |
| Freeze or silence where the cut was | Timestamps were not regenerated | Add `setpts` and `asetpts`, or `PTS-STARTPTS` |
| Small drift that grows over the lecture | Many cuts, each off by under one frame | Cut on frame boundaries, or cut at pauses |
| Audio slowly separates from the picture | Variable frame rate source | Convert to constant frame rate first |
| Click or pop at the cut | Audio cut mid-waveform | Add a short fade at the cut (next chapter) |
Checking the result
Do both of these checks after every export.
- Compare stream durations. The video and audio should differ by no more than a frame or two.
ffprobe -v error -show_entries stream=codec_type,duration \
-of csv=p=0 lecture_cut.mp4- Watch across each cut. Play five seconds before and after each cut point. Look for a mouth that moves before or after the sound, and for cursor actions that do not match the narration. Then check the end of the lecture, where accumulated drift is largest.
Exercise
You remove the section from 40 s to 55 s from a lecture. After export, the speaker's voice is 15 seconds ahead of their lips. What most likely went wrong, and what is the fix?
Show answer
Only one stream was cut. The 15-second section was removed from the video but not from the audio (or the reverse), so one stream is 15 seconds shorter than the other. Apply the same range to both streams, for example select with aselect, or trim with atrim, and regenerate timestamps with setpts and asetpts.
Exercise
A 40-minute screen recording from a laptop drifts about half a second out of sync by the end, even though both streams were cut at the same times. What should you check first?
Show answer
Check whether the source has a variable frame rate by comparing r_frame_rate and avg_frame_rate with ffprobe. If it does, convert it to a constant frame rate before cutting, or use the trim and PTS-STARTPTS approach so the frames keep their original timing.