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

  • -i input1.mp4 -i input2.mp4: Specifies the input files. You can add as many inputs as you need.
  • -filter_complex: Tells FFmpeg to use a complex filtergraph, which is required when dealing with multiple inputs and outputs.
  • [0:v][0:a]: Selects the video and audio streams of the first input file (index 0).
  • [1:v][1:a]: Selects the video and audio streams of the second input file (index 1).
  • concat=n=2:v=1:a=1: Calls the concat filter.
    • n=2 tells the filter there are 2 input segments.
    • v=1 specifies that there should be 1 output video stream.
    • a=1 specifies that there should be 1 output audio stream.
  • [outv][outa]: Names the temporary output video and audio streams created by the filter.
  • -map "[outv]" -map "[outa]": Maps the filtered streams to the final output file.

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.