How to record video from a camera using OpenCV on Mac
OpenCV has several nuances and silent fails that makes recording videos from cameras difficult and frustrating. Here’s how to record from a camera:
import cv2cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (w,h))
ret, frame = cap.read()
if cv2.waitKey(1) & 0xFF == ord('q'):
If your videos are being saved as ~6kb files, there is likely a size mismatch between the resolution of the camera and the VideoWriter.
Hope this helps.