0

我是 OpenCV 的新手,试图用 ffmpeg 录制简单的视频,但大小每次都是 0kb ......我用 Cinepak 编解码器取得了成功,但视频录制速度很快,或者我将 fps 设置为 4 和 cwWaitKey(20) .. 这里是我正在尝试的代码..

    cvNamedWindow("Webcam", CV_WINDOW_AUTOSIZE);
    CvCapture *cap; cap = cvCreateCameraCapture(0);
    IplImage *img; img = cvQueryFrame(cap);
    CvVideoWriter *writer;
    double fps = cvGetCaptureProperty(cap, CV_CAP_PROP_FPS);
    int camWidth = (int)cvGetCaptureProperty(cap, CV_CAP_PROP_FRAME_WIDTH);
    int camHeight = (int)cvGetCaptureProperty(cap, CV_CAP_PROP_FRAME_HEIGHT);
    CvSize size = cvSize(camWidth, camHeight);
    writer = cvCreateVideoWriter("record.avi", CV_FOURCC('M','J','P','G'), fps, size);

    while(cvQueryFrame(cap))
    {
        img = cvRetrieveFrame(cap);
        cvWriteFrame(writer, img);
        cvShowImage("Webcam", img);
        cvWaitKey(20);
    }

//在这里释放东西。

有人可以告诉我我做错了什么吗?谢谢你。

PS 我也尝试了其他 FOURCC,但结果仍然相同,文件为 0 kbytes,没有写入任何内容.. 帮助 :( 尝试了 FOURCC:XVID、MP4V、FVFW、MPEG、MPG1/2、WM1/2。

PPS 使用 Windows 7 x64,安装了 ffmpeg x86 和 x64 版本。

4

2 回答 2

0

It's been a while for me (did something like that back at University 4 years ago), but from what I remember is the fact that not every codec available on Windows includes an encoder as well as a decoder. Most of them might be decoder only. As such some FOURCCs will work while others produce errors or simply won't work at all.

FFMPEG provides several codecs, but I think they're not available from within OpenCV: I think they're provided as DirectShow filters, while OpenCV uses the classic Video for Windows interface (I might be wrong here, so feel free to correct me on this).

The timing issue you experience is easy to explain: If you set the video's fps to 4 you'll have to ensure to only take and paste 4 images per second. Otherwise you're essentially creating a slow motion or time lapse video. There are more elegant ways, but using basic OpenCV functions this is probably the easiest solution already.

于 2012-12-11T00:21:03.410 回答
0

已修复.. 我刚刚下载了 DIVX 编解码器,安装并使用了 DIVX Fourcc,就像一个魅力。但是我仍然在计算帧数时遇到问题,呵呵:-) 10 fps 将是 100 帧,但我以 25 fps 失败.. 25 * 10 不起作用。

于 2012-12-11T10:58:31.920 回答