*!!!* This tutorial is a work in progress *!!!*
In dealing with digital video, concatenating or joining generally refers to taking multiple files and placing them together sequentially in a single container (e.g. .avi, .mp4, .flv, etc.) without re-encoding. It is preferable to use this method when re-encoding is not necessary as it is much faster and there is no quality/data loss.
Two of the most common issues that I have faced with joining video is audio sync and frame corruption problems. In this tutorial I will demonstrate some of the different free/open source software methods that I prefer to use when joining various video types. Each video type is different and not every method works well for them.
Important Note: All files being joined must have been encoded using the same method. Because the video is not being re-encoded the result will be corrupted or joining will fail altogether.
Some things that will cause video incompatibility:
Scan Type: Progressive, top frame first interlacement, and bottom frame first interlacement are not compatible with each other and will guarantee frame corruption.
Framerate: The speed at which frames are displayed during playback must match. It may be possible to change the framerate of all files without re-encoding with some programs.
Resolution: All video files must have the same width and height.
Aspect Ratio: Most programs will use the aspect ratio of the initial video unless otherwise specified. If the trailing videos do not use the same it will likely result in them looking “stretched” or “squeezed”.
Bitrate: Videos using different bitrates may end up corrupted.
Pixel Format / Color Space: This one has been a real pain for me. I didn’t realize until recently just how many different color spaces are available for video encoding. Some codecs support many different color spaces while others only a few or maybe even one. A few common color spaces are RGB, YUV4:4:4, YUV4:2:2, and YUV4:2:0.
Audio Format and Encoding: The audio will be a factor as well. Codec, bitrate, frequency, and channel count should all be taken into account.
GUI (Graphical User Interface) Methods:
Avidemux:
http://avidemux.sourceforge.net/
Avidemux is a versatile in-line video editor and its primary functions are joining, muxing, and encoding. It supports almost any video and audio type (not perfectly unfortunately) and is built on top of the FFmpeg libraries. It is free open source software and released under the GNU General Public License (GPL).
It does a good job of joining MPEG-2 videos as long as indexing is enabled. Older versions have an option in the preferences menu that allow it to be disabled while newer versions seem to have omitted it, making indexing mandatory.
Open the first .mpg in Avidemux. It will begin indexing and create an .idx2 file in the same directory.
Next go to the menu bar and select “File” -> “Append”. This will append another file at the end of the previous. If the two MPEGs are not compatible with each other the appending will mostly likely fail. Older versions had an option to automatically append all filenames that followed a numbered sequence (e.g. video001.mpg, video002.mpg, video003.mpg, …) and was located in the same directory. However, this often cause audio sync issues on my system. So my recommendation is to add each file independently, though it is more tedious. If you chose to append the files all at once but want to go back and redo it you will need to delete or rename the .idx/.idx2 file that was created first.
After you have added all the files that you want go to the left side of the application window and make sure that under both Video Output and Audio Output “Copy” is selected. Otherwise the video and/or audio will be re-encoded. Finally, at the bottom left corner, choose the container that you would like to use. If you are creating an MPEG-2 file for use on a DVD you will probably want to use the MPEG PS format. The Matroska (.mkv) container should hold MPEG-2 data without any problems as well.
Finally press the save button (or go to “File” -> “Save”). A dialog will pop up and ask you where you want to save your video. Be sure to put the extension at the end of the filename (.mpg/.mpeg/.m2v) as Avidemux will not do this automatically. Then press “Save”.
Note: Avidemux has a setting that splits MPEG files when they reach a certain size. This is problematic if your goal is a single video file. To change the setting go to the menu bar and select “Edit” -> “Preferences”. Under the “Output” tab go to the option called “Split MPEG files every (MB):” and raise the value above the size of your output video then press “OK”.
Works well for:
- MPEG-2 / VOB / DVD
Available for:
- Apple OS X
- Microsoft Windows
- Unix-like
MKVToolNix (MKVMerge GUI):
http://www.bunkus.org/videotools/mkvtoolnix/
** No instructions yet.
Works well for:
- MPEG-4 Part 10 / AVC / BluRay
Available for:
- Apple OS X
- Microsoft Windows
- Unix-like
VirtualDub:
** No instructions yet.
Works well for:
Available for:
- Microsoft Windows
CLI (Command Line Interface) Methods:
Cat Command:
http://www.gnu.org/software/coreutils/
The cat command (also available for Windows) is supposed to work similarly to the Windows copy command. In my experience it doesn’t work as well. To use it invoke “cat” followed by a list of input files. By default cat outputs to standard-out. So to make it output to a file add a “>” followed by the name of the output video:
cat <in video 1> <in video 2> … > <out video>
Works well for:
Available for:
- Apple OS X
- Microsoft Windows
- Unix-like
Copy Command:
This method seems to work flawlessly for MPEG-2 but does not work at all for H.264/MP4. To concatenate files with the copy command the binary switch /b must be added. Next add the name of the output file followed by a list of input files separated by a “+”:
copy /b out_vid.mpg in_vid1.mpg + in_vid2.mpg + in_vid3.mpg ...
Works well for:
- MPEG-2 / VOB / DVD
Available for:
- Microsoft Windows
MKVToolNix (mkvmerge):
http://www.bunkus.org/videotools/mkvtoolnix/
The following is a Windows Batch file that will concatenate all the files in the current working directory. Copy and paste it into a text document. Save it with the .bat file extension (I have it named as concatvids.bat) and place it in a directory on the system PATH (C:\Windows). Then open a command prompt and change to the directory where your videos are located. Type the filename that you gave the batch file followed by the desired name for your new video: concatvids “../video_out” (file extension not needed).
@echo off setlocal EnableDelayedExpansion if "%~1" == "" ( echo Not enough arguments. exit /b 1 ) set filename=%~1.mkv set filelist= for %%a in (*) do ( set filelist=!filelist! "%%~a" + ) mkvmerge -o "%filename%" %filelist%
On Unix-like systems (and OS X I think) this is even simpler. Open a terminal, navigate to the directory containing your videos and execute the following command:
mkvmerge -o "../video_out.mkv" $( for x in $(ls); do echo "$x + "; done )
This is a bash/sh script that will do the same as the above batch file:
#! /bin/bash if [ "$#" -lt "1" ] then echo Not enough arguments. exit 1 fi filename=${1}.mkv mkvmerge -o ${filename} $( for x in $(ls); do echo "$x + "; done )
Works well for:
- MPEG-4 Part 10 / AVC / BluRay
Available for:
- Apple OS X
- Microsoft Windows
- Unix-like
MPEG-4 Part 2 / DivX / Xvid:
No info yet.