How to combine multiple videos together:

Posted on 31st May, 2010 by Shervin Emami

Sometimes, you want to combine 2 video files into 1, either for general video editing, or to compare two videos side-by-side, or to play one video and then another video when it has finished. While you can use professional video editing software to do these and many more functions, it is nice to be able to do this easily, or from a command-line script, or from within your own programs. Therefore I have created two open-source (freeware) programs that will combine videos, either next to one another, or after one another. Note that these programs can not process audio, so you will loose the soundtrack when you use these.

"CombineVids" will put the videos next to each other at the same time (also called Picture-In-Picture).


"AppendVids" will attach the second video onto the end of the first video.

 

How to combine 2 videos next to each other (PiP):

Here is a sample OpenCV program that puts 2 videos next to each other, so you can see both of them playing at the same time. The inputs may be AVI videos or MPG/MPEG videos.

The command line syntax is:
    CombineVids <input_video1> <input_video2> [output_video] [--VERTICAL] [--FPS <fps>]

The source-code to my program is available here (opensource freeware), to use as you wish for non-military projects only.

Click here to download "CombineVids": combineVids.7z

(19kB 7-Zip file, including C/C++ source code, VS2005 project files and the compiled Win32 program, created 27th Apr 2010).

If you dont have the freeware 7-Zip program (better than WinZip and WinRar in my opinion), you can download it at http://www.7-zip.org/download.html.

And if you dont have the OpenCV 2.0 SDK then you can just get the Win32 DLLs for running this program here: openCV200_Win32DLLs.7z (1.3MB 7-Zip file).

 

How the videos are combined next to each other:

The basic method of rendering both videos next to each other is shown here:

// Open the input video files or camera stream.
CvCapture* capture1 = cvCaptureFromAVI( argv[1] );
CvCapture* capture2 = cvCaptureFromAVI( argv[2] );
// Create a new video file for output.
CvSize combinedSize = [Big enough to fit both video frames next to each other, plus a border].
CvVideoWriter* videoWriter = cvCreateVideoWriter(outputFilename, CV_FOURCC('D','I','V','3'), FPS, combinedSize, TRUE);
IplImage *frameOut = cvCreateImage(combinedSize, 8, 3);		// Create an empty RGB image for storing the combined frame.
IplImage *frame1, *frame2;

// Process both video streams while atleast one is still running (AVI or MPG file or camera stream).
frame1 = (IplImage*)1;	// Enter the loop.
frame2 = (IplImage*)1;	// Enter the loop.
while (frame1 || frame2) {
	// Get the next video frames.
	frame1 = cvQueryFrame( capture1 );
	frame2 = cvQueryFrame( capture2 );
	if (frame1 || frame2) {
		// Combine the 2 image frames into 1 big frame.	
		frameOut = combineImages(frame1, frame2);
		// Store the combined video frame into the new video file.
		cvWriteFrame(videoWriter, frameOut);
	}
}

 

Append a video on the end of another video:

Here is a sample OpenCV program that attaches one video onto the end of another video, so you can watch one video after the first one has finished. The inputs may be AVI files, MPG files, folder of JPG images, or even a camera stream.

The command line syntax is:
    AppendVids <input_video1>|<image_folder1> [<input_video2>] [output_video] [--FPS <fps>]

Note that if you are using image folder mode, the syntax is in the printf() format. For example, "AppendVids frames1\img%03d.jpg frames2\img%d.jpg out.mpg --FPS 25" will generate the video "out.mpg" from the files "frames1\img001.jpg", "frames1\img002.jpg", ..., and "frames2\img1.jpg", "frames2\img2.jpg", ...

The source-code to my program is available here (opensource freeware), to use as you wish for non-military projects only.

Click here to download "AppendVids": appendVids.7z

(19kB 7-Zip file, including C/C++ source code, VS2005 project files and the compiled Win32 program, created 27th Apr 2010).

If you dont have the freeware 7-Zip program (better that WinZip and WinRar in my opinion), you can download it at http://www.7-zip.org/download.html.

And if you dont have the OpenCV 2.0 SDK then you can just get the DLLs for running this program here: openCV200_Win32DLLs.7z.

 

How the videos are joined one after the other:

The basic method of using the first video and then using the second video is shown here:

// Create a new video file for output.
CvVideoWriter* videoWriter = cvCreateVideoWriter(outputFilename, CV_FOURCC('D','I','V','3'), FPS, size, TRUE);
// Open the input video files or camera stream.
CvCapture* capture1 = cvCaptureFromAVI( argv[1] );
CvCapture* capture2 = cvCaptureFromAVI( argv[2] );
IplImage *frame;

// Process the first video stream (AVI or MPG file or camera stream).
frame = (IplImage*)1;	// Enter the loop.
while (frame) {
	// Get the next video frame.
	frame = cvQueryFrame( capture1 );
	// Store the video frame into the new video file.
	if (frame) {
		cvWriteFrame(videoWriter, frame);
	}
}

// Process the first video stream (AVI or MPG file or camera stream).
frame = (IplImage*)1;	// Enter the loop.
while (frame) {
	// Get the next video frame.
	frame = cvQueryFrame( capture2 );
	// Store the video frame into the new video file.
	if (frame) {
		cvWriteFrame(videoWriter, frame);
	}
}