Reading/writing 16bit or float video using codecs with compression

Hi,
I am using thermal/infrared cameras (Optris Xi 410) to record videos. Since the values can be converted to actual temperatures, I need to be able to either:

  • Save video frames as float
  • Save video frames as 16U to later convert and have the desired precision (uint8 is too restrictive)

To have the desired value range, I am currently saving the output as single-channel 16U avi video with the FFV1 fourcc. Due to the lack of compression, the videos get prohibitively large, since I need to record over several hours with multiple cameras. Therefore, I would like to switch to a codec with compression. I tried XVID and DIVX, but they either get converted to uint8 or it fails to load the frames.

For recording, I use Debian 12, reading on Ubuntu 24.04, opencv-python version 4.10.0.84.

Here are the snippets how I read/write the frames (with xvid codec, not the working ffv1 one:

twriter_thermal = cv2.VideoWriter(filename=recfilepath, apiPreference=cv2.CAP_FFMPEG, fourcc=cv2.VideoWriter_fourcc(*'XVID'), fps=25, frameSize=(thermal_width.value, thermal_height.value),
            params=[cv2.VIDEOWRITER_PROP_DEPTH, cv2.CV_16U, cv2.VIDEOWRITER_PROP_IS_COLOR, 0, ])
...
writer_thermal.write(tempimg.astype(np.uint16))

and for reading:

cap = cv2.VideoCapture(videoroot, cv2.CAP_FFMPEG)
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc(*'XVID'))
cap.set(cv2.CAP_PROP_FRAME_TYPE, cv2.CV_16U)
cap.set(cv2.CAP_PROP_CONVERT_RGB, 0.0)

With cap.read() always returns this way. Here are my questions:

  • Is it possible to use a codec with compression for 16bit single-channel videos? If so, which one?
  • Is this caused by some other mistake I am making?
  • I am also grateful for any other insight on how to write/read 16bit or float video frames in any form.

Best regards and thank you in advance!

OpenCV uses ffmpeg.

find out what codecs ffmpeg can offer you in general.

$ ffmpeg -codecs

due to legal reasons, OpenCV may come with an ffmpeg that only has “free” codecs, so you might miss out on advanced choices. you cannot change that by installing some other ffmpeg on your system. libraries don’t work like that. if (!) you actually want to use a codec that is actually missing due to legal reasons, you can build ffmpeg yourself and you can build OpenCV to use your own build of ffmpeg. there might be shortcuts. there are full-featured binary builds of ffmpeg (and its libraries) out there. if you want to use one of those, you need to understand that libraries built with one compiler cannot be used by another compiler. on linux, everyone uses GCC, but on Windows, people do well to use the native compiler, which is MSVC, so if you want to build anything on Windows, stick to MSVC (included in Visual Studio), and do not mess around with mingw.

also see: