OpenCV VideoWriter不起作用

时间:2014-12-03 15:06:39

标签: c++ opencv video ffmpeg ubuntu-14.04

我尝试使用opencv videoWriter来获取视频文件。但是我遇到了以下问题:

>[libx264 @ 0x132b680] broken ffmpeg default settings detected
>[libx264 @ 0x132b680] use an encoding preset (e.g. -vpre medium)
>[libx264 @ 0x132b680] preset usage: -vpre <speed> -vpre <profile>
>[libx264 @ 0x132b680] speed presets are listed in x264 --help
>[libx264 @ 0x132b680] profile is optional; x264 defaults to high
>Could not open codec 'libx264': Unspecified error!!! Output video could not be opened

我的系统中有libx264,所以我猜这最后一行只是副作用

我试图运行的代码是从How to write video file in OpenCV 2.4.3获取的示例。

int main (int argc, char *argv[]){
// Load input video
VideoCapture input_cap("testi.mp4");
if (!input_cap.isOpened())
{
        std::cout << "!!! Input video could not be opened" << std::endl;
        return -1;
}

// Setup output video
cv::VideoWriter output_cap("testo.mp4", 
               input_cap.get(CV_CAP_PROP_FOURCC),
               input_cap.get(CV_CAP_PROP_FPS),
               cv::Size(input_cap.get(CV_CAP_PROP_FRAME_WIDTH),
               input_cap.get(CV_CAP_PROP_FRAME_HEIGHT)));

if (!output_cap.isOpened())
{
        std::cout << "!!! Output video could not be opened" << std::endl;
        return -1;
}


// Loop to read from input and write to output
cv::Mat frame;

while (true)
{       
    if (!input_cap.read(frame))             
        break;

    output_cap.write(frame);
}

input_cap.release();
output_cap.release();

return 0;
}

我发现了一个类似问题的帖子How to get stream info from opened file in ffmpeg?但是还没有人正确回答 我发现有人告诉人们检查opencv是否使用了旧的fmmpeg而不是libav,这不是因为它是一个新的构建而我的ubuntu没有ffmpeg。

2 个答案:

答案 0 :(得分:2)

VideoWriter不支持.mp4扩展名。请改用.avi

答案 1 :(得分:1)

Dimazavr的回答并不完全正确。首先,您需要将输出视频文件扩展名从.mp4更改为.avi。然后,如果您运行代码,您将收到以下错误信息:

OpenCV Error: Unsupported format or combination of formats (Gstreamer Opencv backend does not support this codec.) in CvVideoWriter_GStreamer::open, file /home/rwduzhao/store/opencv-2.4.13/modules/highgui/src/cap_gstreamer.cpp, line 1372
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/rwduzhao/store/opencv-2.4.13/modules/highgui/src/cap_gstreamer.cpp:1372: error: (-210) Gstreamer Opencv backend does not support this codec. in function CvVideoWriter_GStreamer::open

Aborted (core dumped)

这意味着cv::VideoWriter中的opencv2.4不支持libx264格式或avi扩展名与libx264格式不兼容。我建议不要使用libx264编解码器。您可以尝试CV_FOURCC支持的以下编解码格式列表:

CV_FOURCC('P','I','M','1')    = MPEG-1 codec
CV_FOURCC('M','J','P','G')    = motion-jpeg codec
CV_FOURCC('M', 'P', '4', '2') = MPEG-4.2 codec
CV_FOURCC('D', 'I', 'V', '3') = MPEG-4.3 codec
CV_FOURCC('D', 'I', 'V', 'X') = MPEG-4 codec
CV_FOURCC('U', '2', '6', '3') = H263 codec
CV_FOURCC('I', '2', '6', '3') = H263I codec
CV_FOURCC('F', 'L', 'V', '1') = FLV1 codec

根据我的经验,CV_FOURCC('D', 'I', 'V', 'X')的质量很好。此外,如果将cv_fourcc设置为-1,则可以在GUI窗口中选择系统支持的编解码器格式之一。您可以看到正在运行的进程here