Last Updated : 11 Aug, 2025
Determining a video’s total duration is a key step in video processing. With OpenCV in Python, we can retrieve the total frame count and frame rate (FPS) to calculate the duration and use the datetime module to present it in a human-readable format (hours, minutes, seconds).
InstallationPrerequisites:
You can install OpenCV using pip:
Approachpip install opencv-python
To get the duration of a video, the following steps has to be followed:
import cv2
import datetime
video = cv2.VideoCapture('C:/Users/Asus/Documents/videoDuration.mp4')
frames = video.get(cv2.CAP_PROP_FRAME_COUNT)
fps = video.get(cv2.CAP_PROP_FPS)
seconds = round(frames / fps)
video_time = datetime.timedelta(seconds=seconds)
print(f"Frames: {frames}")
print(f"FPS: {fps}")
print(f"Duration in seconds: {seconds}")
print(f"Video time (HH:MM:SS): {video_time}")
Output:
Frames: 840.0
FPS: 30.0
Duration in seconds: 28
Video time (HH:MM:SS): 0:00:28
Explanation:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4