Faster video file FPS with cv2.VideoCapture and OpenCV
Last Updated :
24 Apr, 2025
Sometimes we need to speed up the FPS(Frame Per Second) of a video file to perform certain operations on the video such as surveillance footage analysis, video editing, and video game recording. In this article, we will learn to increase the FPS of our input video and download the resultant output video with the help of cv2.VideoCapture in Python.
What is cv2.VideoCapture
The cv2.VideoCapture is a class in the OpenCV library that allows us to capture video from different sources, and perform various tasks on the video such as reading, processing, and writing on video files.
Fast a Video FPS using cv2.VideoCapture
To increase the video FPS in Python, we will use cv2.VideoCapture function of the OpenCV library. This can be done by reading the frames of the input video at a certain FPS and writing them to an output video file at a higher FPS. Lets us first understand what is their functions and how can they increase a video's FPS.
Steps to Increase a Video FPS
Now, we will see the following steps to covert FPS, for that we will use the following video.
Input Video:
Step 1: Importing the required libraries
Imports the required library that is, the OpenCV library. Then import cv2_imshow from the Google Colab patches library, allowing us to display videos in a Colab notebook.
import cv2
from google.colab import files
from google.colab.patches import cv2_imshow
Step 2: Loading the video file
The cv2.VideoCapture() function is used to open the video file. This function takes the path of the input file as the parameter.
cap = cv2.VideoCapture('path_of_input_video_file')
Step 3: Getting the FPS of the video
The get() function is used to retrieve the current frames per second (FPS) of the video.
fps = cap.get(cv2.CAP_PROP_FPS)
Step 4: Defining the output file name and Required FPS
Then we will define the name of the output video file with the '.mp4' extension and set the FPS of the output video to twice the FPS of the input video.
output_file = 'output_file.mp4'
output_fps = fps * 2
Step 5: Creating a VideoWriter object
Then define the video code, that is to be used in the output video as "mp4v" and create a VideoWriter object that will be used to write frames to the output video file. The parameters of the VideoWriter object are the name of the output file.
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_file, fourcc, output_fps, (int(cap.get(3)), int(cap.get(4))))
Step 6: Converting Video FPS
Next, we will read each frame of the input video file using the read() function and write it to the output video file using the write() function. The while loop continues until all frames have been processed. The "ret" variable checks if a frame has been successfully read from the video file.
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
out.write(frame)
Step 7: Releasing the resources and closing the video files
The last step is to release the resources of the input and output video files and close them. The cv2.destroyAllWindows() function closes any windows that were created during the execution of the script.
cap.release()
out.release()
cv2.destroyAllWindows()
Step 8: Downloading the output video file
We can also download the output video file to the local machine using the following code. This part is optional and you may skip it.
from google.colab import files
files.download(output_file)
Complete Code:
Python3
# import modules
import cv2
from google.colab import files
from google.colab.patches import cv2_imshow
# open file
cap = cv2.VideoCapture('/content/video.mp4')
# get FPS of input video
fps = cap.get(cv2.CAP_PROP_FPS)
# define output video and it's FPS
output_file = 'output.mp4'
output_fps = fps * 2
# define VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_file, fourcc, output_fps,
(int(cap.get(3)), int(cap.get(4))))
# read and write frams for output video
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
out.write(frame)
# release resources
cap.release()
out.release()
cv2.destroyAllWindows()
# download output video on local machine
files.download(output_file)
Output:
The video with faster FPS will be downloaded on your local machine.
Note: The above code is written in Google Colab Notebook, however, we can also use the same in your regular PythonIDE. For that follow the below-given steps:
- Open the command prompt or terminal and run the following command: pip install opencv-python-headless. This will install the OpenCV package that is required to run this code.
- Modify the input and output file path, with the actual path of the file in your local machine, with the actual path where you want to save the output file.
- Finally, remove the following line of code from google.colab.patches import cv2_imshow. This is a Colab-specific package and is not required in PyCharm, or any other IDEs you are using.
Similar Reads
Save frames of live video with timestamps - Python OpenCV
Images are very important data nowadays from which we can obtain a lot of information which will helpful for analysis. Sometimes data to be processed can be in the form of video. To process this kind of data we have to read video extract frames and save them as images. Security camera videos we have
3 min read
Saving key event video clips with OpenCV
OpenCV is an open-source and popular computer vision library that contains several computer vision algorithms. You can read, display, write and do lots of other operations on images and videos using OpenCV. The OpenCV module is generally used in popular programming languages like C++, Python, and Ja
5 min read
Face Detection using Python and OpenCV with webcam
Face detection is a important application of computer vision that involves identifying human faces in images or videos. In this Article, we will see how to build a simple real-time face detection application using Python and OpenCV where webcam will be used as the input source.Step 1: Installing Ope
3 min read
How to get properties of Python cv2.VideoCapture object ?
Let us see how we can get the properties from the cv2.VideoCapture objects and understand how they work. cv2.VideoCapture is a function of openCV library(used for computer vision, machine learning, and image processing) which allows working with video either by capturing via live webcam or by a vide
3 min read
Python OpenCV: Optical Flow with Lucas-Kanade method
Prerequisites: OpenCV OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human
3 min read
Blur and anonymize faces with OpenCV and Python
In this article, we are going to see how to Blur and anonymize faces with OpenCV and Python. For this, we will be using Cascade Classifier to detect the faces. Make sure to download the same, from this link: haarcascade_frontalface_default.xml ApproachFirstly, we use a built face detection algorithm
4 min read
MoviePy â Getting Width of Video File Clip
In this article we will see how we can get the width of the video file in MoviePy. MoviePy is a Python module for video editing, which can be used for basic operations on videos and GIF's. Width is the part of the dimensions of the video, width is basically the base of the video. We can load the vid
2 min read
MoviePy â FPS of Video File Clip
In this article we will see how we can get the fps i.e frame per second(frame rate) of the video file in MoviePy. MoviePy is a Python module for video editing, which can be used for basic operations on videos and GIF's. Frame rate is the speed at which those images are shown, or how fast you âflipâ
2 min read
Python OpenCV: Capture Video from Camera
Python provides various libraries for image and video processing. One of them is OpenCV. OpenCV is a vast library that helps in providing various functions for image and video operations. With OpenCV, we can capture a video from the camera. It lets you create a video capture object which is helpful
4 min read
Feature detection and matching with OpenCV-Python
In this article, we are going to see about feature detection in computer vision with OpenCV in Python. Feature detection is the process of checking the important features of the image in this case features of the image can be edges, corners, ridges, and blobs in the images. In OpenCV, there are a nu
5 min read