How to Concat Multiple Video and Audio Streams in FFmpeg

Concatenating multiple media files with both video and audio tracks in FFmpeg requires the use of the concat complex filter. This article provides a straightforward guide on how to merge multiple video and audio streams seamlessly using a single FFmpeg command, explaining the syntax, filter parameters, and stream mapping required to achieve a clean output file.

The FFmpeg Concat Filter Command

To concatenate multiple input files containing both video and audio, you must use the -filter_complex option. This filter decodes the inputs, joins them sequentially in memory, and encodes them into a single output file.

Here is the standard command to concatenate three input files (input1.mp4, input2.mp4, and input3.mp4):

ffmpeg -i input1.mp4 -i input2.mp4 -i input3.mp4 \
-filter_complex "[0:v][0:a][1:v][1:a][2:v][2:a]concat=n=3:v=1:a=1[outv][outa]" \
-map "[outv]" -map "[outa]" output.mp4

How the Command Works

Important Requirements for Success

For the concat filter to work successfully without errors or sync issues, the input streams must meet the following criteria:

  1. Matching Dimensions: All input video streams must have the exact same width and height (e.g., all must be 1920x1080). If they do not match, you must scale them before concatenating.
  2. Matching Frame Rates: The input videos should share the same frame rate (fps) and timebase.
  3. Matching Audio Sample Rates: The audio streams must share the same sample rate (e.g., 44100Hz or 48000Hz) and channel layout (e.g., stereo or mono).