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.mp4How the Command Works
-i input1.mp4 -i input2.mp4 -i input3.mp4: Specifies the input files. FFmpeg indexes these starting from 0 (input 1 is0, input 2 is1, input 3 is2).[0:v][0:a]: Selects the video (v) and audio (a) streams from the first input file (index 0). This pattern repeats for all inputs.concat=: Invokes the concatenation filter.n=3: Tells the filter the total number of input segments to expect (3 files).v=1: Specifies that each input segment has 1 video stream, and the filter should output 1 combined video stream.a=1: Specifies that each input segment has 1 audio stream, and the filter should output 1 combined audio stream.[outv][outa]: Names the temporary output pads for the merged video and audio.-map "[outv]" -map "[outa]": Maps these temporary output pads to the final output file (output.mp4).
Important Requirements for Success
For the concat filter to work successfully without
errors or sync issues, the input streams must meet the following
criteria:
- 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.
- Matching Frame Rates: The input videos should share the same frame rate (fps) and timebase.
- 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).