How to build OpenCV Python bindings to have c2.VideoWriter with h265 support (hev1)?

Hi guys,

So I am building OpenCV (4.6.0) from sources on Windows (using CMake). I’ve selected WITH_FFMPEG and OPENCV_ENABLE_NONFREE. However, I am unable to write video in h265. I’ve searched every possible answer to this problem on forums. Here is what I am getting for the below code:

video_writer = cv2.VideoWriter('some_video.mp4', cv2.VideoWriter_fourcc('h','e','v','1'), fps, frame_size)

[hevc_mf @ 000002583f4940c0] could not find any MFT for the given media type
[hevc_mf @ 000002583f4940c0] could not create MFT
[ERROR:0@1.901] global /build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp (3001) open Could not open codec hevc_mf, error: Unspecified error (-40)

Has anyone had success with this?

you’re still using the prebuilt opencv_ffmpeg dll, so your changes wont do anything

however, have a look here:

1 Like

Wow, so it wasn’t supported for all that time and literally you’ve added this two days ago? :slight_smile:

1 Like

please try & report back !

Sorry @berak I’ve confused your name with the contributor :sweat_smile:
You are mentioning pre-build opencv_ffmpeg dll … but I think when I build OpenCV from sources it also builds ffmpeg?

So in other words, the only thing that I need to do is to rebuild OpenCV with latest changes? Let me know if that is the thing and I will test it and report back.

no it doesnt (cmake downloads readymade dlls)

exactly ! (just note, you need to use some VS c++ compiler, mingw wont do !)
once that succeeds, specify the CAP_MSMF backend in the VìdeoWriter()` call

I did compile previous versions with VS so that is not a problem:)

Correct me if I am wrong but isn’t CAP_MSMF only for VideoCapture object? OpenCV: Flags for video I/O

VideoCaptureAPIs is just an enum declaring a backend. backends may be read-only (e.g. V4L), write-only (I don’t know), or can do both (ffmpeg)

MSMF is windows-only in any case. the MSMF API itself can be used to write video files but I don’t know if the plumbing is in OpenCV to use it for that.

to get HEVC (x265) support into ffmpeg, you’ll have to download the binaries (must contain libraries and shared objects/dlls, not just CLI programs) that come with the codec (there are “free” builds that don’t) and then figure out how to make OpenCV’s cmake stage pick them up. I’ve never done that but I hear others have. it requires setting some cmake variables to the right paths.

This works for me.

2 Likes

Thank you, documentation is a bit misleading at least for me because I thought that MSMF can be used only with video capture, but in fact OpenCV falls back to it (on my machine) when initializing video writer for ffmpeg not worked.

@cudawarped many thanks, I will try that :slight_smile: