Merge Video and Audio Streams with FFmpeg Concat

This article provides a step-by-step guide on how to use the FFmpeg concat filter to merge multiple video and audio streams into a single cohesive file. You will learn the core syntax of the filter_complex command, how to define your inputs and outputs, and how to handle files with differing resolutions or formats.

The Basic Concat Filter Command

The concat filter is ideal when your input videos have different formats, codecs, or resolutions because it decodes the files, joins them, and re-encodes the final output.

Here is the standard command to merge 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.mp4

Command Breakdown

Merging Three or More Files

To merge more than two files, add the extra inputs, specify their streams in the filter graph, and increase the n value. Here is an example with three files:

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

Handling Mismatched Video Dimensions

The concat filter requires all input streams to have the same width, height, and frame rate. If your source videos have different resolutions, you must scale them to a uniform size within the filter complex before concatenating them.

The following command scales both inputs to 1920x1080 before merging:

ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[0:v]scale=1920:1080[v0]; [1:v]scale=1920:1080[v1]; [v0][0:a][v1][1:a] concat=n=2:v=1:a=1 [outv][outa]" -map "[outv]" -map "[outa]" output.mp4

In this command: * [0:v]scale=1920:1080[v0] resizes the first video and labels it [v0]. * [1:v]scale=1920:1080[v1] resizes the second video and labels it [v1]. * [v0] and [v1] are then passed into the concat filter instead of the raw input streams.