Combining Two Mpeg (or Other Video Types) Files With Ffmpeg
Combining two video files in an graphical editor is easy, but what if you had reason to combine several dozen videos at once? Unless that editor has some form of scripting you can use, the process could take hours. On the other hand, combining the files in command line is not intuitive, but is far easier to script.
This tip assumes that the file is already in mpg format. Some formats may also work with this method, while others may have to be converted first.
First run, cat 1.mpg 2.mpg >> temp.mpg
The command cat
reads the data into the input. Normally this would simply read a mess of unreadable (to humans) information into the terminal, but we are using the append operator, >> to direct the output to the file temp.mpg
Now you have a file that has all the video data, and the size of both video files put together, but if you try to play it, it will only play the first file (actually, some video players might be able to play it all. VLC tends to be able to read despite difficulties). The problem is that the metadata does not reflect the combined length of the two files, the media player only picking up on the first file’s metadata. To rectify this problem, you have to re-encode the file. Fortunately, ffmpeg makes this easy.
ffmpeg -i temp.mpg -sameq final.mpg
. In this command the video name following the -i
flag is the input, and the final name is the output file. The sameq
flag ensures that the same level of video quality is preserved between the two files. Ffmpeg is able to determine the codec based off file extensions.
Once ffmpeg is finished encoding, you should be able to watch your newly combined video.