Unable to use the encoder libx264 for windows

Hello, I need to use the encoder as libx264 for an output video generated by videowriter . I used the following PowerShell command to enable it.

$env:OPENCV_FFMPEG_WRITER_OPTIONS=“vcodec;x264|vprofile;high|vlevel;4.0”

Despite configuring the environmental variables, the OpenCV:FFmpeg backend persists in using the libopenh264 encoder. Please find the reference logs below.

[DEBUG:0@0.013] global cap_ffmpeg_impl.hpp:1179 open FFMPEG: stream[0] is video stream with codecID=27 width=1920 height=1080
[DEBUG:0@0.013] global cap_ffmpeg_hw.hpp:934 HWAccelIterator FFMPEG: allowed acceleration types (none): ‘’
[OPENCV:FFMPEG:48] nal_unit_type: 7(SPS), nal_ref_idc: 3
[OPENCV:FFMPEG:48] nal_unit_type: 8(PPS), nal_ref_idc: 3
[DEBUG:0@0.013] global cap_ffmpeg_impl.hpp:2773 open Selected pixel format: bgr24
[ WARN:0@0.071] global cap.cpp:619 cv::VideoWriter::open VIDEOIO(FFMPEG): created, isOpened=1
[DEBUG:0@0.013] global cap_ffmpeg_impl.hpp:2980 open VIDEOIO/FFMPEG: using writer options from environment: vcodec;x264|vprofile;high|vlevel;4.0
[DEBUG:0@0.013] global cap_ffmpeg_hw.hpp:934 HWAccelIterator FFMPEG: allowed acceleration types (none): ‘’
[OPENCV:FFMPEG:40] [OpenH264] this = 0x000001748241ED70, Info:CWelsH264SVCEncoder::SetOption():ENCODER_OPTION_TRACE_CALLBACK callback = 00000000716E9B10.
[OPENCV:FFMPEG:40] Using CAVLC, select EProfileIdc PRO_BASELINE in libopenh264.

Can anyone please help me with this issue.

I don’t think this is implemented see

and

@cudawarped, currently, I am using ffmpeg with the below specified version installed manually on my Windows machine. Upon examining the ffmpeg command in PowerShell, I observed that libx264 is already enabled. So is it still required to build against this version of ffmpeg to uses the libx264 in opencv.(videowriter)

PS D:\local_test> ffmpeg -codecs | Select-String “libx264”
ffmpeg version 2023-11-20-git-e56d91f8a8-full_build-www.gyan.dev Copyright (c) 2000-2023 the FFmpeg developers
built with gcc 12.2.0 (Rev10, Built by MSYS2 project)
configuration: --enable-gpl --enable-version3 --enable-static --pkg-config=pkgconf --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-sdl2 --enable-libaribb24 --enable-libaribcaption --enable-libdav1d --enable-libdavs2 --enable-libuavs3d --enable-libzvbi --enable-librav1e --enable-libsvtav1 --enable-libwebp –enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libaom --enable-libjxl --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-libharfbuzz --enable-liblensfun --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-dxva2 --enable-d3d11va --enable-libvpl --enable-libshaderc --enable-vulkan --enable-libplacebo --enable-opencl --enable-libcdio --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libshine --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libcodec2 --enable-libilbc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa --enable-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint
libavutil 58. 32.100 / 58. 32.100
libavcodec 60. 33.100 / 60. 33.100
libavformat 60. 17.100 / 60. 17.100
libavdevice 60. 4.100 / 60. 4.100
libavfilter 9. 13.100 / 9. 13.100
libswscale 7. 6.100 / 7. 6.100
libswresample 4. 13.100 / 4. 13.100
libpostproc 57. 4.100 / 57. 4.100

DEV.LS h264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (decoders: h264 h264_qsv h264_cuvid) (encoders: libx264 libx264rgb h264_amf
h264_mf h264_nvenc h264_qsv)

If your on windows unless you built OpenCV with -DOPENCV_FFMPEG_USE_FIND_PACKAGE=ON and have the FFMpeg shared libraries correctly set up you are using the OpenCV FFMpeg shared library.

Check your CMake configuration output unless you have something similar to the below you are using the OpenCV version

– Video I/O:
– DC1394: NO
– FFMPEG: YES (find_package)
– avcodec: YES (59.18.100)
– avformat: YES (59.16.100)
– avutil: YES (57.17.100)
– swscale: YES (6.4.100)

@cudawarped I have installed the opencv-python via the below command

pip3 install opencv-python

I have enabled the logs via environment variables
$env:OPENCV_LOG_LEVEL = “VERBOSE”
$env:OPENCV_VIDEOIO_DEBUG = “1”
$env:OPENCV_FFMPEG_DEBUG = “1”
$env:OPENCV_FFMPEG_LOGLEVEL = “56”
$env:OPENCV_FFMPEG_WRITER_OPTIONS=“vcodec;x264|vprofile;high|vlevel;4.0”

OpenCV version - 4.9.0

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)

I haven’t seen opencv come with an ffmpeg that contains libx264 encoder. legal reasons probably.

Your using the OpenCV version and as per my initial comment the environmental variable

does not seem to be supported at present.

@cudawarped /@crackwitz
Thanks for the clarification , is there any alternative way to use libx264 as an encoder for Videowriter in OpenCV-Python.