Calculate Number of frames in video using OpenCV

In this article, we have explained how to calculate Number of frames in video using OpenCV along with the complete Python code.

Table of contents:

  1. Basics of Frames and Video
  2. Calculate Number of frames in video using OpenCV

Basics of Frames and Video

A video is a sequence of images known as frames. FPS is a property of a video which is the number of frames displayed every second. FPS stands for Frames per second.

More the FPS, larger is the size of the video as the number of images displayed in a duration increases. More the FPS, the smoother the transition in the video. Ideally, FPS of video is 50 to 60.

So, if the duration of video is D, then the total number of frames in the video is:

Number of frames = Duration x FPS

So, if the video is of duration 10 minutes (= 600 seconds) with FPS as 60, then the total number of frames is:

Number of frames = 600 x 60 = 36,000

So, the 10 minutes video is equivalent to 36,000 images in this case.

Calculate Number of frames in video using OpenCV

Import OpenCV as we will use it to get the number of frames in a video:

import cv2

Load the video in OpenCV:

video = "video.avi"
cap = cv2.VideoCapture(video1)

The property CAP_PROP_FRAME_COUNT in OpenCV stores the number of frames in a video. Fetching this value will give us the total number of frames in the video:

number_of_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))

Following is the complete Python code to calculate Number of frames in video using OpenCV:

import cv2

video = "video.avi"
cap = cv2.VideoCapture(video1)
number_of_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))

print("Number of frames: ", str(number_of_frames))

Output:

Number of frames: 12000

With this article at OpenGenus, you must have the complete idea of how to Calculate Number of frames in video using OpenCV.