Merging multiple (more than two) videos on Ubuntu

10.5k Views Asked by At

I want to merge videos in batch size of twenty (20) each. I'm running a Linux machine. The videos are in mp4 format and moderate quality. Some even have the audio stream missing. So far I've tried ffmpeg, mencoder, cvlc/vlc and MP4Box. I want to write a command line script to achieve this, since I'm doing batch processing.

The main issue is that some of the solutions I tried work well for two videos, some work well for videos with audio stream and yet others work well for some other subset of my video set. However, I have not been able to find a comprehensive solution for this task.

3

There are 3 best solutions below

0
On

If ffmpeg parameter usage seems complex and causes fear, just like it did to me, another alternative is mkvmerge.

For installation instructions, follow >> MKVToolNix

mkvmerge -o /path/to/file/output_file.mkv /path/to/file/01.mp4 \+ /path/to/file/02.mp4 \+ /path/to/file/03.mp4 \+ ...

PS: I had no chance to try this on different formatted media types.

0
On

An easier option is to use Video editing tools like Kdenlive.

0
On

FFmpeg is the best way to accomplish this from the command line using the concat filter. I tried 20+ different ways before I found this and it works like a charm. If the videos are without audio, your command would be:

ffmpeg -i vid-1.mp4 -i vid-2.mp4 -filter_complex '[0:v] [1:v] concat=n=2:v=1 [v]' -map '[v]' output.mp4

If the videos have audio, your command would be:

ffmpeg -i vid-1.mp4 -i vid-2.mp4 -ar 44100 -ab 64k -ac 1 -c:a libmp3lame -filter_complex '[0:0] [0:1] [1:0] [1:1] concat=n=2:v=1:a=1 [v]' -map '[v]' -map '[a]' output.mp4

You can obviously substitute the audio bitrate, channels, etc. for other numbers, but these are very standard.