How to do it...

Here are the steps we need to execute in order to complete this recipe:

  1. First, we create a camera capture object, as in the previous recipes, and get the frame height and width:
import cv2capture = cv2.VideoCapture(0)frame_width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))frame_height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))print('Frame width:', frame_width)print('Frame height:', frame_height)
  1. Create a video writer:
video = cv2.VideoWriter('../data/captured_video.avi', cv2.VideoWriter_fourcc(*'X264'),                        25, (frame_width, frame_height))
  1. Then, in an infinite while loop, capture frames and write them using the video.write method:
while True:    has_frame, frame = capture.read()    if not has_frame: print('Can\'t get frame') ...

Get OpenCV 3 Computer Vision with Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.