Terminal tool to join mp4 videos

I would like to join mp4 videos like I join pdfs by pdfjoin.

Is there any default tool to join videos?


Yes, you can join multiple videos into one file using default terminal commands. Using a simple cat will do want you want.

cat video1.avi video2.avi videon.avi > output.avi

There is a big but here - this method will output the video1.avi header onto output.avi. Therefore, the header of output.avi is the same as video1.avi, so on most video players, it will look like video2.avi and videon.avi are lost - source 1, source 2.

To fix this, you need additional tools. There are many other terminal tools for joining movies that update the header as well. The most known video editing tools are mencoder (which is a part of MPlayer) and ffmpeg. Both have an enormous amount of options and settings and it is beyond this forum to go into much detail, but I will give the short commands below.

The use and installation of mencoder or ffmpeg can be intimidating, but the video editing capabilities are the most powerful I have encountered so far. As the installation from source can be a hassle for native OS X users, I advise you to use Homebrew.

To install these tools using Homebrew, run this one-liner in your terminal:

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

Homebrew is now installed, it is wise to follow the installer's suggestions after installation. Now we install ffmpeg and mencoder using brew.

brew install ffmpeg mplayer

Now we can use mencoder to merge the two videos:

mencoder -oac copy -ovc copy -idx -o output.mp4 video1.mp4 video2.mp4 video3.mp4

Or we can use ffmpeg to merge the two videos:

ffmpeg -i concat:"video1.mp4|video2.mp4" -codec copy output.mp4

Note that you may need to escape the character "|" which is special for many shells, so

ffmpeg -i concat:video1.mp4\|video2.mp4

You specifically ask for .mp4 files. mp4 is a container format, it is possible that the default installation of ffmpeg or mencoder does not have the correct coding/decoding ('codecs') packages by default and that you need to install these separately. But that is beyond the scope of this topic.


The first answer above didn't work for a recent version of ffmpeg, which would be 5 years now since this answer was given.

So in order to do a simple concatenation of video files without re-encoding them using a modern version of ffmpeg (currently 4.4.1):

Create a .txt file with each line containing the files you want to concatenate, and each filename needs to be preceded by the word file and the filename needs to be surrounded by tick-marks. It should end up looking like this:

file 'Video1.mov'
file 'Video2.mov'
file 'Video3.mov'

Also, if you're running the ffmpeg command from the same folder that your files and the text file exist in, then no need to worry about using absolute paths in the text file.

Save that file as whatever ... I called mine files.txt.

Then this command will join them together and put the result into a single video file:

ffmpeg -f concat -safe 0 -i files.txt -c copy VideoFinal.mov

If you need to use absolute paths in your text file, then you would OMIT the -safe 0 from the command like so:

ffmpeg -f concat -i files.txt -c copy VideoFinal.mov

This worked like a champ for me and it joined a couple of hundred megs of video in less than three seconds.