opencv写入视频文件

来源:互联网 发布:软件测试职位 编辑:程序博客网 时间:2024/05/16 19:49

容易出错的地方:cvCreateVideoWriter

   /* initialize video file writer */
   CVAPI(CvVideoWriter*) cvCreateVideoWriter( const char* filename, int fourcc,double fps, CvSize frame_size, int is_color CV_DEFAULT(1));


1.第二个参数:编解码器

 

CV_FOURCC('P','I','M','1')= MPEG-1 codec 

CV_FOURCC('M','J','P','G')= motion-jpeg codec (does not work well) 

CV_FOURCC('M','P', '4', '2') = MPEG-4.2 codec 

CV_FOURCC('D','I', 'V', '3') = MPEG-4.3 codec 

CV_FOURCC('D','I', 'V', 'X') = MPEG-4 codec 

CV_FOURCC('U','2', '6', '3') = H263 codec 

CV_FOURCC('I','2', '6', '3') = H263I codec 

CV_FOURCC('F','L', 'V', '1') = FLV1 codec 

 

  建议使用-1,由自己去设置,这个一定要注意自己的电脑上有没有这个解码器

 

2.4个参数的设置非常坑,一定要注意要跟你的原图像的大小一致,不一致也不会报错,但生成的

       视频文件可能为0kb(我的一直显示为5.54kb不知道为什么,调了好久才发现是这个问题)

 

3.第一个参数的路径一定要用'/',我也不知道为什么用‘\\’不能读取路径,很诡异。

 

4.所有的文件都要释放,我没试过没释放的后果。编程习惯,调用的内存,肯定要释放。

 

下面是一个我写的一个简易demo,供参考:

#include"stdafx.h"

#include"cv.h"

#include"highgui.h"

 

int _tmain(int argc,_TCHAR* argv[])

{  

CvCapture*capture = 0;

capture = cvCreateFileCapture("E:\\迅雷下载\\EmoMirror2.avi");

IplImage*frame = cvQueryFrame(capture);

CvSizesize = cvSize((int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH),(int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT));

CvVideoWriter*writer =cvCreateVideoWriter("C:/Users/Administrator/Desktop/11/data/11oty.avi",-1, 25, size);

while((frame = cvQueryFrame(capture)) != NULL)

{

cvWriteFrame(writer,frame);

cvNamedWindow("sh");

cvShowImage("sh", frame);

cvWaitKey(25);

}

cvReleaseVideoWriter(&writer);

cvReleaseCapture(&capture);

cvReleaseImage(&frame);

cvDestroyWindow("sh");

return0;

}

原创粉丝点击