Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Ubuntu OpenCV VideoWriter using ffmpeg with “Could not open codec 'libx264'” Error

I am new to OpenCV, and I want write Mat images into video using VideoWriter on Ubuntu 12.04. But when constructing VideoWriter, errors came out.

It seems that OpenCV invoke ffmpeg API using default parameters and ffmpeg invoke x264 using its default parameters. Then these setting is broken for libx264. Thus the "Could not open codec 'libx264'" error.

Anyone has ideas to solve this problem?

More specifically:

  1. anyone knows where and how OpenCV invoke ffmpeg API?
  2. how to change ffmpeg default settings using code, hopefull, can be easily embeded into OpenCV?
  3. will changes of default in ffmpeg be carried to libx264?

Errors:

1. Uising CV_FOURCC('H', '2', '6', '4')
[libx264 @ 0x255de40] broken ffmpeg default settings detected
[libx264 @ 0x255de40] use an encoding preset (e.g. -vpre medium)
[libx264 @ 0x255de40] preset usage: -vpre <speed> -vpre <profile>
[libx264 @ 0x255de40] speed presets are listed in x264 --help
[libx264 @ 0x255de40] profile is optional; x264 defaults to high
Could not open codec 'libx264': Unspecified error

2. Using FOURCC = -1 to invoke user customized codec
OpenCV Error: Unsupported format or combination of formats (Gstreamer Opencv backend doesn't support this codec acutally.) in CvVideoWriter_GStreamer::open, file /home/XXX/Downloads/opencv-2.4.8/modules/highgui/src/cap_gstreamer.cpp, line 505
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/XXX/Downloads/opencv-2.4.8/modules/highgui/src/cap_gstreamer.cpp:505: error: (-210) Gstreamer Opencv backend doesn't support this codec acutally. in function CvVideoWriter_GStreamer::open

Codes:

int main(int argc, char *argv[])
{
    VideoWriter outputVideo;
    bool fourcc_on = true; //switch on / off different error
    if (fourcc_on)
        outputVideo.open("outVideo.avi", CV_FOURCC('H', '2', '6', '4'), 25, Size(100, 100), true);
    else
        outputVideo.open("outVideo.avi", -1, 25, Size(100, 100), true);

    if (!outputVideo.isOpened())
    {
        cout  << "Could not open the output video for write" << endl;
        return -1;
    }
    return 0;
}

OpenCV Configuration:

enter image description here

FFMPEG

ffmpeg is enable to support OpenCV and libx264 is enabled when compiling ffmpeg. By using ffmpeg command line, libx264 is running normally.

ffmpeg -i test.avi -vcodec libx264 test.mp4 enter image description here

Ubuntu OpenCV VideoWriter using ffmpeg with “Could not open codec 'libx264'” Error

I am new to OpenCV, and I want write Mat images into video using VideoWriter on Ubuntu 12.04. But when constructing VideoWriter, errors came out.

It seems that OpenCV invoke ffmpeg API using default parameters and ffmpeg invoke x264 using its default parameters. Then these setting is broken for libx264. Thus the "Could not open codec 'libx264'" error.

Anyone has ideas to solve this problem?

More specifically:

  1. anyone knows where and how OpenCV invoke ffmpeg API?
  2. how to change ffmpeg default settings using code, hopefull, can be easily embeded into OpenCV?
  3. will changes of default in ffmpeg be carried to libx264?

Errors:

1. Uising CV_FOURCC('H', '2', '6', '4')
[libx264 @ 0x255de40] broken ffmpeg default settings detected
[libx264 @ 0x255de40] use an encoding preset (e.g. -vpre medium)
[libx264 @ 0x255de40] preset usage: -vpre <speed> -vpre <profile>
[libx264 @ 0x255de40] speed presets are listed in x264 --help
[libx264 @ 0x255de40] profile is optional; x264 defaults to high
Could not open codec 'libx264': Unspecified error

2. Using FOURCC = -1 to invoke user customized codec
OpenCV Error: Unsupported format or combination of formats (Gstreamer Opencv  backend doesn't support this codec acutally.) in CvVideoWriter_GStreamer::open, file file
/home/XXX/Downloads/opencv-2.4.8/modules/highgui/src/cap_gstreamer.cpp, line 505
terminate called after throwing an instance of 'cv::Exception'
 what():  /home/XXX/Downloads/opencv-2.4.8/modules/highgui/src/cap_gstreamer.cpp:505:  error: (-210) Gstreamer Opencv backend doesn't support this codec acutally. in function function
CvVideoWriter_GStreamer::open

Codes:

int main(int argc, char *argv[])
{
    VideoWriter outputVideo;
    bool fourcc_on = true; //switch on / off different error
    if (fourcc_on)
        outputVideo.open("outVideo.avi", CV_FOURCC('H', '2', '6', '4'), 25, Size(100, 100), true);
    else
        outputVideo.open("outVideo.avi", -1, 25, Size(100, 100), true);

    if (!outputVideo.isOpened())
    {
        cout  << "Could not open the output video for write" << endl;
        return -1;
    }
    return 0;
}

OpenCV Configuration:

enter image description here

FFMPEG

ffmpeg is enable to support OpenCV and libx264 is enabled when compiling ffmpeg. By using ffmpeg command line, libx264 is running normally.

ffmpeg -i test.avi -vcodec libx264 test.mp4 enter image description here