Fix Audio-Video Sync Offset in FFmpeg
In FFmpeg, you can shift only the audio forward or backward to realign it with the video. This works when a recording has lips that don't match the voice, or when a clap sounds after the picture shows the hands meeting.
Sync errors come in two kinds: ones that stay the same from start to finish, and ones that grow over time. I made three test videos: one with audio 0.3 seconds late, one with audio 0.2 seconds early, and one where the audio gradually falls further behind. I measured the sync error before and after each fix.
The test environment was Ubuntu 24.04.5 with FFmpeg 6.1.1 (Ubuntu package 6.1.1-3ubuntu5). The videos were 320×180 at 30fps in H.264, with 48kHz stereo AAC at 128kbps, in an MP4 container. The section on measuring sync error below explains how I measured it. The official documentation I referred to is the latest version I could confirm at the time. I did not check the fixed videos by playing them in a browser or video player, and I did not try formats other than MP4.
How I Measured the Sync Error
Each test video showed a black screen that flashed white for two frames at 1, 3, 5 seconds and so on, at 2-second intervals. At the same moments, a 0.05-second beep played. I also made versions where only the audio was shifted.
I read the flash times from the average brightness (YAVG) per frame, using the signalstats filter. I took a flash to start when YAVG rose above 100. I read the beep times from silence_end, the time when silence ended, as output by the silencedetect filter. The following command outputs both in a single read. I paired each beep with the flash and calculated the sync error as "beep time minus flash time." A positive value means the audio is late, and a negative value means it is early.
ffmpeg -i late.mp4 -filter_complex \
"[0:v]signalstats,metadata=mode=print:key=lavfi.signalstats.YAVG:file=yavg.txt[v];[0:a]silencedetect=n=-30dB:d=0.02[a]" \
-map "[v]" -map "[a]" -f null -
For the tests I used the same filters and only changed the output file name. yavg.txt contains the time and YAVG for each frame, and the screen shows the silence_end times. These are the results for the three original videos:
| Video | Measured sync error (seconds) |
|---|---|
late.mp4 |
+0.300 at all 5 points |
early.mp4 |
−0.200 at all 5 points |
drift.mp4 (60 seconds) |
+0.001 at 1 second, +0.059 at 59 seconds |
In drift.mp4, I stretched the beep interval and length by 0.1% (the beep pitch stayed at 1000Hz). The sync error grew by 0.002 seconds every 2 seconds, reaching 0.059 seconds of lag at the end.
Shifting Without Re-encoding: -itsoffset
Read the same file twice, and shift the timestamps of only the second input so that video and audio come from separate inputs. -itsoffset adds the given number of seconds to the timestamps of the input read by the -i that comes right after it. The official documentation says that a positive value delays that input's streams by the specified time.
For late.mp4, where the audio is 0.3 seconds late, add -itsoffset -0.3 before the second input to move the audio 0.3 seconds earlier. Take the video from the first input.
ffmpeg -i late.mp4 -itsoffset -0.3 -i late.mp4 \
-map 0:v -map 1:a -c copy late_itsoffset_audio.mp4
-map 0:v selects the video from the first input, and -map 1:a selects the audio from the second. -c copy copies both streams without re-encoding them.
For early.mp4, where the audio is 0.2 seconds early, use -itsoffset 0.2 to delay the audio instead.
ffmpeg -i early.mp4 -itsoffset 0.2 -i early.mp4 \
-map 0:v -map 1:a -c copy early_itsoffset_audio.mp4
These are the sync error and the audio start time (start_time) and duration from ffprobe for each output:
| Fixed video | Measured sync error (seconds) | Audio start (seconds) | Audio duration (seconds) |
|---|---|---|---|
late_itsoffset_audio.mp4 |
0.000 at all 5 points | 0.000000 | 9.700000 |
early_itsoffset_audio.mp4 |
−0.001 at all 5 points | 0.178000 | 10.021333 |
In both cases, the sync error was almost entirely gone. In the version where I moved the audio earlier, the audio starts at 0 seconds and is 9.7 seconds long, 0.3 seconds shorter than the original 10 seconds.
In the version where I delayed the audio, ffprobe shows the audio starts at 0.178 seconds, not 0.2 seconds. There is no audio data before that point. If you want the audio to start at 0 seconds by adding silence at the beginning, use adelay as described in the next section.
The value for -itsoffset is the measured sync error with its sign reversed. If the audio is 0.3 seconds late, use -0.3. If it is 0.2 seconds early, use 0.2. Add the value before the second input, which is the one providing the audio.
You can also shift the video instead of the audio. For late.mp4, adding -itsoffset 0.3 before the first input (the one used for video) brought the sync error to 0.000. However, the video now starts at 0.300000 seconds, and the flash times move to 1.300 seconds, 3.300 seconds and so on, so the whole video content moves 0.3 seconds later. If you move the audio instead, the video timestamps stay the same as the original.
Re-encoding the Audio: adelay and atrim
If you can re-encode the audio, you can also process it directly with filters. Copy the video with -c:v copy and re-encode only the audio to AAC.
When the audio is early, use the adelay filter to add silence at the start and delay it. Its values are in milliseconds. adelay takes a value per channel, so I added all=1 to delay both stereo channels by the same amount. Without it, the second channel is not delayed.
ffmpeg -i early.mp4 -c:v copy -af "adelay=200:all=1" \
-c:a aac -b:a 128k early_adelay.mp4
When the audio is late, use the atrim filter to cut off the beginning. atrim keeps the original timestamps, so I add asetpts=PTS-STARTPTS afterward to make the trimmed audio start at 0 seconds. The official documentation for atrim also recommends adding asetpts when you want timestamps to start from 0.
ffmpeg -i late.mp4 -c:v copy -af "atrim=start=0.3,asetpts=PTS-STARTPTS" \
-c:a aac -b:a 128k late_atrim.mp4
| Fixed video | Measured sync error (seconds) | Audio start (seconds) | Audio duration (seconds) |
|---|---|---|---|
early_adelay.mp4 |
0.000 at all 5 points | 0.000000 | 10.205000 |
late_atrim.mp4 |
0.000 at all 5 points | 0.000000 | 9.705000 |
In both cases, the sync error was 0.000. The adelay version starts the audio at 0 seconds. As the official documentation describes, the delay is filled with silence. As a result, the audio is 10.205 seconds long, longer than the video (10 seconds).
Drift That Grows Over Time: Fix It by Changing the Audio Speed
When the sync error grows over time, as in drift.mp4, the whole audio needs to play slightly faster. Shifting it by a fixed amount does not fix the problem. I shifted the audio with -itsoffset -0.03 so that it matched at the 30-second midpoint. The sync error became −0.029 seconds at the start and +0.029 seconds at the end. This only halved the remaining error.
To change the speed, I tried two methods: combining the asetrate filter with the aresample filter, and using the atempo filter.
# asetrate and aresample
ffmpeg -i drift.mp4 -c:v copy -af "asetrate=48048,aresample=48000" \
-c:a aac -b:a 128k drift_asetrate.mp4
# atempo
ffmpeg -i drift.mp4 -c:v copy -af "atempo=1.001" \
-c:a aac -b:a 128k drift_atempo.mp4
asetrate keeps the sample data unchanged and only rewrites the sample rate value. If you treat 48000Hz audio as 48048Hz (48000 × 1.001), it plays 0.1% faster. aresample=48000 then converts it back to 48000Hz. As the official documentation notes, this method changes the pitch along with the speed. atempo changes the tempo (speed) directly, and 1.001 means 0.1% faster.
I applied each filter to 10 seconds of a 1000Hz tone and counted the waves over the 6 seconds from 2 to 8 seconds to find the frequency. atempo=1.001 kept the pitch at 1000.00Hz, while asetrate=48048,aresample=48000 gave 1001.00Hz. To make the difference easier to see, I also tried 10% faster: atempo=1.1 gave 1000.00Hz and asetrate=52800,aresample=48000 gave 1100.00Hz. For the 1000Hz tone I tested, atempo did not change the pitch.
I plotted the sync error at 30 points:

