Skip to content
Advertisement

How can I record a video with MJPG instead of YUYV on a raspberry PI 4b with opencv-python code

Good day to you dear developers from stackoverflow. I am currently recording live-video with my usb webcam on my raspberry Pi with the following code:

JavaScript

The code doesn’t work anymore, when I use the cap.set(cv.CAP_PROP_FPS, 15) function and I don’t know what I’ve done wrong.

I get the following errors:

JavaScript

Not the thing is, if I remove the cap.set(cv.CAP_PROP_FPS, 15) then the code works fine, but then the camera record with YUYV and not MJPG.

Advertisement

Answer

Set the CAP_PROP_FOURCC property to MJPG.

Look at VideoWriter (yes) documentation for details on handling fourcc values.

JavaScript

Order of .set() method calls matters. Try to put this first. And perhaps also set CAP_PROP_FPS to be sure.

Also make sure to do proper error handling.

JavaScript
JavaScript

And if the backend is gstreamer, perhaps change that.

JavaScript
Advertisement