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:
- anyone knows where and how OpenCV invoke ffmpeg API?
- how to change ffmpeg default settings using code, hopefull, can be easily embeded into OpenCV?
- 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:
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
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.
I downloaded OpenCV2.4.8 from the opencv.org website, and it redirected me to the sourcefourge.com. Would it be any problem?
Nope, but the 2.4.9 release got agan 100+ bugs solved.Try that one first?
I am seeing the same behavior with ffmpeg 2.3.3, opencv 2.4.9.
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?