In this tutorial we will learn how to read frames from a webcam and save them in a video file, using Python and OpenCV.
For a detailed tutorial on how to get frames from a camera, please check here.
This tutorial was tested on Windows 8.1, with version 4.1.2 of OpenCV. The Python version used was 3.7.2.
The code from this tutorial is based on the example from the OpenCV documentation, which I encourage you to check.
We will start by importing the cv2 module.
Then, we will create an object of class VideoCapture. This object will allow us to get frames from the camera. As input of the constructor, we need to pass the index of the device we want to use. If we have a single webcam connected to the computer, we should pass the value 0.
capture = cv2.VideoCapture(0)
After this, we need to create an object of class VideoWriter. This object will allow us to write a video file from the frames captured from the camera.
So, we call the VideoWriter_fourcc function, passing as input the 4 characters of the codec code. It will return an integer representing the codec.
fourcc = cv2.VideoWriter_fourcc('X','V','I','D')
Going back to the VideoWriter object instantiation, it receives the following parameters:
- The name of the file (including the path of the location where you want to save it and the extension);
- An integer representing the fourcc code;
- The frame rate, in frames per second;
- A tuple with the dimensions of the video.
For the first parameter, I will be creating an .avi file called video. You can check the difference between the file format and the video codec here.
As second parameter we will pass the output of the VideoWriter_fourcc function. As third parameter, we will pass a frame rate of 30 frames per second.
As fourth and final parameter I’ll be passing a tuple with the value (640,480), which corresponds to the dimensions of the frame obtained with my camera.
videoWriter = cv2.VideoWriter('C:/Users/N/Desktop/video.avi', fourcc, 30.0, (640,480))
After instantiating all the needed objects, we will start obtaining the frames from the camera in an infinite loop that will break when the user clicks the ESC key.
while (True): #capture and save frames if cv2.waitKey(1) == 27: break
To get a frame, we call the read method on our VideoCapture object. This method takes no arguments and returns a tuple.
The first returned value of the tuple is a Boolean indicating if the frame was read correctly (True) or not (False). The second value is a ndarray representing the frame.
ret, frame = capture.read()
In case the frame was correctly captured, we will show it in a window and also write it to the file. To write the frame to the file, we need to call the write method on our VideoWriter object, passing as input the frame.
if ret: cv2.imshow('video', frame) videoWriter.write(frame)
In case the infinite loop breaks, it means the capture of frames should finish and we should end our program. Thus, we call the release method on the VideoCapture object, to release the camera, and the release method on the VideoWriter object, to close it.
We will also call the destroyAllWindows function, to destroy the window we opened to show the frames. The final code can be seen below.
import cv2 capture = cv2.VideoCapture(0) fourcc = cv2.VideoWriter_fourcc('X','V','I','D') videoWriter = cv2.VideoWriter('C:/Users/N/Desktop/video.avi', fourcc, 30.0, (640,480)) while (True): ret, frame = capture.read() if ret: cv2.imshow('video', frame) videoWriter.write(frame) if cv2.waitKey(1) == 27: break capture.release() videoWriter.release() cv2.destroyAllWindows()
Testing the code
To test the code, simply run it in a tool of your choice. I’ll be using PyCharm, a Python IDE. During the video recording, you should see a window with the obtained frames, like shown in figure 1.
After clicking the ESC button, you should have a file with the video obtained from the camera, like shown in figure 2.