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