How to Use the FFmpeg Concat Filter
This article provides a straightforward guide on how to use the
concat filter in FFmpeg to merge multiple video and audio
files. You will learn the basic syntax of the filter, how to combine
clips with different properties, and see practical command-line examples
for seamless video concatenation.
The FFmpeg concat filter is used to join multiple video
and audio streams back-to-back. Unlike the concat demuxer,
which simply appends files without re-encoding, the concat
filter re-encodes the files. This makes it ideal for joining videos that
have different formats, codecs, resolutions, or frame rates.
The Basic Syntax
To use the concat filter, you must use a complex
filtergraph (-filter_complex). The basic parameters for the
filter are:
n: The number of input files to concatenate. (Default is 2).v: The number of output video streams. (Default is 1).a: The number of output audio streams. (Default is 0).
Step-by-Step Example
Here is a standard command to concatenate two video files that both contain video and audio streams:
ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[0:v][0:a][1:v][1:a]concat=n=2:v=1:a=1[outv][outa]" -map "[outv]" -map "[outa]" output.mp4Command Breakdown:
-i input1.mp4 -i input2.mp4: Specifies the two input files.[0:v][0:a]: Selects the video (v) and audio (a) streams of the first input file (index0).[1:v][1:a]: Selects the video and audio streams of the second input file (index1).concat=n=2:v=1:a=1: Tells FFmpeg to concatenate2segments, resulting in1video output and1audio output.[outv][outa]: Names the resulting output streams.-map "[outv]" -map "[outa]": Maps the named output streams to the final output file,output.mp4.
Handling Different Resolutions and Aspect Ratios
The concat filter requires all input streams to have the
same width, height, and pixel format. If your input videos have
different resolutions, you must scale them to matching dimensions before
concatenating them.
You can chain the scale and setsar filters
inside the filtergraph before applying the concat
filter:
ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[0:v]scale=1920:1080,setsar=1[v0]; [1:v]scale=1920:1080,setsar=1[v1]; [v0][0:a][v1][1:a]concat=n=2:v=1:a=1[outv][outa]" -map "[outv]" -map "[outa]" output.mp4In this command, scale=1920:1080,setsar=1 resizes both
video streams to a standard 1080p resolution and sets a 1:1 sample
aspect ratio before feeding them into the concat filter as
[v0] and [v1].