FFmpeg can join videos in two ways. If all the files use the same codec and format, the concat demuxer joins them without re-encoding. If they differ, the concat filter re-encodes them into one file.

Comparing the Two concat Approaches

Criterion concat demuxer concat filter
Speed Fast (no re-encoding) Slow (re-encoding required)
Compatibility requirement Same codec and format required Works with different codecs (resolution and SAR must match)
Number of input files Any number supported One -i flag per input file required
File list Requires a text file (list.txt) Not required
Command complexity Simple Requires filter_complex syntax

concat demuxer (Fast, Same-Format Files)

Overview

You list the files in a text file, and the concat demuxer joins them in that order. It works without re-encoding, so it is very fast.

Creating the File List (list.txt)

Create a text file named list.txt in this format:

# Example list.txt (comment lines start with #)
file 'part1.mp4'
file 'part2.mp4'
file 'part3.mp4'

To create it from the command line on Linux:

printf "file 'part1.mp4'\nfile 'part2.mp4'\nfile 'part3.mp4'\n" > list.txt

To create it from the Windows Command Prompt:

(echo file 'part1.mp4' & echo file 'part2.mp4' & echo file 'part3.mp4') > list.txt

Running the concat demuxer

ffmpeg -f concat -safe 0 -i list.txt -c copy output.mp4
Option Meaning
-f concat Use the concat demuxer
-safe 0 Disable safe path checking (required for absolute paths or special characters)
-i list.txt Specify the file list as input
-c copy No re-encoding (stream copy)

Notes on Using concat demuxer

  • All files must have the same codec, resolution and frame rate.
  • If the formats differ, you can get errors, or the audio and video can go out of sync.
  • Without -safe 0, absolute paths and file names that contain spaces, non-ASCII characters (such as Japanese) or other special characters fail with Unsafe file name.

concat filter (Flexible, Supports Mixed Formats)

Overview

The concat filter, used inside filter_complex, can join files even when their codecs or containers differ. The resolution and SAR (sample aspect ratio, the shape of each pixel) must match, so convert them first if they differ (see below). This method always re-encodes.

Basic Command (Joining 2 Files)

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

Explaining the filter_complex Syntax

[0:v][0:a][1:v][1:a]concat=n=2:v=1:a=1[outv][outa]
Part Meaning
[0:v][0:a] Video and audio streams from the first input (index 0)
[1:v][1:a] Video and audio streams from the second input (index 1)
concat=n=2 Join 2 segments (change to match the number of files)
v=1:a=1 Output 1 video stream and 1 audio stream
[outv][outa] Output labels (referenced by -map)

Joining 3 Files

ffmpeg -i input1.mp4 -i input2.mp4 -i input.mkv -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

Set n=3 and list the video and audio streams of every input.

Joining with a Specified Codec

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]" -c:v libx264 -crf 23 -c:a aac output.mp4

Normalizing Resolution and Frame Rate

The concat filter stops with an error if the resolution or SAR differs. Different frame rates do not cause an error; the output then has a variable frame rate. Before joining, match the resolution and SAR with the scale and setsar filters, and the frame rate with the fps filter.

ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[0:v]scale=1280:720,setsar=1,fps=30[v0];[1:v]scale=1280:720,setsar=1,fps=30[v1];[v0][0:a][v1][1:a]concat=n=2:v=1:a=1[outv][outa]" -map "[outv]" -map "[outa]" -c:v libx264 -crf 23 -c:a aac output.mp4
Added Filter Purpose
scale=1280:720 Normalize resolution
setsar=1 Normalize SAR (sample aspect ratio) to 1:1 (sources with a different aspect ratio are stretched)
fps=30 Normalize frame rate

Choosing the Right Method

Choose concat demuxer when:

  • All files share the same codec and resolution
  • You have many files and want to join them quickly, without re-encoding
  • The files are all alike, such as parts of a recording that was split into several files

Choose concat filter when:

  • Files have different codecs or resolutions (scale them to one resolution first)
  • You want a script to pass the files directly (no list file needed)
  • You want to join trimmed parts in the same command

Common Errors and Solutions

DTS ... out of order Error

This happens when the concat demuxer joins files whose timestamps are not continuous. Replace -c copy with -c:v libx264 -c:a aac.

Audio Goes Out of Sync

This happens when the frame rates or audio sample rates differ. Use the concat filter, and match them first with the fps and aresample filters.

Specific Files Are Skipped

If a path in list.txt contains spaces or special characters, check that it is quoted correctly.