Laura Muiño Laura Muiño - 14 days ago 12
C++ Question

OpenCV VideoWriter doesn't work

I'm trying to use opencv videoWriter to get a video file. But I get the following problem:

>[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

I do have libx264 in my system, so I guess this last line is just a side effect

The code i'm trying to run is an example taken from 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",

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 (!



return 0;

I found a post with a similar problem How to get stream info from opened file in ffmpeg? but no one answered correctly yet.

I've found people are told to check if opencv is using old fmmpeg instead of libav, which it isn't since it's a fresh build and my ubuntu doesn't have ffmpeg.


Dimazavr's answer is not totally right. First, you need to change the output video filename extension from .mp4 to .avi. Then if you run the code, you will get the following error information:

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)

That means either cv::VideoWriter in opencv2.4 doesn't support libx264 format or avi extension is not incompatible with libx264 format. I don't suggest to use libx264 codec. You can try the following codec formats list supported by 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

From my experiences, the quality of CV_FOURCC('D', 'I', 'V', 'X') is fine. Besides, if you set cv_fourcc as -1, you can choose one of codec formats supported in your system in a GUI window. You can witness the running process here.