OpenCV将视频保存到文件 [英] OpenCV saving video to file

查看:751
本文介绍了OpenCV将视频保存到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我的问题是相当基本的,但我在OpenCV中编写这段代码,只是从网络摄像头捕获视频数据,并将其保存到文件。现在的问题是,文件在所需的目的地,它最初是大约286字节。然后,当我将第一帧分配给指针时,大小增加到414字节。但是,当我运行整个代码,保存的视频的大小仍然是414字节。当然,因此,我的媒体播放器不能播放该文件,并说不是QuickTime播放器理解的格式。



这里是我的代码:

  #include< opencv2 / opencv.hpp> 
#include< opencv2 / highgui / highgui.hpp>

int main(int argc,char ** argv){
CvCapture * capture;

capture = cvCreateCameraCapture(0);

assert(capture!= NULL);

IplImage * bgr_frame = cvQueryFrame(capture);

CvSize size = cvSize(
(int)cvGetCaptureProperty(capture,
CV_CAP_PROP_FRAME_WIDTH),
(int)cvGetCaptureProperty(capture,
CV_CAP_PROP_FRAME_HEIGHT)
);

cvNamedWindow(Webcam,CV_WINDOW_AUTOSIZE);

CvVideoWriter * writer = cvCreateVideoWriter(/Users/user/Desktop/OpenCV_trial/OpenCV_trial/vidtry.AVI,
CV_FOURCC('D','I','V' X'),
30,
size
);

while((bgr_frame = cvQueryFrame(capture))!= NULL)
{
cvWriteFrame(writer,bgr_frame);
cvShowImage(Webcam,bgr_frame);
char c = cvWaitKey(33);
if(c == 27)break;
}
cvReleaseVideoWriter(& writer);
cvReleaseCapture(& capture);
cvDestroyWindow(Webcam);
return(0);
}

我不知道为什么会发生这种情况。我使用mac OSX Lion并运行Xcode。



之前有人遇到过这个问题吗?

谢谢!



-Yash

解决方案

您尝试使用其他播放器打开文件吗? VLC例如..



这是因为Quicktime和.avi不是很好。



查看苹果文档



否则尝试更改视频编解码器,这是opencv 参考


I think my question is pretty basic, but I'm writing this code in OpenCV to simply capture video data from the webcam and save it to file. Now the problem is that the file gets made at the desired destination, it initially is about 286 bytes in size. Then when I assign the first frame to the pointer, the size increases to 414 bytes. However, when I run the whole code, the size of the saved video remains 414 bytes. Of course, as a result my media player cannot play the file and says " is not in a format that QuickTime Player understands." and the same happens with the VLC player.

Here is my code for the same:

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>

int main( int argc, char** argv ) {
CvCapture* capture;

capture = cvCreateCameraCapture(0);

assert( capture != NULL );

IplImage* bgr_frame = cvQueryFrame( capture );

CvSize size = cvSize(
                     (int)cvGetCaptureProperty( capture,
                                               CV_CAP_PROP_FRAME_WIDTH),
                     (int)cvGetCaptureProperty( capture,
                                               CV_CAP_PROP_FRAME_HEIGHT)
                     );

cvNamedWindow( "Webcam", CV_WINDOW_AUTOSIZE );

CvVideoWriter *writer = cvCreateVideoWriter(    "/Users/user/Desktop/OpenCV_trial/OpenCV_trial/vidtry.AVI",
                                            CV_FOURCC('D','I','V','X'),
                                            30,
                                            size
                                            );

while( (bgr_frame = cvQueryFrame( capture )) != NULL ) 
{
    cvWriteFrame(writer, bgr_frame );
    cvShowImage( "Webcam", bgr_frame );
    char c = cvWaitKey( 33 );
    if( c == 27 ) break;
}
cvReleaseVideoWriter( &writer );
cvReleaseCapture( &capture );
cvDestroyWindow( "Webcam" );
return( 0 );
}

I don't know why this is happening. I am using the mac OSX Lion and running Xcode.

Has anyone faced this problem before? If so, how could I solve it?

Thank you!

-Yash

解决方案

Have you try to open your file with another player? VLC for instance..

This because Quicktime and .avi do not get along very well.

Take a look on apple documentation.

Otherwise try to change the video codec, this is the opencv reference.

这篇关于OpenCV将视频保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