Merge multiple mp4 files into a single video via the terminal
Solution 1
I used the following bash command in one of my old scripts, and I am sure it works, provided
- all videos have absolutely same size, frame-per-second, and codec.
- you have mencoder installed
you specify the variables ${FPS} etc. before running the command, for example:
FPS=24
videoX=640
videoY=480
Here is the command:
mencoder -really-quiet -ovc lavc -lavcopts vcodec=mjpeg -mf fps=${FPS} -vf scale=${videoX}:${videoY} -o $output_video_file_name video_*.avi
Here I assumed you have your videos under files "video_001.avi" "video_blah.avi" etc.
Solution 2
Install package gpac
apt install gpac
That has a utility MP4Box that can concatenate:
MP4Box [-keepsys] -cat FILE1+FILE2+FILE3 out.mp4
Ubuntu 14.04 LTS ymmv.
Related videos on Youtube
Blunderchips
Updated on September 18, 2022Comments
-
Blunderchips over 1 year
I am trying to concatenate a directory full of
.mp4
files into a single video through the terminal. I have found plenty of methods that say they can do this but either require FFmpeg (which, to the best of my knowledge, is no longer supported) or they simple do not work. Is it still possible to do this and if so how might I go about doing it?-
Tobias J about 7 yearsKinda funny this specifically states "via the terminal" yet all the answers in the supposed duplicate are GUI tools.
-
-
Blunderchips over 8 yearsAt the end of the first video the image freezes yet the audio for the second continues to play..... not ideal
-
JdeHaan over 8 yearsI have the following "guaranteed to work"(tm), but that requires converting mp4 to ts:
avconv -ss 0 -i "INPUT.mp4" -vcodec libx264 -acodec aac -bsf:v h264_mp4toannexb -f mpegts -strict experimental -y "OUTPUT.ts" avconv -i concat:"OUTPUT1.ts|OUTPUT2.ts" -c copy -bsf:a aac_adtstoasc -y "FINAL.mp4"
-
Masoud over 8 yearsOK, now I see that there was a lot of bloat in my answer above. This is the bare minimum you need to merge multiple videos:
mencoder -c copy video_*.avi -o final.avi
The input videos are sorted alphabetically. If you want a particular order, change the names to (for example) 01.avi 02.avi etc. to make sure you get the desired order. -
Murali almost 7 yearsThis command worked for me:
mencoder -oac pcm -ovc copy -o output_file.mp4 input_file1.mp4 input_file2.mp4
-
joan16v almost 5 yearsMP4Box -add file1.mp4 -cat file2.mp4 output.mp4
-
Aveesh over 4 yearsThis worked. mencoder -oac pcm -ovc copy -o output_file.mp4 input_*.mp4