HOWTO Merge video files

From LinuxReviews
Jump to navigationJump to search

Merging two video files into one is easy or impossible depending on what tool you use for the job and what kind of files you want to merge. It's super-easy if resolution as well as audio format, video format and container match. It gets dicey very quickly if they don't.

Introduction[edit | edit source]

Video file merging requires that the files you want to merge have the same resolution and file format. You have to convert if they don't, see HOWTO Convert video files.

Merging MPEG2 files founds on DVDs etc[edit | edit source]

This is actually the simplest of the simple examples of how files can be merged. Multiple MPEG2 files of the same resolution can simply be squished into one file with cat. If you have mounted a DVD on say /mnt/tmp/ you will have a /mnt/tmp/VIDEO_TS/ folder with MPEG2-formatted files called VTS_01_0.VOB, VTS_01_1.VOB and so on. You can simply cat these into one file:

cat VTS_01_0.VOB VTS_01_1.VOB > $HOME/MyDVDRIPedFile.mpg

Merging video files with ffmpeg[edit | edit source]

ffmpeg has an option called concat for joining two files which have identical formats. It's used like this:

ffmpeg -i "concat:file1.mp4|file2.mp4|file3.mp4" MyFineOutput.mp4

This will only work if the files have the same resolution and the same video and audio formats.

The concat filter is required if they do not have the same video format or audio format or container:

ffmpeg -i videofile1.mp4 -i videofile2.mkv -filter complex "[0:v:0] [0:a:0] [1:v:0] [1:a:0] concat=n=2:v=1:a=1 [v] [a]" -map "[v]" -map "[a]" MyFineOutput.mp4

Kemonomimi rabbit.svg
Note: complex filter still requires that the files have the same frame-rate and resolution. You can't just merge some 720p with a 1080p file using this method. You will have to convert the file(s) so that they have the same frame-rate and resolution.

Do notice how the complex works: You need to add a video and an audio option for each file and a number option to concat=. You need to add more if you want to merge say 3 files:

ffmpeg -i videofile1.mp4 -i videofile2.mkv -i videofile3.mkv -filter complex "[0:v:0] [0:a:0] [1:v:0] [1:a:0] [2:v:0] [2:a:0] concat=n=3:v=1:a=1 [v] [a]" -map "[v]" -map "[a]" MyFineOutput.mp4

You can go on adding these options and merge 10 files but at that point you're better off considering a video editor like kdenlive or pitivi.