When you want to join two or more videos in to a single video you have a couple of different options. The demuxer method is faster but have stricter requirements to the input. The concat filter is slower but can merge videos with different codeces, framerates etc.
Join two video with the same codec π
To avoid reencoding the files we can do this. If you want to merge two files that have the same properties (same codec, time base, etc) you can do it without re-encoding the inputs. The demuxer method is much faster than the concat filter method, so you want to use it whenenver possible.
As input for the ffmpeg
command we first have to generate a text file that contains all the inputs that we want to concatenate. Let’s call the file inputs.txt
.
file 'first_half.mp4'
file 'second_half.mp4'
Second, we call ffmpeg
with inputs.txt
as input.
ffmpeg -f concat -safe 0 -i inputs.txt -c copy output.mp4
Join two video with the different codecs π
If your videos cannot be merged using the demuxer you probably want to use the filter method instead. The command is a little more complex so I will give a few examples that get increasingly more complex.
Merge videos without audio π
We are only copying the video streams so the output will not contain any audio.
ffmpeg -i input.mp4 -i input.mp4 \
-filter_complex "[0:v] [1:v] \
concat [v]" \
-map "[v]" output.mp4
Merge two videos with audio and different codec π
With the concat filter we explictitly have to specify how we want the audio streams mapped in the output.
ffmpeg -i first_half.mp4 -i second_half.mp4 \
-filter_complex "[0:v] [0:a] [1:v] [1:a] \
concat=n=2:v=1:a=1 [v] [a]" \
-map "[v]" -map "[a]" output.mp4