Friday, April 23, 2010

Concatenating AVI Files with MEncoder

Suppose you have a bunch of AVI files, named part_01.avi, part_02.avi, etc. Suppose, furthermore, that you want to concatenate them into a single AVI file named concatenated.avi. Here's how to do it with MEncoder:
mencoder part_*.avi -o concatenated.avi -ovc copy -oac copy
This should work nicely as long as all parts have been encoded with the same video and audio encoders, using similar encoding parameters.

Now, suppose that you've downloaded a bunch of MP4 video files from, say, using a command line tool like get-flash-videos (highly recommended!), and you want to concatenate them into a single AVI file. But some of these files have different encoding parameters than the rest, so we need to re-encode the files such that their parameters match:
ls -1 part_*.mp4 | \
while read f ; do \
 mencoder $f -o ${f/.mp4/.avi} -oac mp3lame -lameopts mode=2:cbr:br=128 \
 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=1200 \
   -vf scale=720:480 -af volnorm=1 -ffourcc XVID -ofps 29.917; \
where the parameters can be determined by examining the console output of MPlayer during playback of each file and selecting the parameters that match most files, in order to reduce quality loss as much as possible (you may find this script helpful).

Re-encoding is a time consuming process, so unless you absolutely need these files concatenated (e.g. for playback using your DivX DVD player), you may just want to playback the files in order...
mplayer -fs part_*.mp4


  1. Ah! very nice article, I was wondering how to do it on linux and here it is.

    Is there a limit on the avi's sizes?
    Say 2gb ?

  2. I dunno. Largest AVI I generated was a bit over 1GB.

    I did a quick Google search and it seems that there's a 2GB size limit to the AVI format itself.

    And there's also a limit on the file size, depending on the file system that's used to store the file (e.g. FAT32 limit is 4GB).


Note: Only a member of this blog may post a comment.