简体   繁体   English

OpenCV将视频保存到文件

[英]OpenCV saving video to file

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. 我认为我的问题非常基本,但我在OpenCV中编写此代码,只是从网络摄像头捕获视频数据并将其保存到文件中。 Now the problem is that the file gets made at the desired destination, it initially is about 286 bytes in size. 现在的问题是文件是在所需的目的地进行的,它最初的大小约为286字节。 Then when I assign the first frame to the pointer, the size increases to 414 bytes. 然后,当我将第一帧分配给指针时,大小增加到414字节。 However, when I run the whole code, the size of the saved video remains 414 bytes. 但是,当我运行整个代码时,保存的视频的大小仍然是414字节。 Of course, as a result my media player cannot play the file and says " is not in a format that QuickTime Player understands." 当然,因此我的媒体播放器无法播放该文件并说“不是QuickTime Player理解的格式”。 and the same happens with the VLC player. VLC播放器也是如此。

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. 我正在使用mac OSX Lion并运行Xcode。

Has anyone faced this problem before? 以前有人遇到过这个问题吗? If so, how could I solve it? 如果是这样,我该如何解决?

Thank you! 谢谢!

-Yash -Yash

Hi I think I found the answer to the question. 嗨,我想我找到了问题的答案。

As Velthune had suggested, it seems to be a codec issue and the MAC OS can run only a few of them. 正如Velthune所建议的那样,它似乎是一个编解码器问题,MAC OS只能运行其中的几个。 Here is the link for all the ones that work: List of QuickTime codecs supported by the mac os port 以下是所有工作链接的链接: mac os端口支持的QuickTime编解码器列表

Not all of the codecs listed there work though. 并非所有列出的编解码器都可以工作。 Out of all the ones that I tried only the WRLE seemed to work. 在我尝试的所有那些只有WRLE似乎工作。

Thanks a lot Velthune. 非常感谢Velthune。 -Yash -Yash

Have you try to open your file with another player? 您是否尝试使用其他播放器打开文件? VLC for instance.. 例如VLC ..

This because Quicktime and .avi do not get along very well. 这是因为Quicktime和.avi不能相处得很好。

Take a look on apple documentation . 看看苹果文档

Otherwise try to change the video codec, this is the opencv reference . 否则尝试更改视频编解码器,这是opencv 参考

Actually I too was trying to do same. 实际上我也试图这样做。 But its that i tried in Visual C++ (Express Edition) in windows 7. But in this case we need to add extra header as "#include "stdafx.h" and also make sure the link to save the file exists. For example the code i modified was as: 但是我在Windows 7中尝试了Visual C ++(Express Edition)。但在这种情况下,我们需要添加额外的标题为“#include”stdafx.h“并确保存在保存文件的链接。例如我修改的代码如下:

#include "stdafx.h"
#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("D:/vidtry.AVI",CV_FOURCC('D','I','V','X'),15,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 );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM