v4l2 Python - streaming video - mapping buffers

Just to add another option here that I just discovered, you are also able to use the V4L2 backend with OpenCV as well.

You simply need to specify it in the VideoCapture constructor. For example

cap = cv2.VideoCapture()

cap.open(0, apiPreference=cv2.CAP_V4L2)

cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 960)
cap.set(cv2.CAP_PROP_FPS, 30.0)

When this is not explicitly specified, OpenCV will often use another camera API (e.g., gstreamer), which is often slower and more cumbersome. In this example I went from being limited to 4-5 FPS to up to 15 at 720p (using an Intel Atom Z8350).

And if you wish to use it with a ring buffer (or other memory-mapped buffer), take a look at the following resources:

https://github.com/Battleroid/seccam

https://github.com/bslatkin/ringbuffer