Get video duration using OpenCV Python


OpenCV is an Open Source Computer Vision Library in python. It provides numerous functions to perform various Image and video processing operations. The library uses the Numpy module to represent all the video frames and images as a ndarray type. It needs the numpy library, we need to make sure that the numpy module is also installed in our python interpreter.

In this article, we will see different ways to get the duration of a video using the python OpenCV module.

The OpenCV provides a VideoCapture.get() method to get the specified property of a video by passing one of the property identifier flags. Following is the syntax of the get() method –

VideoCapture.get(propId)

Where propId is the property identifier. Following is the list of a few property identifiers –

  • CAP_PROP_FRAME_COUNT: This flag gives the property of the number of frames in the video file.

  • CAP_PROP_FPS: this will give the frame rate. (FPS stands for Frames per second).

  • CAP_PROP_POS_MSEC: Current position of the video file in milliseconds or video capture timestamp.

  • CAP_PROP_FRAME_WIDTH: This will give width of the frames in the video stream.

  • CAP_PROP_FRAME_HEIGHT: This will give height of the frames in the video stream.

Here we will use the CAP_PROP_FRAME_COUNT and CAP_PROP_FPS flags To get the duration of a video.

Approach

  • Load a video using cv2.VideoCapture().

  • Get the Number of Frames per Second using the cv2.CAP_PROP_FPS property.

  • And get the total Number of Frames using the cv2.CAP_PROP_FRAME_COUNT property.

  • Calculate the duration in Seconds by dividing the (total number of frames) / (frames per second).

  • Convert the total duration in seconds to the required format.

The below input video of length 01:05 will be used to get the duration in different formats using OpenCV python.

Example

In this example, we will get the video duration in seconds only.

import cv2

# create video capture object
cap = cv2.VideoCapture('Videos/One min video.mov')

# count the number of frames
fps = cap.get(cv2.CAP_PROP_FPS)
totalNoFrames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
durationInSeconds = totalNoFrames // fps

print("Video Duration In Seconds:", durationInSeconds, "s")

Output

Video Duration In Seconds: 65.0 s

Example

In this example, we will find the video duration in minutes and seconds.

import cv2 

def get_dur(filename):
    video = cv2.VideoCapture(filename)
    fps = video.get(cv2.CAP_PROP_FPS)
    frame_count = video.get(cv2.CAP_PROP_FRAME_COUNT)
    seconds = frame_count / fps
    minutes = int(seconds / 60)
    rem_sec = int(seconds % 60)
    return f"{minutes}:{rem_sec}"

print('Video Duration In Seconds:',get_dur("Videos/One min video.mov"))

Output

Video Duration In Seconds: 1:5

Example

Instead of manually converting the duration from seconds to minutes, here we will calculate video duration using the datetime module.

import datetime

def get_dur(filename):
    video = cv2.VideoCapture(filename)
    fps = video.get(cv2.CAP_PROP_FPS)
    frame_count = video.get(cv2.CAP_PROP_FRAME_COUNT)
    seconds = frame_count // fps
    video_time = datetime.timedelta(seconds=seconds)
    return video_time

print('Video Duration In Seconds:',get_dur("Videos/One min video.mov"))

Output

Video Duration In Seconds: 0:01:05

Initially, we have calculated the video duration in seconds by subtracting the total number of frames with the fps(frames per second). Then by using the timedelta() method we converted the video duration from seconds to hours:minutes:secods form.

Updated on: 31-May-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements