How to Save a Video File in Python using OpenCV


In this article, we show how to save a video file in Python using the OpenCV module.

OpenCV gives us great functionality to work with images and videos.

We can do many things to videos with OpenCV, such as even creating them such as through recording with a webcam.

This video can then be saved so that you can store it somewhere such as on your PC or in a directory on your website.

The way the video is saved is through the VideoWriter() function.

We use the VideoWriter() function to create a writer object.

This writer object can then be used to write frames from the video to the writer object.

When all is done, we simply have to release the writer object, so that all writing is halted.

Let's see how this works in actual code.

Below we save a video that we record with the default webcam of the computer used.

Let's now go over this code.

First we import the cv2 module to use OpenCV.

Next, we create a variable video, which gets set to the default webcam of our computer.

We get the width and height of the video because this is then needed for the VideoWriter() function to create a writer object.

We then create a writer object, which is the heart of saving a video file.

With the cv2.VideoWriter() function, with the first parameter, we choose where the file will be saved and what its name will be, along with the type of file it will be. In this case, it will be an mp4 video file. If saving in the current working directory, you will simply specify the name and type of video file.

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.

Next we run a while loop that continually reads in the video.

Each frame that it reads in, we use the writer object to write the frame to it.

Lastly, when we want to end the video, we press the Esc key, which halts the video and also halts the writing to our video file that we've created, which is done with the release() function.

And this is how we can save 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

HTML Comment Box is loading comments...