O'Reilly logo

Computer Vision with Python 3 by Saurabh Kapur

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Writing a video

To save a video, OpenCV provides the VideoWriter class. Let's see how to use this class to save a video by writing some code. For this example, we will first read images from the default webcam in computers and then save it to a file using the VideoWriter class.

The following is the code that does the same:

import cv2cam = cv2.VideoCapture(0)ret, frame = cam.read()h, w = frame.shape[:2]fourcc = cv2.VideoWriter_fourcc(*'DIVX')video_write = cv2.VideoWriter(saved_out.avi', fourcc, 25.0, (w, h) )while (cam.isOpened()):         ret, frame = cam.read()         video_write.write(frame)         cv2.imshow('video',frame)         if cv2.waitKey(1) & 0xFF == ord('q'):                  breakcam.release()video_write.release()cv2.destroyAllWindows()

In this code, we first create a video_write ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required