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

**URL:** <https://forum.opencv.org/t/reading-writing-16bit-or-float-video-using-codecs-with-compression/18682>\
**Category:** Python\
**Tags:** videoio\
**Created:** [September 6, 2024, 3:30pm UTC](https://forum.opencv.org/t/reading-writing-16bit-or-float-video-using-codecs-with-compression/18682 "2024-09-06T15:30:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ysuter](https://avatars.discourse-cdn.com/v4/letter/y/f1d935/32.png) [@ysuter](https://forum.opencv.org/u/ysuter)\
**Post date:** [September 6, 2024, 3:30pm UTC](https://forum.opencv.org/t/reading-writing-16bit-or-float-video-using-codecs-with-compression/18682/1 "2024-09-06T15:30:25Z")

</div>

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:

```auto
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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [September 7, 2024, 9:34am UTC](https://forum.opencv.org/t/reading-writing-16bit-or-float-video-using-codecs-with-compression/18682/2 "2024-09-07T09:34:04Z")

</div>

OpenCV uses ffmpeg.

find out what codecs ffmpeg can offer you _in general_.

```shell
$ 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.

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [September 16, 2024, 8:25am UTC](https://forum.opencv.org/t/reading-writing-16bit-or-float-video-using-codecs-with-compression/18682/3 "2024-09-16T08:25:26Z")

</div>

also see:

> <https://github.com/opencv/opencv/pull/22894>
>
> Partially resolves #10623
> Replaces #12284 and #12290
> 
> \* \_VideoCapture\_: we ca…n now disable BGR conversion using parameter for the FFmpeg backend and return image in original format, straight from decoder. However this feature has many limitations, for example it is not possible to return multiplanar images yet - new interface or additional processing is necessary for this. So I added a warning with some details which will be printed when this feature is enabled. Basically 8UC1 and 16UC1 can now be returned from \_VideoCapture\_ in addition to BGR.
> \* \_VideoWriter\_: we can set depth using parameter during object initialization. It can be 8U or 16U. In combination with \_isColor\_ it gives us three supported formats: 8UC3, 8UC1 and 16UC1; and one unsupported: 16UC3. 16UC1 can be handled by the \`FFV1\` codec.
> \* Several debug and warning log messages have been added for convenience and safety
> \* The new test writes a video and then reads it back, frames are not compared with the original, only format and count is verified. Unsupported format combination is also checked in this test which is not very good, but I believe is fine for now.
> \* FFV1 codec/container has been added to generic ffmpeg and videoio tests (regular BGR)
> 
> Note: some other codecs had \_hidden\_ support for 16UC1, but I did not test them (PNG and RAWVIDEO).
> 
> Note: originally I wanted to modify #12290 - add a test, fix minor issues, but it turned out that I had to rewrite 90% of changes made in that PR, so I decided to publish it as a brand new one.
> 
> \`\`\`
> buildworker:Custom=linux-4
> build\_image:Custom=ffmpeg-master
> \`\`\`
