How to record video from a camera using OpenCV on Mac

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

ret, frame =
if ret:
out.write(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
if cv2.waitKey(1) & 0xFF == ord('q'):

Adapted from:

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.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Joel Masters

Joel Masters

Creator of Help others achieve their goals. Get paid.