OpenCV 1.1 で映像ファイルを映像ファイルに出力

OpenCV 1.1 (2008年11月28日 CVS)で video2video.OpenCV 2.0 版は OpenCV 2.0 で AVI ファイルを AVI ファイルに書き出す(第2版) - BiBoLoG

video2video.cpp

#include <opencv/cv.h>
#include <opencv/highgui.h>
#include "video_property.h"

#pragma comment( lib, "cv.lib" )
#pragma comment( lib, "cxcore.lib" )
#pragma comment( lib, "highgui.lib" )

int in_video_argc = 0;
CvCapture *in_video;
CvVideoWriter *out_video;
video_property base_prop;

void printusage( void )
{
	printf("\n");
	printf("1個以上の映像ファイルを1個の映像ファイルに出力します.\n");
	printf("\n");
	printf("video2video.exe 入力ファイル1 [入力ファイル2 ...] 出力ファイル FOURCC \n");
	printf("\n");
}

bool checkargs( int argc, char *argv[] )
{
	// パラメータ数
	if ( argc < 4 )
	{
		printf(__FUNCTION__": パラメータが少ないです.\n");
		return false;
	}
	// fourcc
	int fourcc = CV_FOURCC_PROMPT;
	if ( strlen(argv[argc-1]) == 4 )
	{
		fourcc = CV_FOURCC(argv[argc-1][0], argv[argc-1][1], argv[argc-1][2], argv[argc-1][3]);
	}

	in_video_argc = argc - 2;
	in_video = cvCaptureFromAVI( argv[1] );
	if ( in_video == NULL )
	{
		printf(__FUNCTION__": 入力ファイルオープン失敗.\n");
		return false;
	}
	base_prop.set( in_video );
	if ( base_prop.isZero() )
	{
		printf(__FUNCTION__": 入力ファイルの幅または高さが 0 です.\n");
		return false;
	}
	out_video = cvCreateVideoWriter( argv[in_video_argc], fourcc, 
		base_prop.get_fps(), cvSize(base_prop.get_width(), base_prop.get_height()) );
	if ( out_video == NULL )
	{
		printf(__FUNCTION__": 出力ファイルオープン不能\n");
		return false;
	}

	return true;
}


int main(int argc, char *argv[])
{
	// パラメータチェック
	if ( checkargs( argc, argv ) == false )
	{
		printf("%s(%d): パラメータ不正\n", __FILE__, __LINE__);
		if ( in_video )
		{
			cvReleaseCapture( &in_video );
		}
		return 0;
	}

	int i;
	IplImage *image;
	for ( i = 0; image = cvQueryFrame( in_video ); i++ )
	{
		cvWriteFrame( out_video, image );
	}
	printf("wrote %d frame(s)\n", i );

	for ( int n = 2; n < in_video_argc; n++ )
	{
		cvReleaseCapture( &in_video );
		in_video = cvCaptureFromAVI( argv[n] );
		video_property prop( in_video );
		if ( base_prop.compareSize( prop ) != 0 )
		{
			continue;
		}
		for ( i = 0; image = cvQueryFrame( in_video ); i++ )
		{
			cvWriteFrame( out_video, image );
		}
		printf("wrote %d frame(s)\n", i );
	}

	if ( in_video != NULL )
	{
		cvReleaseCapture( &in_video );
	}
	if ( out_video != NULL )
	{
		cvReleaseVideoWriter( &out_video );
	}

	return 0;
}

video_property.h

#ifndef _HIGH_GUI_
#include <highgui.h>
#endif

class video_property
{
private:
	int count;
	int width;
	int height;
	double fps;
public:
	int get_count(void)  { return this->count;  }
	int get_width(void)  { return this->width;  }
	int get_height(void) { return this->height; }
	double get_fps(void)    { return this->fps; }
	video_property( void ) { };
	video_property( CvCapture *capture ) { set(capture); };
	void set( CvCapture *capture)
	{
		if ( capture )
		{
			this->count  = (int) cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_COUNT  );
			this->width  = (int) cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH  );
			this->height = (int) cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT );
			this->fps    = cvGetCaptureProperty( capture, CV_CAP_PROP_FPS );
		}else{
			this->count = this->width = this->height = 0;
			this->fps = 0.0;
		}
	}
	void print(void)
	{
		printf("%-26s: %d\n",  "CV_CAP_PROP_FRAME_COUNT",  this->count );
		printf("%-26s: %d\n",  "CV_CAP_PROP_FRAME_WIDTH",  this->width );
		printf("%-26s: %d\n",  "CV_CAP_PROP_FRAME_HEIGHT", this->height);
		printf("%-26s: %lf\n", "CV_CAP_PROP_FPS",          this->fps   );
	}
	int compareSize( video_property other )
	{
		return this->get_height() - other.get_height() + 
			this->get_width() - other.get_width();
	}
	bool isZero( void )
	{
		if ( !(this->width) && !(this->height) )
			return true;
		else
			return false;
	}
};