xcode OpenCV 将视频保存到文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13698772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:14:27  来源:igfitidea点击:

OpenCV saving video to file

c++xcodemacosopencvcomputer-vision

提问by yashc

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.

我认为我的问题非常基本,但我正在 OpenCV 中编写此代码以简单地从网络摄像头捕获视频数据并将其保存到文件。现在的问题是文件是在所需的目的地制作的,它最初的大小约为 286 字节。然后当我将第一帧分配给指针时,大小增加到 414 字节。但是,当我运行整个代码时,保存的视频的大小仍然是 414 字节。当然,结果是我的媒体播放器无法播放该文件并显示“不是 QuickTime 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

-亚什

采纳答案by Luca Davanzo

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参考

回答by yashc

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. Here is the link for all the ones that work: List of QuickTime codecs supported by the mac os port

正如 Velthune 所建议的,这似乎是一个编解码器问题,MAC OS 只能运行其中的几个。这是所有可用的链接: 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. -Yash

非常感谢 Velthune。-亚什

回答by Gopal Chandra Nepal

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++(速成版)中尝试过的。但在这种情况下,我们需要添加额外的标题作为“#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 );
}