如何使用OpenCV编写视频文件? [英] How to write a video file with OpenCV?

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

问题描述

我正在尝试编写一个程序来分析诸如眼泪之类的情感表达.作为跟踪器的一部分,我正在使用OpenCV录制示例视频.特别是,我不确定如何正确选择FPS(10FPS似乎应该可以使用).我也不确定 我应该在OS X上使用的编解码器,我也在此处中尝试了所有可能的CV_FOURCC但返回以下错误:

I am trying to write a program to analyze emotional expressions like tears. As part of my tracker I am using OpenCV to record sample videos. Particularly, I am not certain about how to correctly choose FPS (10FPS seems like it ought to work). I am also not sure which Codec I should use on OS X, I have tried all possible CV_FOURCC from here as well but returned the following error:

Stream #0.0: Video: rawvideo, yuv420p, 640x480, q=2-31, 19660 kb/s, 90k tbn, 10 tbc
Assertion failed: (image->imageSize == avpicture_get_size( (PixelFormat)input_pix_fmt, image->width, image->height )), function writeFrame, file /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_ports_graphics_opencv/work/OpenCV-2.2.0/modules/highgui/src/cap_ffmpeg.cpp, line 1085.

你们都对cvWriteFrame有一些有效的代码吗?感谢您抽出宝贵时间来查看我的问题!

Do you all have some working code with cvWriteFrame? Thanks for taking time to look at my problem!

对于那些感兴趣的人,整个程序是:

For those interested the entire program is:

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

int main (int argc, const char * argv[]) 
{
    CvCapture *capture;
    IplImage  *img;
    int       key = 0;
    CvVideoWriter *writer;

    // initialize camera
    capture = cvCaptureFromCAM( 0 );
    // capture = cvCaptureFromAVI("AVIFile");

    // always check
    assert( capture );

    // create a window
    cvNamedWindow( "video", 1 );

    int color = 1; // 0 for black and white

    // get the frame size
    CvSize size = cvSize((int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH),(int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT));

    writer = cvCreateVideoWriter(argv[1], -1 , 10 , size, color);

    while( key != 'q' ) {
        // get a frame
        img = cvQueryFrame( capture );

        // always check
        if( !img ) break;

        cvWriteFrame( writer, img );        
        cvShowImage("video", img );

        // quit if user press 'q'
        key = cvWaitKey( 5 );
    }

    // free memory
    cvReleaseVideoWriter( &writer );
    cvReleaseCapture( &capture );
    cvDestroyWindow( "video" );

    return 0;   
}

推荐答案

此处中查找推荐的编解码器使用.

Have a look here for the recommended codecs to use.

基本上:

  • 仅当您使用Windows时,才可以使用-1代替CV_FOURCC(...) (请参阅
  • You can use -1 instead of CV_FOURCC(...) only if you're on Windows (see the manual). Since you're on OSX, you can't use it.
  • Use CV_FOURCC('I', 'Y', 'U', 'V') to encode using yuv420p into an uncompressed AVI container -- this is the recommended approach
  • If you want compressed video, then you can use tools like ffmpeg to compress the AVI output by OpenCV

OpenCV不建议使用压缩编解码器的原因可能是因为每个编解码器都有大量特定于编解码器的选项,并且不可能通过简单的OpenCV视频编写器界面处理所有这些选项.

The reason OpenCV doesn't recommend using compressor codecs is probably because each codec has a large number of codec-specific options, and dealing with all of them through the simple OpenCV video writer interface would be impossible.

编辑

我有一些坏消息:

不幸的是,我们仍然没有适用于Mac OS X的(即本机)视频编写器.如果您需要进行视频编写,则当前应配置ffmpeg或xine并禁用QuickTime.

Unfortunately, we still have no working (i.e. native) video writer for Mac OS X. If you need video writing, you should currently configure for ffmpeg or xine and disable QuickTime.

听起来像您可能需要重新配置和重新编译您的库.

Sounds like you may need to reconfigure and recompile your library.

编辑2

或者,您可以仅将每个帧写入JPEG文件,然后使用ffmpeg(在OS/X上可以使用)将这些JPEG文件缝合到电影中.无论如何,这本质上就是MJPEG.不过,利用时间冗余(例如MPEG4,H.264等)的编解码器可能会获得更好的压缩率.

Alternatively, you could just write each frame to a JPEG file, and then stitch those JPEG files into a movie using ffmpeg (that should work on OS/X). This is essentially what MJPEG is, anyway. You'll probably get better compression rates with codecs that exploit temporal redundancy (e.g. MPEG4, H.264, etc), though.

这篇关于如何使用OpenCV编写视频文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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