How to build and install opencv on windows 10 to recognize your custom ffmpeg


I have an user build of ffmpeg using the repo “GitHub - m-ab-s/media-autobuild_suite: This Windows Batchscript helps setup a Mingw-w64 compiler environment for building ffmpeg and other media tools under Windows.”. I’m trying to build opencv so it uses my user build ffmpeg, but no matter what I do, it seems to be using it’s prebuilt binaries ffmpeg.

I cloned opencv 4.9.0-dev and created a build folder in opencv.
I then used the cmake command to configure:
"cmake -DCMAKE_VERBOSE_MAKEFILE=ON -D WITH_FFMPEG=ON -D FFMPEG_INCLUDE_DIRS=C:/media-autobuild_suite/local64/include -D FFMPEG_LIBRARIES=C:/media-autobuild_suite/local64/lib …

part of the log shows:
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
– Media Foundation: YES

Does anyone know what I’m doing wrong or can a shed to the right direction? Have been trying various methods the past few days and can’t resolve it.

My build and install commands are:
cmake --build . --config Release
cmake --build . --target INSTALL --config Release

I have set my PATH on windows for my user build ffmpeg
I don’t have Visual Studio but I have the Visual Studio Build Tools
I’m ultimately trying to use opencv in Python to convert images to video using h264

The easiest way is to use a CMake script, see

note you may need to update the script used in the post for the latest version of FFMpeg.

Hi, I just did that today and when I run my python code I get this error
Traceback (most recent call last):
line 20, in
import cv2
File “C:\Users\Myo\AppData\Local\Programs\Python\Python312\Lib\site-packages\”, line 181, in
File “C:\Users\Myo\AppData\Local\Programs\Python\Python312\Lib\site-packages\”, line 153, in bootstrap
native_module = importlib.import_module(“cv2”)
File “C:\Users\Myo\AppData\Local\Programs\Python\Python312\Lib\”, line 90, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
ImportError: DLL load failed while importing cv2: The specified module could not be found.
I didn’t change anything in the cmake script and I used

But now I have a serious problem, even when i remove the build folder in opencv and use opencv from pip install opencv-python I still get that error, before if I just use opencv from pip I would get a h264 not found id 27 not recognized follow by a cisco msg but now I can’t even use prebuilt opencv.

If that error appears after you build against FFMpeg libs then you need to add the path to the FFmpeg shared libraries to python.


import os
import cv2

Your problem is you have left the PYTHON_DIST\Lib\site-packages\cv2 directory containing the OpenCV bindings you built and then installed a new version over the top of it. I suggest cleaning your python environment before proceeding

pip uninstall opencv-python

and then delete the PYTHON_DIST\Lib\site-packages\cv2 directory.

Then build OpenCV INSTALL target again and before loading python add the directory to FFMpeg as described above.

I made sure I didn’t have pip version of opencv installed
Then I deleted the build directory in opencv from source then deleted the lib/site-packages/cv2 directory
Rebuilt the opencv and installed it and then added the directory to ffmpeg in python as you instructed and fixed the dll load failed issue

but now I’m back at this error:
OpenCV: FFMPEG: tag 0x34363268/‘h264’ is not supported with codec id 27 and format ‘mp4 / MP4 (MPEG-4 Part 14)’
OpenCV: FFMPEG: fallback to use tag 0x31637661/‘avc1’
when I use ffmpeg -codecs it shows I do have h264?
DEV.LS h264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (decoders: h264 h264_qsv libopenh264 h264_cuvid ) (encoders: libx264 libx264rgb libopenh264 h264_amf h264_mf h264_nven
c h264_qsv )
I’m using the provided in the above post.

part of code used is this:

Define the codec (use ‘H264’ for lossless compression)

fourcc = cv2.VideoWriter_fourcc('h','2','6','4')

Do you know what is causing this?

Is that an error? In OpenCV ‘avc1’ is used for ‘h264’

which it is using. Can you play the resulting .mp4 file?

Yes, I can but quality of the video is diminished quite a bit. Essentially I’m converting images to video and I’m trying to retain the quality of the image in the video. In the video the text is not as smooth and areas with black lines or dots are not “blended” in like the image and shows some white outlines.

I also tried x265 and it says not supported as well but doesn’t “fallback” to anything

Do you have any suggestions for this? I’m not an expert and this is new territory for me. Is there a codec or parameter I need to use specifically to convert jpg to video with lossless.

You need to alter the encoding parameters to use a higher bitrate.

I don’t have any experiance with this but you should be able to pass these parameters by setting them in the OPENCV_FFMPEG_WRITER_OPTIONS environmental variable.

Hi cuda,

Thanks for the info, I’ll look into it. Currently I’m just going to make do with what I got. I appreciate the help on getting me unblocked with the dll issue and getting my opencv to recognize ffmpeg.

1 Like