The video fixed with asetrate and aresample had a sync error of 0.000 at all 30 points. The audio is 59.951 seconds long, slightly shorter than the 60-second video (59.929 seconds with atempo).
The video fixed with atempo also no longer had a sync error that grew over time. However, its sync error ranged from −0.021 to −0.007 seconds across the 30 points. At 25 of those points, the beep played about 0.02 seconds before the video. Looking at the first beep with silencedetect, the asetrate version played from 0.999917 to 1.04992 seconds, while the atempo version played from 0.980583 to 1.04563 seconds. The 0.05-second sound started about 0.02 seconds early and was detected as roughly 0.065 seconds long.
--- drift_atempo
silence_end: 0.980583
silence_start: 1.04563
--- drift_asetrate
silence_end: 0.999917
silence_start: 1.04992
0.02 seconds is less than one frame at 30fps (about 0.033 seconds). If you want to keep the pitch unchanged, use atempo. If you want the sync error at exactly 0, use asetrate with aresample.
Calculating the Speed Factor
In drift.mp4, the audio lagged by about 0.06 seconds over 60 seconds. The audio content was stretched by a factor of 1.001, so playing it at 1.001 times the speed restores the original timing. For asetrate, multiply the original sample rate by this value (48000 × 1.001 = 48048). For atempo, use the value directly.
To find the stretch factor, measure the sync error at two points, one near the start and one near the end of the video. Divide the difference between the two errors by the number of seconds between the two points, then add 1. For drift.mp4, the sync error was +0.001 seconds at 1 second and +0.059 seconds at 59 seconds. So (0.059 − 0.001) ÷ (59 − 1) = 0.001, and 1 + 0.001 = 1.001.




