How to Record Video in Python using OpenCV
In this article, we show how to record video in Python using the OpenCV module.
OpenCV allow us to perform a number of image and video functions.
Using OpenCV, we can connect to a webcam or video camera connected to our computer and then once connected, we can later do things such as open up the video-recording device and record and save video.
In this article, we're simply going to connect to the default webcam of our computer.
We then record video. Once the session is closed out using the Esc key.
Below is the code that records and saves a video using a computer's
default webcam.
Let's now go over this code.
First, we import the cv2 module.
Next, we create a variable, video. We assign this variable to, cv2.VideoCapture(0).
The VideoCapture() function allows us to connect to a device such as a webcam on our computer. When 0 is placed in as the parameter to VideoCapture(), this connects to the default webcam of our computer.
Now that we're connected to a webcam on our computer, we can now do things such as open up the webcam and record video.
The next thing we must get is the width and height of the video-recording device. This is needed because we then specify to the VideoWriter() function the dimensions that the video was recorded in.
We next create a variable, writer, which stores the recorded video.
The video is recorded through the VideoWriter() function.
The first parameter is the name of the file. If you want the file saved in the current working directory, you simply specify the file name. If you want the file saved in another directory other than the current working directory, then you must specify the full path including the file name. When specifying the file name, make sure that you include the file extension. In this example, we use mp4.
The second parameter specifies the video codec. The video codec that you choose is platform dependent (on the operating system you are using). However, one codec that works for all platforms is DIVX, so we will use this for our code.
The third parameter is the number of frames per second you want captured while you're recording a video. This number is limited by the video-recording device. Most webcams are capable of at least 30 FPS, so you can specify any number up to 30. The frames per second is the number of images taken per second. Remember that a video is really just several images taken consecutively and then played over time to give video. So the higher the FPS, the more images, so that the greater the file size for the video.
The fourth and last parameter is the width and height of the video.
Now we have a while statement, we read in the video frames.
We then write each frame using the write() function of the writer object.
We then show the image using the cv2.imshow() function.
The next code allows us to exit the video frame if the 'Esc' key is pressed as long as the frame has been open at least 1 millisecond.
We have to make sure that the video capture is released, the video writer is released, and all windows are destroyed.
So once you run the code and then you exit the frame using the Esc
key, you should see a video saved in the directory where you saved it.
And this is how to record a video in Python using the OpenCV module.
Related Resources
How to Draw a Rectangle in Python using OpenCV
How to Draw a Circle in Python using OpenCV
How to Draw a Line in Python using OpenCV
How to Add Text to an Image in Python using OpenCV
How to Display an OpenCV image in Python with Matplotlib
How to Use Callback functions to Connect Images to Events in Python using OpenCV
How to Check for Multiple Events in Python using OpenCV