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 quality lacks.
My question:
What exactly has to be done to let OpenCV build use a pre-built
(full-featured) ffmpeg of one’s own?
(Which probably means to have let OpenCV built the ffmpeg wrapper anew?)
As I don’t know much about building/ dependencies etc. I couldn’t assess at all which is the right direction, and which completely misleading.
I had a closer look at your proposition; and it seems that I either have to build ffmpeg by myself (being at windows, I will have to install m2sys, and doing in unix world),
or somehow have to make ffmpeg use x264 instead of openh264. Both are external but OpenCV’s ffmpeg asks/ uses openh264, not x264 (?)
The chain becomes longer and longer; and every step in unknown territory
(which gives an uneasy feeling: if the simplest thing goes wrong, I’m at loss again).
There’s only that setting OPENCV_FFMPEG_WRITER_OPTIONS (e.g. to “crf;18|tune;grain”) before starting my app or before building my app has no effect.
It seems to have been done once and forever when building OpenCV itself?
So there would be no way to use different encoding parameters?
I don’t know as I have not tried changing the writer options myself. That said documentation
Note: extra FFmpeg options should be pased in form key;value|key;value|key;value, for example hwaccel;cuvid|video_codec;h264_cuvid|vsync;0 or vcodec;x264|vprofile;high|vlevel;4.0
implies you should be able to tweak some parameters but maybe not all of them.
Produces a strange avc1 file (quality very bad/ file size very large).
Next:
set OPENCV_FFMPEG_WRITER_OPTIONS=crf;18
then ran my app.
Produces… the same file!
Now my assumption was OpenH264 was not capable to use the env var.
That’s why I installed newest OpenCV 4.8.0 with built-in x264 (with your kind help).
Result:
The produced avc1 file is coded with default crf23 (= standard) alright.
But in both cases!
My next supposition (until now) was that the env var have to be set when building OpenCV (as the names are similar to the CMake entries).
But your link to the documentation contradicts that (thank god).
It would be nice if somebody would check if/ how these env var do have an effect at all.
Maybe a bug/ lack in documenation?
@crackwitz has already raised an issue to this effect.
If it was me I would check that the env var is being passed correctly to OpenCV either by placing a breakpoint
or increasing the log level
OPENCV_FFMPEG_DEBUG=1
OPENCV_LOG_LEVEL=DEBUG
I would also double check that OpenCV is using the supplied version of FFMpeg, not the automaitically downloaded OpenCV shared library and that calling that version outside of OpenCV (ffmpeg.exe) with the arguments you mention has the desired result.
Curiously I tried using HEVC for output codec.
It works - producing (nearly) lossless output;
taking very long and producing file size abt. half FFV1.
But crf=28 is default for HEVC - not lossless.
It produces only that (nearly) lossless - setting “crf;18” has no effect here either.
So the problem of wrong handling of codec settings is severe.
Would be nice if anybody would retrace that
and either confirm or reject it,
to possibly enter an issue.