Merging multiple AVI files into one on Linux

Occasionally I come across AVI files that have been split up, for whatever reason, and I want to put them back into one file. Every time I do, I end up having to dig through tonnes of Google search results to find what I want and then make sense of it. Therefore I decided to figure out the best way to get this work for me and then do this little write up.

What I discovered, is that it’s actually VERY easy and you can do it in one command.

Step 1 – Install mencoder

This is easy. Simply execute the following:

sudo apt-get install mencoder

This will install mencoder which is the program we’ll use to merge and fix up the AVI files. This will also install any dependencies along with it.

Step 2 – Merge and re-index

This is the meat of it. It’s just one command that until now I never knew could actually take multiple files for input.

mencoder -forceidx -oac copy -ovc copy file1.avi file2.avi -o output.avi

This command does a couple of things in one go. Not only will it merge the file1.avi and file2.avi files, it will re-index the streams so that the audio and video are synced correctly. This is usually a problem when you merge files the ‘old fashioned’ way by simply running cat file1.avi file2.avi > output.avi. Now because this isn’t actually changing any of the audio or video data (it’s just copying it) this should be quite quick. I merged two ~700MB files and it took about 5 minutes.

You could also, if you want, change the -ovc copy to make it re-compress the video to make the file smaller. So maybe I’ll do that in another post.

Submit a Comment

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>