A program to save your captured video

prerequisites: video_basics

This will just show you how to save the video captured from your machine’s default camera.

#to capture a live stream from camera and saving the video
import cv2
cap=cv2.VideoCapture(0)
fourcc=cv2.VideoWriter_fourcc(*'XVID')
out=cv2.VideoWriter('video.avi',fourcc,20.00,(640,480))

while(cap.isOpened()): #isOpened func returns true if video is getting captured
    ret,frame=cap.read()
    if ret==True:

        out.write(frame)


        cv2.imshow('frame',frame)
        if cv2.waitKey(1)==ord('q'):#press 'q' to quit
            break
    else:
        break
cap.release()
out.release()
cv2.destroyAllWindows()

Functions used:

VideoWriter_fourcc():

This function is used to get the fourcc code of the video capture object.

VideoWriter ():

This function is used to save our captured videos.It takes four parameters: video file name,the four cc code of our video ,no. of frames per second of our video and the frame size of our video in the form of a tuple. VideoWriter(‘video.avi’,fourcc,20.00,(640,480))

Here ‘videdo.avi’ is file name given to our captured video.

Leave a Reply