FFmpeg - Concatenate MP4 Files: Difference between revisions
mNo edit summary |
No edit summary |
||
| (2 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
[[Category:FFmpeg]] | [[Category:FFmpeg]] | ||
[[Category:MP4]] | |||
Concatenating MP4 files in FFmpeg can be done using three official methods. Each method behaves differently, has different requirements, and supports different levels of compatibility. This article explains when to use each method, how they work, and common pitfalls to avoid—especially when working with MP4, which is strict about matching codecs and container metadata. | |||
== 1. concat Video Filter == | |||
The concat filter works at the decoded‑frame level. FFmpeg fully decodes each input, merges the streams, and then re‑encodes the result. | |||
=== Use this method when: === | |||
* Your input files do not match (resolution, codec, bitrate, frame rate, audio layout). | |||
* You need to apply filters, scaling, overlays, trimming, or processing. | |||
* A full re‑encode is acceptable or required. | |||
Because the filter merges raw audio and video frames, re‑encoding is mandatory. | |||
=== Example: Concatenating Three Clips === | |||
(echo file 'first file.mp4' & echo file 'second file.mp4' )>list.txt | ``` | ||
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 === | |||
* Most flexible method; works with mismatched or problematic sources. | |||
* If you want to avoid re‑encode, normalise the inputs first, then use the concat demuxer. | |||
== 2. concat Demuxer == | |||
The concat demuxer is the recommended MP4 method when files are perfectly matching. It concatenates at the container level while rewriting metadata, allowing stream copying (-c copy) for a lossless, fast result. | |||
=== Use this method when: === | |||
* All inputs are identical in codec, profile, level, frame rate, resolution, pixel format, audio codec, and channel layout. | |||
* You want to avoid re‑encoding. | |||
* You're joining MP4 files exported from the same device or encoder. | |||
=== Create a List File === | |||
``` | |||
file '/path/to/file1.mp4' | |||
file '/path/to/file2.mp4' | |||
file '/path/to/file3.mp4' | |||
``` | |||
=== 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 | 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 | (for %i in (*.mp4) do @echo file '%i') > list.txt | ||
ffmpeg -safe 0 -f concat -i list.txt -c copy output.mp4 | ffmpeg -safe 0 -f concat -i list.txt -c copy output.mp4 | ||
``` | |||
=== Notes === | |||
* All technical parameters must match, or FFmpeg cannot stream copy. | |||
* Safest method for MP4. | |||
* -safe 0 allows absolute or unusual paths. | |||
* Use UTF‑8 list files for international filenames. | |||
== 3. concat Protocol == | |||
This is a raw byte-level concatenation method used only for formats that can be joined without adjusting metadata. | |||
=== Use this only for: === | |||
* MPEG‑1 | |||
* MPEG‑2 Program Streams | |||
* DV | |||
=== Example === | |||
``` | |||
ffmpeg -i "concat:input1|input2" -codec copy output.mkv | |||
``` | |||
== Why This Fails for MP4 == | |||
The concat protocol does not rewrite container metadata. MP4 containers require updated moov atoms, index tables, durations, and sample tables. Raw concatenation usually corrupts playback, causes wrong durations, broken seeking, or player failures. | |||
== | == Summary == | ||
Use | {| class="wikitable" | ||
! Method !! Re‑Encode? !! MP4 Support !! Best Use Case | |||
|- | |||
| concat filter || Yes || Yes || Mismatched sources, filtering, full control | |||
|- | |||
| concat demuxer || No || Yes (if identical) || Safe MP4 joining with stream copy | |||
|- | |||
| concat protocol || No || No || MPEG‑1 / MPEG‑2 PS / DV only | |||
|} | |||
== Reference == | |||
* https://stackoverflow.com/questions/7333232/how-to-concatenate-two-mp4-files-using-ffmpeg | |||
Latest revision as of 14:36, 14 March 2026
Concatenating MP4 files in FFmpeg can be done using three official methods. Each method behaves differently, has different requirements, and supports different levels of compatibility. This article explains when to use each method, how they work, and common pitfalls to avoid—especially when working with MP4, which is strict about matching codecs and container metadata.
1. concat Video Filter
The concat filter works at the decoded‑frame level. FFmpeg fully decodes each input, merges the streams, and then re‑encodes the result.
Use this method when:
- Your input files do not match (resolution, codec, bitrate, frame rate, audio layout).
- You need to apply filters, scaling, overlays, trimming, or processing.
- A full re‑encode is acceptable or required.
Because the filter merges raw audio and video frames, re‑encoding is mandatory.
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
- Most flexible method; works with mismatched or problematic sources.
- If you want to avoid re‑encode, normalise the inputs first, then use the concat demuxer.
2. concat Demuxer
The concat demuxer is the recommended MP4 method when files are perfectly matching. It concatenates at the container level while rewriting metadata, allowing stream copying (-c copy) for a lossless, fast result.
Use this method when:
- All inputs are identical in codec, profile, level, frame rate, resolution, pixel format, audio codec, and channel layout.
- You want to avoid re‑encoding.
- You're joining MP4 files exported from the same device or encoder.
Create a List File
``` file '/path/to/file1.mp4' file '/path/to/file2.mp4' file '/path/to/file3.mp4' ```
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 technical parameters must match, or FFmpeg cannot stream copy.
- Safest method for MP4.
- -safe 0 allows absolute or unusual paths.
- Use UTF‑8 list files for international filenames.
3. concat Protocol
This is a raw byte-level concatenation method used only for formats that can be joined without adjusting metadata.
Use this only for:
- MPEG‑1
- MPEG‑2 Program Streams
- DV
Example
``` ffmpeg -i "concat:input1|input2" -codec copy output.mkv ```
Why This Fails for MP4
The concat protocol does not rewrite container metadata. MP4 containers require updated moov atoms, index tables, durations, and sample tables. Raw concatenation usually corrupts playback, causes wrong durations, broken seeking, or player failures.
Summary
| Method | Re‑Encode? | MP4 Support | Best Use Case |
|---|---|---|---|
| concat filter | Yes | Yes | Mismatched sources, filtering, full control |
| concat demuxer | No | Yes (if identical) | Safe MP4 joining with stream copy |
| concat protocol | No | No | MPEG‑1 / MPEG‑2 PS / DV only |