Sunday, October 6, 2013

Join Multiple Video Files

I wanted to join a few .mp4 video files together on a Linux host using free software.With my limited experience with avconv (and ffmpeg) and some web search, I attempted to join the .mp4 files using the following command,
avconv -i concat:1.mp4\|2.mp4\|3.mp4 all.mp4
Unfortunately, the above command did not produce the expected result. It threw an exception and stopped at the end of the first .mp4 file, which was also observed by a few frustrated users, e.g.,

The reason turns out to be that the concatenation operation cannot be performed on .mp4 file, which is also suggested in the answer to the last of the three examples above.

The solution to the problem is in fact included in the FAQ of libav and that of ffmpeg, which is suggested by a stackoverflow post and answer.  Below is how I actually got it done,
avconv -i 1.mp4 1.mpeg
avconv -i 2.mp4 2.mpeg
avconv -i 3.mp4 3.mpeg
cat 1.mpeg 2.mpeg 3.mpeg | avconv -f mpeg -i - -vcodec mpeg4 -strict experimental output.mp4
ffmpeg -i 1.mp4 1.mpeg
ffmpeg -i 2.mp4 2.mpeg
ffmpeg -i 3.mp4 3.mpeg
cat 1.mpeg 2.mpeg 3.mpeg | ffmpeg -f mpeg -i - -vcodec mpeg4 -strict experimental output.mp4
This solution is also suggested by a few posts, such as, this one, this one.

If you observe quality degradation in the resulting video, you may add the "-qscale" option, such as,
avconv -i 1.mp4 -qscale 1 1.mpeg
where the value of qscale is between 1 and 0 with 1 being the highest quality (also the largest file) and 0 being the lowest quality (also the smallest file).

Note that many web posts, when discussing the tools, refer the "sameq" option, which apparently not supported any more, as discussed here.

By the way, when I was looking for a solution to the "join-mp4-vidoe" problem, I happened to come across a post on the history of libav and ffmpeg that I enjoyed reading and that answers my question, "why are there two almost identical but different libraries and tools, libav and ffmpeg?"


  1. Researching the same question, it seems there is a better answer than converting mpeg -- at bottom shows how to losslessly combine MP4s with intermediate extraction to .ts :)

    Tried it (Ubuntu ffmpeg current build December 2014) and it worked like a champ!

    1. I believe that you are referring to the mmcat script discussed in the page. I concur that the script is very handy and users can use it easily avoiding the effort of understanding the command line options of ffmpeg.

      Thanks for pointing it out.