jay
January 2, 2024, 10:47am
1
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
Building OpenCV in “default” way (CMake/ Code::Blocks/ mingw-64)
it uses a prebuilt ffmpeg, wrapped in “opencv_videoio_ffmpeg480_64.dll”.
My 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
But this ffmpeg seems to offer only “rudimentary” (video) codecs.
Especially the most-used h264 codec with acceptable qu…
and
opened 05:54PM - 21 Nov 23 UTC
bug
category: videoio
### System Information
OpenCV version: 4.8.0
Operating System / Platform: Wi… ndows 7-64
Compiler & compiler version: mingw-64
CMake
Code::Blocks
### Detailed description
Built OpenCV v.4.8.0 with tools as above
Used prebuild ffmpeg backend.
([Detailed description](https://forum.opencv.org/t/error-your-platform-doesnt-suppport-hardware-accelerated-av1-decoding-when-i-use-cv2-videocapture/9605/14)) and [this](https://forum.opencv.org/t/how-to-have-opencv-built-process-use-self-built-ffmpeg/15561/1).
### Steps to reproduce
Using the VideoWriter, and setting ffmpeg's crf-option before
```
_putenv ("OPENCV_FFMPEG_WRITER_OPTIONS=crf;18");
VideoWriter VW ("Output.mp4", CAP_FFMPEG, VideoWriter::fourcc('a','v','c','1'), capt.get(CAP_PROP_FPS), refS);
```
generates always h264 default crf 23.
See lines
[DEBUG:0@0.065] and
[OPENCV:FFMPEG:32] in the following (bolding doesn't seem to work in code):
```
[DEBUG:0@0.008] global videoio_registry.cpp:218 VideoBackendRegistry VIDEOIO: Builtin backends(8): FFMPEG(1000); GSTREAMER(990); INTEL_MFX(980); MSMF(970); DSHOW(960); CV_IMAGES(950); CV_MJPEG(940); UEYE(930)
[DEBUG:0@0.008] global videoio_registry.cpp:242 VideoBackendRegistry VIDEOIO: Available backends(8): FFMPEG(1000); GSTREAMER(990); INTEL_MFX(980); MSMF(970); DSHOW(960); CV_IMAGES(950); CV_MJPEG(940); UEYE(930)
[ INFO:0@0.008] global videoio_registry.cpp:244 VideoBackendRegistry VIDEOIO: Enabled backends(8, sorted by priority): FFMPEG(1000); GSTREAMER(990); INTEL_MFX(980); MSMF(970); DSHOW(960); CV_IMAGES(950); CV_MJPEG(940); UEYE(930)
[OPENCV:FFMPEG:40] Reinit context to 1280x720, pix_fmt: yuv420p
[DEBUG:0@0.061] global cap_ffmpeg_impl.hpp:1182 open FFMPEG: stream[0] is video stream with codecID=27 width=1280 height=720
[DEBUG:0@0.061] global cap_ffmpeg_hw.hpp:934 HWAccelIterator FFMPEG: allowed acceleration types (none): ''
[DEBUG:0@0.065] global cap_ffmpeg_impl.hpp:2773 open Selected pixel format: bgr24
**[DEBUG:0@0.065] global cap_ffmpeg_impl.hpp:2980 open VIDEOIO/FFMPEG: using writer options from environment: crf;18**
[DEBUG:0@0.065] global cap_ffmpeg_hw.hpp:934 HWAccelIterator FFMPEG: allowed acceleration types (none): ''
[OPENCV:FFMPEG:32] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[OPENCV:FFMPEG:32] profile High, level 3.1, 4:2:0, 8-bit
[OPENCV:FFMPEG:32] 264 - core 164 - H.264/MPEG-4 AVC codec - Copyleft 2003-2022 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 **crf=23.0** qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
[OPENCV:FFMPEG:40] Reinit context to 1280x720, pix_fmt: yuv420p
[DEBUG:0@0.082] global cap_ffmpeg_impl.hpp:1609 retrieveFrame Input picture format: yuv420p
[DEBUG:0@0.085] global parallel.cpp:107 createDefaultParallelForAPI core(parallel): Initializing parallel backend...
[DEBUG:0@0.085] global registry_parallel.impl.hpp:69 ParallelBackendRegistry core(parallel): Builtin backends(3): ONETBB(1000); TBB(990); OPENMP(980)
[DEBUG:0@0.085] global registry_parallel.impl.hpp:94 ParallelBackendRegistry core(parallel): Available backends(3): ONETBB(1000); TBB(990); OPENMP(980)
[ INFO:0@0.085] global registry_parallel.impl.hpp:96 ParallelBackendRegistry core(parallel): Enabled backends(3, sorted by priority): ONETBB(1000); TBB(990); OPENMP(980)
[DEBUG:0@0.085] global parallel.cpp:65 createParallelForAPI core(parallel): trying backend: ONETBB (priority=1000)
[DEBUG:0@0.085] global plugin_parallel_wrapper.impl.hpp:233 getPluginCandidates Found 2 plugin(s) for ONETBB
[ INFO:0@0.085] global plugin_loader.impl.hpp:67 libraryLoad load D:\Guido\develop\opencv\build\bin\opencv_core_parallel_onetbb480_64.dll => FAILED
[ INFO:0@0.086] global plugin_loader.impl.hpp:67 libraryLoad load opencv_core_parallel_onetbb480_64.dll => FAILED
[DEBUG:0@0.086] global parallel.cpp:65 createParallelForAPI core(parallel): trying backend: TBB (priority=990)
[DEBUG:0@0.086] global plugin_parallel_wrapper.impl.hpp:233 getPluginCandidates Found 2 plugin(s) for TBB
[ INFO:0@0.086] global plugin_loader.impl.hpp:67 libraryLoad load D:\Guido\develop\opencv\build\bin\opencv_core_parallel_tbb480_64.dll => FAILED
[ INFO:0@0.086] global plugin_loader.impl.hpp:67 libraryLoad load opencv_core_parallel_tbb480_64.dll => FAILED
[DEBUG:0@0.086] global parallel.cpp:65 createParallelForAPI core(parallel): trying backend: OPENMP (priority=980)
[DEBUG:0@0.086] global plugin_parallel_wrapper.impl.hpp:233 getPluginCandidates Found 2 plugin(s) for OPENMP
[ INFO:0@0.086] global plugin_loader.impl.hpp:67 libraryLoad load D:\Guido\develop\opencv\build\bin\opencv_core_parallel_openmp480_64.dll => FAILED
[ INFO:0@0.086] global plugin_loader.impl.hpp:67 libraryLoad load opencv_core_parallel_openmp480_64.dll => FAILED
[DEBUG:0@0.086] global parallel.cpp:93 createParallelForAPI core(parallel): fallback on builtin code
[DEBUG:0@0.117] global cap_ffmpeg_impl.hpp:1609 retrieveFrame Input picture format: yuv420p
[DEBUG:0@0.146] global cap_ffmpeg_impl.hpp:1609 retrieveFrame Input picture format: yuv420p
...
```
(Codec hevc always generated (near) lossless, no matter what crf is set.)
### Issue submission checklist
- [X] I report the issue, it's not a question
- [X] I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
- [X] I updated to the latest OpenCV version and the issue is still there
- [x] There is reproducer code and related data files (videos, images, onnx, etc)
jay
January 2, 2024, 2:02pm
3
jay:
libopenh264
@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)
jay
January 2, 2024, 2:54pm
5
@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.
jay
January 3, 2024, 4:52am
8
@cudawarped /@crackwitz
Thanks for the clarification , is there any alternative way to use libx264 as an encoder for Videowriter in OpenCV-Python.