How to Sync Video and Audio in FFmpeg with -async
When muxing separate video and audio streams, synchronization issues
can often occur, causing the audio to drift ahead of or lag behind the
video. This article provides a straightforward guide on how to use
FFmpeg’s -async option to align your audio stream with the
video stream, ensuring perfect playback synchronization in your output
file.
The Basic Muxing Command
with -async
To combine a video file and an audio file while keeping them synchronized, use the following FFmpeg command structure:
ffmpeg -i input_video.mp4 -i input_audio.wav -c:v copy -c:a aac -async 1 output.mp4How the Command Works
-i input_video.mp4: Specifies the source video stream.-i input_audio.wav: Specifies the source audio stream.-c:v copy: Copies the video stream directly without re-encoding, saving time and preserving original video quality.-c:a aac: Encodes the audio stream to the AAC format (recommended for MP4 containers).-async 1: Instructs FFmpeg to synchronize the audio stream to the video stream.output.mp4: The final synchronized output file.
Understanding the
-async Option
The -async parameter synchronizes the start of the audio
stream with the video stream.
- Value of
1(-async 1): This is a special mode where FFmpeg corrects the start of the audio stream to match the first video frame. It does this by either adding silence (padding) at the beginning if the audio starts late, or trimming the audio if it starts too early. - Values greater than
1: If you specify a higher sample rate limit (e.g.,-async 48000), FFmpeg will stretch or squeeze the audio stream incrementally throughout the file to match the video timestamps, correcting drift over longer durations.
Modern Alternative: The
aresample Filter
In newer versions of FFmpeg, the standalone -async
option is deprecated in favor of the audio resampling filter. If you
encounter issues with -async, you can achieve the exact
same synchronization effect using the -af (audio filter)
flag:
ffmpeg -i input_video.mp4 -i input_audio.wav -c:v copy -c:a aac -af aresample=async=1 output.mp4This filter-based approach is highly robust and performs the same stream stretching, squeezing, or padding required to keep your media perfectly aligned.