OpenCV 2.0 で AVI ファイルを AVI ファイルに書き出す(第3版)

各種識別子をちょっと変えたくらい.変化量は小さい.

video2video.cpp

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

#ifdef _DEBUG
#pragma comment( lib, "cv200d.lib" )
#pragma comment( lib, "cxcore200d.lib" )
#pragma comment( lib, "highgui200d.lib" )
#else
#pragma comment( lib, "cv200.lib" )
#pragma comment( lib, "cxcore200.lib" )
#pragma comment( lib, "highgui200.lib" )
#endif

//#define PREVIEW // 読み出し画像を表示

int inVideoArgc = 0;
cv::VideoCapture inVideo;
cv::VideoWriter outVideo;
VideoProperty baseProp;

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]);
	}

	inVideoArgc = argc - 2;
	if ( inVideo.open( argv[1] ) == false )
	{
		printf(__FUNCTION__": 入力ファイルオープン失敗.\n");
		return false;
	}
	baseProp.set( inVideo );
	if ( baseProp.isZero() )
	{
		printf(__FUNCTION__": 入力ファイルの幅または高さが 0 です.\n");
		return false;
	}
	outVideo.open( argv[inVideoArgc], fourcc, 
		baseProp.getFps(), cv::Size(baseProp.getWidth(), baseProp.getHeight()) );
	if ( outVideo.isOpened() == false )
	{
		printf(__FUNCTION__": 出力ファイルオープン不能\n");
		return false;
	}

	return true;
}


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

	cv::Mat image;
#ifdef PREVIEW
	cv::namedWindow("preview", CV_WINDOW_AUTOSIZE);
#endif
	int wroteFrames = 0;
	for ( ; 1; wroteFrames++ )
	{
		inVideo >> image;
		if ( image.empty() )
		{
			break;
		}
#ifdef PREVIEW
		cv::imshow( "preview", image);
		cv::waitKey(1);
#endif
		outVideo << image;
	}
	printf("wrote %d frame(s)\n", wroteFrames );


	for ( int n = 2; n < inVideoArgc; n++ )
	{
		inVideo.release();
		inVideo.open( argv[n] );
		VideoProperty prop( inVideo );
		if ( baseProp.compareSize( prop ) != 0 )
		{
			continue;
		}
		for ( ; 1; wroteFrames++ )
		{
			inVideo >> image;
			if ( image.empty() )
			{
				break;
			}
			outVideo << image;
		}
		printf("wrote %d frame(s)\n", wroteFrames );
	}

	getchar();

	return 0;
}

VideoProperty.h

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

class VideoProperty
{
private:
	int count;
	int width;
	int height;
	double fps;
public:
	int getCount(void)  { return this->count;  }
	int getWidth(void)  { return this->width;  }
	int getHeight(void) { return this->height; }
	double getFps(void)    { return this->fps; }
	VideoProperty( void ) { };
	VideoProperty( cv::VideoCapture capture ) { set(capture); };
	void set( cv::VideoCapture capture)
	{
		if ( capture.isOpened() )
		{
			this->count  = (int) capture.get( CV_CAP_PROP_FRAME_COUNT  );
			this->width  = (int) capture.get( CV_CAP_PROP_FRAME_WIDTH  );
			this->height = (int) capture.get( CV_CAP_PROP_FRAME_HEIGHT );
			this->fps    = capture.get( 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( VideoProperty other )
	{
		return this->getHeight() - other.getHeight() + 
			this->getWidth() - other.getWidth();
	}
	bool isZero( void )
	{
		if ( !(this->width) && !(this->height) )
			return true;
		else
			return false;
	}
};