2011-11-14 84 views
0

我想使用OpenCV(C++)编写视频文件。看看这些文档,它看起来非常简单。但是,在我的程序(在Windows 7上运行)中尝试它时,我无法做到这一点。无法使用OpenCV录制视频,但未发出错误

在互联网上搜索潜在问题时,我找不到使用2.x OpenCV界面或与我的问题有关的示例。

首先这里是代码:

cv::VideoCapture cap("C:\\Users\\Me\\Video\\test.mov"); 

cv::VideoWriter writer("C:\\Users\\Me\\Video\\test_result.mov", 
          cap.get(CV_CAP_PROP_FOURCC), 
          cap.get(CV_CAP_PROP_FPS), 
          cv::Size((int)cap.get(CV_CAP_PROP_FRAME_HEIGHT), (int)cap.get(CV_CAP_PROP_FRAME_WIDTH))); 

while(cap.grab()) { 
    cv::Mat img; 
    cap.retrieve(img); 

    // process img 

    writer << img; 
} 

没有出现错误信息,但不创建视频文件。更糟糕的是,控制台上会出现以下消息:

Output #0, mov, to 'C:\Users\Me\Video\teste_result.mov': 
Stream #0.0: Video: [0][0][0][0]/0x0000, yuv420p, 480x720, q=2-31, 22118 kb/s, 90k tbn, 15 tbc 

是不是应该创建视频文件?我还可以在哪里搜索可能的错误?

编辑:

当使用上面的代码中,我还检查VideoCapture和VideoWriter与isOpened方法,该方法去确定对象。

回答

1

您是否尝试过手动配置FOURCC,FPS和帧大小字段?

事情是这样的:

cv::VideoWriter writer("C:\\Users\\Me\\Video\\test_result.mov", 
         CV_FOURCC('M','J','P','G'), 
         30, 
         Size(720, 480), 
         true); 

这是否输出什么?有时get(CV_*)不返回有效数据。您可能会想要验证它们是否返回了正确的信息。

编辑:此外,我只是注意到你正在创建(高度,宽度)元组的大小对象。您应该将其替换为(宽度,高度),因为它是CvSize构造函数的预期订单。

0

没有出现错误信息,你也是检查VideoCapture成功和VideoWriter

cv::VideoCapture cap("C:\\Users\\Me\\Video\\test.mov"); 
if (!cap.isOpened()) // check if we succeeded 
{ 
    // print error message 
} 


cv::VideoWriter writer("C:\\Users\\Me\\Video\\test_result.mov", 
          cap.get(CV_CAP_PROP_FOURCC), 
          cap.get(CV_CAP_PROP_FPS), 
          cv::Size((int)cap.get(CV_CAP_PROP_FRAME_HEIGHT), (int)cap.get(CV_CAP_PROP_FRAME_WIDTH))); 

if (!writer.isOpened()) // check if we succeeded 
{ 
    // print error message 
} 

做这些检查,你就会有更多的信息,什么是失败,为什么。

+0

对不起,忘了写它,但我做了我的代码。刚刚编辑了这个问题。 – Renan