Ask Your Question

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

asked 2014-04-24 21:38:17 -0600

hackcv gravatar image

updated 2014-04-24 21:42:11 -0600

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?


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


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

edit retag flag offensive close merge delete


Stupid comment probably, but did you use openCV 2.4.8? There have been a lot of bugfixes lately on videoWriter class. Try grabbing the latest 2.4 branch on GitHub to see if it has been fixed already.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-04-25 03:37:31 -0600 )edit

I downloaded OpenCV2.4.8 from the website, and it redirected me to the Would it be any problem?

hackcv gravatar imagehackcv ( 2014-04-26 01:43:11 -0600 )edit

Nope, but the 2.4.9 release got agan 100+ bugs solved.Try that one first?

StevenPuttemans gravatar imageStevenPuttemans ( 2014-04-26 09:06:22 -0600 )edit

I am seeing the same behavior with ffmpeg 2.3.3, opencv 2.4.9.

Jameson gravatar imageJameson ( 2014-09-04 14:54:57 -0600 )edit

Just checked on latest master and 2.4 branch. Uninstalled ffmpeg completely from my system, then it uses the package supplied in the 3rd party folder. This one works perfectly. Can you try this?

StevenPuttemans gravatar imageStevenPuttemans ( 2014-09-05 04:34:52 -0600 )edit

2 answers

Sort by » oldest newest most voted

answered 2015-03-23 14:24:42 -0600

nbubis gravatar image

It looks like this issue is specific to certain newer versions of ffmpeg which are incompatible with the 2.4.x branch of OpenCV. However, I have successfully used Opencv 3.0.0 beta to write videos with the X264 FourCC.

What exact version of ffmpeg break the OpenCV compatibility is still a mystery however. I'll be trying out various versions and report back if I find the culprit.

edit flag offensive delete link more

answered 2015-01-27 18:48:54 -0600

margaridamfo.gomes gravatar image

I also have this problem. I'm using opencv 2.4.9. on Ubuntu 14.04.

StevenPuttemans, you say that if I remove ffmpeg that it will work? And it could write the videos?

edit flag offensive delete link more


It then uses the ffmpeg version supplied with OpenCV yes. Also, grab the latest 2.4 branch from Github. Has way more fixes than 2.4.9!

StevenPuttemans gravatar imageStevenPuttemans ( 2015-01-28 02:27:23 -0600 )edit

Can you explain how can I install opencv 2.4.10? Because I tried all day and I can't do the step make, it stands on 0%.

margaridamfo.gomes gravatar imagemargaridamfo.gomes ( 2015-01-28 16:46:45 -0600 )edit

then please address the exact error output of your make and cmake command into a new topic and we will help you out there. No need in reviving this ancient topic :)

StevenPuttemans gravatar imageStevenPuttemans ( 2015-01-29 02:01:16 -0600 )edit

I grabbed the lastest 2.4 branch from Github and removed ffmpeg from my system, so no sign of ffmpeg. But I still have this problem, I can't write videos with the codec H.264 with the container .mp4.

margaridamfo.gomes gravatar imagemargaridamfo.gomes ( 2015-02-06 09:25:00 -0600 )edit

Like being said before, give us your output, I need to know which video capturing system can be the base of your problems...

StevenPuttemans gravatar imageStevenPuttemans ( 2015-02-09 02:52:48 -0600 )edit

My output is:

[libx264 @ 0x9357bc0] broken ffmpeg default settings detected
[libx264 @ 0x9357bc0] use an encoding preset (e.g. -vpre medium)
[libx264 @ 0x9357bc0] preset usage: -vpre <speed> -vpre <profile>
[libx264 @ 0x9357bc0] speed presets are listed in x264 --help
[libx264 @ 0x9357bc0] profile is optional; x264 defaults to high
Could not open codec 'libx264': Unspecified error
Could not open the output video for write: /home/margarida/exams_2014/Output.mp4

margaridamfo.gomes gravatar imagemargaridamfo.gomes ( 2015-02-09 06:25:49 -0600 )edit

Does my problem have a solution? I really need to solve this and I don't know how. If the output that I gave is not enough please tell me what else do I need to give.

margaridamfo.gomes gravatar imagemargaridamfo.gomes ( 2015-02-17 06:04:59 -0600 )edit

Well it says that the used codec for initiation your video cannot be combined with the selected container. For me the only combination that works well is XVID codec with AVI container in OpenCV. Can you try that?

StevenPuttemans gravatar imageStevenPuttemans ( 2015-02-17 06:27:18 -0600 )edit

But if I use the XVID codec with AVI container my output files get a higher frame rate then the input file. That is, the input video properties are lost and the new video does not become as desired.

How I can keep these properties?

margaridamfo.gomes gravatar imagemargaridamfo.gomes ( 2015-02-17 08:04:15 -0600 )edit

If you create a VideoWriter element, than you have to specify the FPS. So please do so, by first grabbing the FPS from the original file and putting it into your writer!

StevenPuttemans gravatar imageStevenPuttemans ( 2015-02-17 08:21:33 -0600 )edit

Question Tools

1 follower


Asked: 2014-04-24 21:38:17 -0600

Seen: 17,342 times

Last updated: Apr 24 '14