Skip to content

Instantly share code, notes, and snippets.

@rinaldo-rex
Last active December 19, 2019 09:26
Show Gist options
  • Save rinaldo-rex/81c060e827d78a465557cd2562ec3724 to your computer and use it in GitHub Desktop.
Save rinaldo-rex/81c060e827d78a465557cd2562ec3724 to your computer and use it in GitHub Desktop.
A simple script to record video via webcam and save to disk
# Thanks to http://tsaith.github.io/record-video-with-python-3-opencv-3-on-osx.html
import numpy as np
import cv2
cap = cv2.VideoCapture(0) # Capture video from camera
# Get the width and height of frame
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) + 0.5)
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) + 0.5)
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Be sure to use the lower case
out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (width, height))
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
frame = cv2.flip(frame,0)
# write the flipped frame
out.write(frame)
cv2.imshow('frame',frame)
if (cv2.waitKey(1) & 0xFF) == ord('q'): # Hit `q` to exit
break
else:
break
# Release everything if job is finished
out.release()
cap.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment