# How to built OpenCV with a useful h264 codec?

**URL:** <https://forum.opencv.org/t/how-to-built-opencv-with-a-useful-h264-codec/15528>\
**Category:** C++\
**Tags:** build, videoio\
**Created:** [November 15, 2023, 11:02am UTC](https://forum.opencv.org/t/how-to-built-opencv-with-a-useful-h264-codec/15528 "2023-11-15T11:02:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![nji9](https://avatars.discourse-cdn.com/v4/letter/n/f08c70/32.png) [@nji9](https://forum.opencv.org/u/nji9)\
**Post date:** [November 15, 2023, 11:02am UTC](https://forum.opencv.org/t/how-to-built-opencv-with-a-useful-h264-codec/15528/1 "2023-11-15T11:02:00Z")

</div>

Recently I needed to save the manipulation of OpenCV as a file.  
It is too big for a lossless format, so I chose h264  
with “quality” parameters.  
Having built OpenCV (4.8.0) by myself (Code::Blocks/ mingw64/ Windows)  
it obviously uses OpenH264 from Cisco Systems,  
as if I don’t provide openh264-1.8.0-win64.dll running my prog it says:

> Failed to load OpenH264 library: openh264-1.8.0-win64.dll  
> Please check environment and/or download library: [Releases · cisco/openh264 · GitHub](https://github.com/cisco/openh264/releases)
> 
> [libopenh264 @ 000000000050eec0] Incorrect library version loaded [ERROR:0@0.018] global cap\_ffmpeg\_impl.hpp:3076 open Could not open codec libopenh264, error: Unspecified error (-22)  
> [ERROR:0@0.018] global cap\_ffmpeg\_impl.hpp:3093 open VIDEOIO/FFMPEG: Failed to initialize VideoWriter

Setting OPENCV\_FFMPEG\_WRITER\_OPTIONS to “crf;18|tune;grain” has no effect.

Result:  
The quality of the generated file is very (!) bad,  
and I was told the reason for this would be the bad OpenH264 codec.  
I shall “build OpenCV with ffmpeg directly” (or use the second so).

At this point I’m at a loss (as I’m no OpenCV dev but only a user).

Can somebody please explain how to achieve the basic operation:  
Writing out a h264 movie with a good quality.

What do I have to do to “built OpenCV with ffmpeg”?  
(ffmpeg lib has been downloaded at the CMake process anyway).

CMake’s output:

> Video I/O:  
> DC1394: NO  
> FFMPEG: YES (prebuilt binaries)  
> avcodec: YES (58.134.100)  
> avformat: YES (58.76.100)  
> avutil: YES (56.70.100)  
> swscale: YES (5.9.100)  
> avresample: YES (4.0.0)  
> GStreamer: NO  
> DirectShow: YES

My Code

> VideoWriter VW (“output.mp4”, CAP\_FFMPEG, VideoWriter::fourcc(‘a’,‘v’,‘c’,‘1’), capt.get(CAP\_PROP\_FPS), refS);
