Delay Audio by 2 Seconds Using FFmpeg
Audio desynchronization can ruin an otherwise perfect video, but
FFmpeg provides a quick, lossless way to fix this on
Linux. This article demonstrates how to delay an audio track by exactly
two seconds relative to the video using the -itsoffset
flag. By utilizing stream copying instead of re-encoding, you can repair
your media file in seconds without sacrificing any video or audio
quality.
The Solution Command
To delay the audio track by two seconds, open your Linux terminal and run the following command:
ffmpeg -i input.mp4 -itsoffset 2.0 -i input.mp4 -map 0:v -map 1:a -c copy output.mp4How This Command Works
The command works by taking the same video file as two separate inputs and shifting the timing of the second input. Here is a breakdown of each argument:
-i input.mp4: Defines the first input file (index0). FFmpeg will read the video track from this instance.-itsoffset 2.0: This is the crucial flag. It applies a time offset (delay) to any input file that follows it. In this case, it adds a 2-second delay.-i input.mp4: Defines the second input file (index1). Because it follows the offset flag, its tracks are delayed. FFmpeg will read the audio from this instance.-map 0:v: Tells FFmpeg to take the video (v) from the first input (0), which has no delay.-map 1:a: Tells FFmpeg to take the audio (a) from the second input (1), which is delayed by 2 seconds.-c copy: Instructs FFmpeg to copy both the video and audio streams directly without re-encoding them. This makes the process nearly instantaneous and prevents quality loss.output.mp4: The name of your newly fixed video file.
Shifting Audio the Other Way (Advanced)
If your audio is playing too late and you need it to play earlier by two seconds, you simply swap the offset to the video track instead.
ffmpeg -itsoffset 2.0 -i input.mp4 -i input.mp4 -map 0:v -map 1:a -c copy output.mp4By placing the -itsoffset 2.0 before the first input,
you delay the video track instead of the audio track, effectively making
the audio track lead the video by two seconds.