FFmpeg - Concatenate MP4 Files: Difference between revisions

From PiRho Knowledgebase
Jump to navigationJump to search
No edit summary
Dex (talk | contribs)
No edit summary
Line 1: Line 1:
[[Category:FFmpeg]]
[[Category:FFmpeg]]
[[Category:MP4]]
[[Category:MP4]]
== 1. concat video filter ==
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.
Use this method if your inputs do not have the same parameters (width, height, etc), or are not the same formats/codecs, or if you want to perform any filtering.<br/>


Note that this method performs a re-encode of all inputs. If you want to avoid the re-encode, you could re-encode just the inputs that don't match so they share the same codec and other parameters, then use the concat demuxer to avoid re-encoding everything.<br/>
== 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 ===
<pre>
<pre>
ffmpeg -i opening.mkv -i episode.mkv -i ending.mkv \
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
-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
</pre>
</pre>
== 2. concat demuxer ==
Use this method when you want to avoid a re-encode and your format does not support file-level concatenation (most files used by general users do not support file-level concatenation).<p>


== 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 ===
<pre>
<pre>
$ cat mylist.txt
file '/path/to/file1'
file '/path/to/file1'
file '/path/to/file2'
file '/path/to/file2'
file '/path/to/file3'
file '/path/to/file3'
</pre>
</pre>
   
<code>$ ffmpeg -f concat -i mylist.txt -c copy output.mp4</code><br/>
For Windows:<br/>


=== Concatenate Without Re‑Encoding ===
<code>
ffmpeg -f concat -i mylist.txt -c copy output.mp4
</code>
=== Windows Examples ===
==== Manual list: ====
<pre>
<pre>
(echo file 'first file.mp4' & echo file 'second file.mp4' )>list.txt
(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
</pre>
</pre>
or<br/>


==== Automatically list all MP4 files: ====
<pre>
<pre>
(for %i in (*.mp4) do @echo file '%i') > list.txt
(for %i in (*.mp4) do @echo file '%i') > list.txt
Line 36: Line 49:
</pre>
</pre>


== 3. concat protocol ==
== Notes ==
Use this method with formats that support file-level concatenation (MPEG-1, MPEG-2 PS, DV). Do not use with MP4.<br/>
* 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 ===
<code>
ffmpeg -i "concat:input1|input2" -codec copy output.mkv
</code>
 
== 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 ==
{| class="wikitable"
! 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
|}


<code>ffmpeg -i "concat:input1|input2" -codec copy output.mkv</code><br/>
== Reference ==
This method does not work for many formats, including MP4, due to the nature of these formats and the simplistic physical concatenation performed by this method. It's the equivalent of just raw joining the files.<br/>
* https://stackoverflow.com/questions/7333232/how-to-concatenate-two-mp4-files-using-ffmpeg
Reference: [[https://stackoverflow.com/questions/7333232/how-to-concatenate-two-mp4-files-using-ffmpeg stackoverflow: How to concatenate two MP4 files using FFmpeg?]]

Revision as of 13:53, 14 March 2026

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