FFmpeg - Concatenate MP4 Files

From PiRho Knowledgebase
Revision as of 13:53, 14 March 2026 by Dex (talk | contribs)
Jump to navigationJump to search

Concatenating video files in FFmpeg can be achieved in several ways, depending on your input formats, codecs, and whether you want to avoid re‑encoding. This article explains the three official FFmpeg concatenation methods, when to use each one, and common pitfalls.

1. concat Video Filter

Use this method when:

  • Your inputs do not share the same properties (resolution, frame rate, codec).
  • You want to apply filters, scaling, or other transformations.
  • You are comfortable with a full re‑encode of all inputs.

Because the filter merges decoded streams, FFmpeg must re‑encode everything.

Example: Concatenating Three Clips

ffmpeg -i opening.mkv -i episode.mkv -i ending.mkv -filter_complex "[0:v] [0:a] [1:v] [1:a] [2:v] [2:a] concat=n=3:v=1:a=1 [v] [a]" -map "[v]" -map "[a]" output.mkv

Notes

  • Ideal for complicated workflows or mismatched sources.
  • If you want to avoid full re‑encode, pre‑normalise the odd inputs first, then use the concat demuxer.

2. concat Demuxer

Use this method when you want to avoid re‑encoding and your formats support stream copying. This is the recommended method for MP4 as long as the files are perfectly matching (same codec, profile, level, frame rate, resolution, audio layout).

Create a List File

file '/path/to/file1'
file '/path/to/file2'
file '/path/to/file3'

Concatenate Without Re‑Encoding

ffmpeg -f concat -i mylist.txt -c copy output.mp4

Windows Examples

Manual list:

(echo file 'first file.mp4' & echo file 'second file.mp4') > list.txt
ffmpeg -safe 0 -f concat -i list.txt -c copy output.mp4

Automatically list all MP4 files:

(for %i in (*.mp4) do @echo file '%i') > list.txt
ffmpeg -safe 0 -f concat -i list.txt -c copy output.mp4

Notes

  • All parameters must match. Even slight differences can break stream copy.
  • Safest method for MP4 when conditions are met.

3. concat Protocol

Use this only when formats support raw file‑level concatenation (MPEG‑1, MPEG‑2 PS, DV). Do not use this for MP4.

Example

ffmpeg -i "concat:input1|input2" -codec copy output.mkv

Why This Fails for MP4

The concat protocol performs raw joining without rewriting container metadata. MP4 requires regenerated moov atoms and indices, so raw concatenation corrupts the container.

Summary

Method Re-Encode? MP4 Support Best Use Case
concat filter Yes Yes Mismatched sources, filtering
concat demuxer No Yes (if identical) Safe MP4 joining
concat protocol No No MPEG‑1 / MPEG‑2 PS / DV

Reference