Fix Audio Video Sync Using FFmpeg itsoffset
Audio and video desynchronization is a common issue in video
processing, but it can be easily resolved using FFmpeg. This article
provides a straightforward guide on how to use the
-itsoffset option in FFmpeg to shift the audio stream
relative to the video, allowing you to quickly restore perfect
synchronization to your media files without the need for full
re-encoding.
Understanding
-itsoffset
The -itsoffset option in FFmpeg applies a time offset to
the input file that immediately follows it in the command line. The
offset value is specified in seconds (e.g., 1.5) or in the
hh:mm:ss.msecs format.
Because FFmpeg processes inputs sequentially, correcting sync issues
requires loading the source file twice: once for the video stream and
once for the audio stream. By applying -itsoffset to only
one of these inputs and mapping the streams correctly, you can shift the
audio forward or backward.
Scenario 1: Delaying the Audio (Audio Plays Too Early)
If the audio is playing ahead of the video, you need to delay the audio stream. If the audio is 1.5 seconds too early, use the following command:
ffmpeg -i input.mp4 -itsoffset 1.5 -i input.mp4 -map 0:v -map 1:a -c copy output.mp4How this command works: * -i input.mp4:
This is the first input (index 0). The video will be taken from here
without any offset. * -itsoffset 1.5 -i input.mp4: This is
the second input (index 1). It is delayed by 1.5 seconds. *
-map 0:v: Selects the video from the first, non-delayed
input. * -map 1:a: Selects the audio from the second,
delayed input. * -c copy: Copies both streams without
re-encoding, preserving original quality and finishing the process
almost instantly.
Scenario 2: Advancing the Audio (Audio Plays Too Late)
If the audio is lagging behind the video, you need to make the audio play sooner. In FFmpeg, this is achieved by delaying the video stream instead of the audio stream. If the audio is 2 seconds too late, use this command:
ffmpeg -itsoffset 2.0 -i input.mp4 -i input.mp4 -map 0:v -map 1:a -c copy output.mp4How this command works: *
-itsoffset 2.0 -i input.mp4: This is the first input (index
0). The video will be taken from here, delayed by 2 seconds. *
-i input.mp4: This is the second input (index 1), which has
no delay. * -map 0:v: Selects the delayed video. *
-map 1:a: Selects the non-delayed audio. *
-c copy: Copies the streams instantly without
re-encoding.
By shifting the video forward relative to the audio, the lagging audio stream is successfully synchronized.