How to record video from a camera using OpenCV on Mac

Joel Masters
Jun 19, 2021
Photo by Waldemar Brandt on Unsplash

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))

while(cap.isOpened()):
ret, frame = cap.read()
if ret:
out.write(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()

Adapted from: https://stackoverflow.com/questions/29317262/opencv-video-saving-in-python/45868817

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.

--

--