Save Video From Webcam Using VideoWriter in Python OpenCV

In this tutorial, we will introduce how to use opencv VideoWriter to save video from a webcam.

Best Practice to Save Video From Webcam Using VideoWriter in Python OpenCV

1.Open webcam using opencv

import cv2
capture = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc('X','V','I','D')

2.Create a videoWriter object to save video using cv2.VideoWriter()

videoWriter = cv2.VideoWriter('C:/Users/N/Desktop/video.avi', fourcc, 30.0, (640,480))

3.Start to save video from webcam

while (True):
    ret, frame = capture.read()
    if ret:
        cv2.imshow('video', frame)
        videoWriter.write(frame)

    if cv2.waitKey(1) == 27:
        break

In this code, we will save each video frame one by one.

7.Release webcam

capture.release()
videoWriter.release()

cv2.destroyAllWindows()

Here is the example:

Save Video From Webcam Using VideoWriter in Python OpenCV