Search code examples
opencvffmpeg

Opencv Cannot write ffmpeg 16bit video


I have ffmpeg 4.4.2 installed, OpenCV 4.7.0 built with ffmpeg support.

The cv::videoio_registry::hasBackend(cv::CAP_FFMPEG) returns true.

But looks like I cannot set writing to 16-bit:

VideoWriter video;
video.open(filename, CAP_FFMPEG, cv::VideoWriter::fourcc('F', 'F', 'V', '1'), FPS, sz, false);
bool success = video.set(cv::VIDEOWRITER_PROP_DEPTH, CV_16U);  
// ^^ success is FALSE!

Is that an issue with my local FFMPEG library? ffmpeg -formats gives has this sublist, I see here Unsigned 16 bit:

 DE u16be           PCM unsigned 16-bit big-endian
 DE u16le           PCM unsigned 16-bit little-endian
 DE u24be           PCM unsigned 24-bit big-endian
 DE u24le           PCM unsigned 24-bit little-endian
 DE u32be           PCM unsigned 32-bit big-endian
 DE u32le           PCM unsigned 32-bit little-endian
 DE u8              PCM unsigned 8-bit

I also confirmed that my ffmpeg can create 16-bit videos with this command:

ffmpeg -framerate 3 -pattern_type glob -i '*.png'  -c:a pcm_s16le -vcodec ffv1 out2.mkv

I need to write 16-bit videos in C++ with OpenCV. How to set the video writer depth property to 16 bit?


Solution

  • Turns out, the setter and getter for the depth property does not work (opencv bug), but adding it to the params in open() does work:

     VideoWriter video;
     video.open(filename, cv::CAP_FFMPEG, VideoWriter::fourcc('F', 'F', 'V', '1'), fps, sz, 
                 {VIDEOWRITER_PROP_DEPTH, CV_16UC1,
                 VIDEOWRITER_PROP_IS_COLOR, false});
    

    I enabled CV debug logging as saw that output format is gray16le

    Also here is OpenCV commit for 16U support, for reference - link