Record Video From a Webcam using OpenCV¶
- cv2.VideoWriter( filename, fourcc, fps, frameSize )
The parameters are :
- filename: Specifies the name of the output video file.
- fourcc: (for recording) Defining the codec
- fps: Defined frame rate of the output video stream
- frameSize: Size of the video frames
In [ ]:
import cv2
capture=cv2.VideoCapture(0)
cod = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('out.avi',cod,20.0,(640,480))
while True:
ret,frame=capture.read()
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray',gray)
cv2.imshow('Color',frame)
out.write(frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
capture.release()
cv2.destroyAllWindows()
Also, a video is recorded and saved with the name ‘out.avi’ in the same file location with predefined frame rate i.e 20fps and frame size (640×480)