Joining two (or more) avi files together with mencoder

Have you ever needed to join two avi (or more) files together to create one avi file? Well there is an easy way to merge them together using mencoder. This also assumes that the files are actually the same size, in the same codec etc.

Make sure you are in the same directory as the files you want to join

Use the following command to join the files.
mencoder -forceidx -ovc copy -oac copy -o outputfile.avi firstfile.avi secondfile.avi

Obviously you would replace outputfile.avi, firstfile.avi and secondfile.avi with whatever is appropriate what you are doing.
Mencoder will go off and join the two files. It should do this really quickly as this command is not re-encoding the files, just putting them together. It should look something like this:


$ mencoder -forceidx -ovc copy -oac copy -o new_file.avi 1.avi 2.avi
MEncoder svn r34540 (Ubuntu), built with gcc-4.6 (C) 2000-2012 MPlayer Team
success: format: 0 data: 0x0 - 0x2bcbd408
libavformat version 53.21.0 (external)
Mismatching header version 53.19.0
AVI file format detected.
[aviheader] Video stream found, -vid 0
[aviheader] Audio stream found, -aid 1
Generating Index: 99 %
AVI: Generated index table for 158686 chunks!
VIDEO: [XVID] 624x256 12bpp 23.976 fps 1319.0 kbps (161.0 kbyte/s)
[V] filefmt:3 fourcc:0x44495658 size:624x256 fps:23.976 ftime:=0.0417
videocodec: framecopy (624x256 12bpp fourcc=44495658)
audiocodec: framecopy (format=2000 chans=6 rate=48000 bits=0 B/s=56000 sample-1)
Writing header...
ODML: Aspect information not (yet?) available or unspecified, not writing vprp header.
Writing header...
ODML: Aspect information not (yet?) available or unspecified, not writing vprp header.
Pos:3308.5s 79325f (99%) 4307.40fps Trem: 0min 700mb A-V:0.000 [1318:448]
Incomplete stream? Trying resync.
Pos:3309.3s 79345f (100%) 4301.24fps Trem: 0min 697mb A-V:0.000 [1318:447]
success: format: 0 data: 0x0 - 0x2bb43de0
AVI file format detected.
[aviheader] Video stream found, -vid 0
[aviheader] Audio stream found, -aid 1
Generating Index: 99 %
AVI: Generated index table for 150239 chunks!
VIDEO: [XVID] 624x256 12bpp 23.976 fps 1414.9 kbps (172.7 kbyte/s)
[V] filefmt:3 fourcc:0x44495658 size:624x256 fps:23.976 ftime:=0.0417
videocodec: framecopy (624x256 12bpp fourcc=44495658)
audiocodec: framecopy (format=2000 chans=6 rate=48000 bits=0 B/s=56000 sample-1)
Pos:4587.6s 109994f (46%) 3049.37fps Trem: 0min 2201mb A-V:0.071 [1419:448]]]
ODML: Starting new RIFF chunk at 1023MB.
Pos:6442.5s 154466f (100%) 3326.00fps Trem: 0min 1395mb A-V:0.071 [1365:447]
Writing index...
Writing header...
ODML: Aspect information not (yet?) available or unspecified, not writing vprp header.
Video stream: 1365.588 kbit/s (170698 B/s) size: 1099729277 bytes 6442.526 secs 154466 frames
Audio stream: 448.000 kbit/s (55999 B/s) size: 360772608 bytes 6442.368 secs

If you are wondering what the mencoder options are, here is a basic run down.

-forceidx – this forces the regeneration of the index table for the video. Apparently it’s needed.
-ovc copy – this tells mencoder to copy the video codec exactly as per the input file(s). In other words don’t re-encode it.
-oac copy – this tells mencoder to copy the audio codec exactly as per the input file(s). Again, in other words don’t re-encode it.

Oh, and in case you are wondering if you have more than two files you just add more on to the end of the command. Example:

mencoder -forceidx -ovc copy -oac copy -o outputfile.avi firstfile.avi secondfile.avi thirdfile.avi forthfile.avi

Also, if you don’t actually have mencoder, you can install it by running this command:

sudo apt-get install mencoder

References: http://www.mplayerhq.hu/DOCS/HTML/en/index.html

This entry was posted in Linux, PVR, video and tagged , , , . Bookmark the permalink.

One Response to Joining two (or more) avi files together with mencoder

  1. Book says:

    Hi to all, how is all, I think every one is getting more from this web page,
    and your views are good in support of new users.

Leave a Reply

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