Merge multiple mp4 files into a single video via the terminal

27,473

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.

Share:
27,473

Related videos on Youtube

Blunderchips
Author by

Blunderchips

Updated on September 18, 2022

Comments

  • Blunderchips
    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
      Tobias J about 7 years
      Kinda funny this specifically states "via the terminal" yet all the answers in the supposed duplicate are GUI tools.
  • Blunderchips
    Blunderchips over 8 years
    At the end of the first video the image freezes yet the audio for the second continues to play..... not ideal
  • JdeHaan
    JdeHaan over 8 years
    I 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
    Masoud over 8 years
    OK, 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
    Murali almost 7 years
    This command worked for me: mencoder -oac pcm -ovc copy -o output_file.mp4 input_file1.mp4 input_file2.mp4
  • joan16v
    joan16v almost 5 years
    MP4Box -add file1.mp4 -cat file2.mp4 output.mp4
  • Aveesh
    Aveesh over 4 years
    This worked. mencoder -oac pcm -ovc copy -o output_file.mp4 input_*.mp4