Open In App

Faster video file FPS with cv2.VideoCapture and OpenCV

Last Updated : 05 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads