Get video duration using Python - OpenCV Last Updated : 11 Aug, 2025 Comments Improve Suggest changes Like Article Like Report 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).Prerequisites: Opencv moduledatetime moduleInstallationYou can install OpenCV using pip:pip install opencv-pythonApproachTo get the duration of a video, the following steps has to be followed:Import Modules : Use cv2 for video processing and datetime.timedelta for formatting.Load Video : Create a cv2.VideoCapture("path/to/video.mp4") object.Get Properties : Extract total frames (CAP_PROP_FRAME_COUNT) and FPS (CAP_PROP_FPS).Calculate Duration : Duration = total frames ÷ FPS, then format using datetime.timedelta.Implementation Python 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.0FPS: 30.0Duration in seconds: 28Video time (HH:MM:SS): 0:00:28Explanation:cv2.VideoCapture('path') creates a capture object for the video file.video.get(cv2.CAP_PROP_FRAME_COUNT) retrieves the total number of frames.video.get(cv2.CAP_PROP_FPS) fetches the frame rate (frames per second).round(frames / fps) computes the total duration in seconds.datetime.timedelta(seconds=seconds) converts seconds into HH:MM:SS format. Comment More infoAdvertise with us A abhijitmahajan772 Follow Improve Article Tags : Technical Scripter Python Technical Scripter 2020 OpenCV Python-OpenCV +1 More Practice Tags : python Explore Python FundamentalsPython Introduction 3 min read Input and Output in Python 4 min read Python Variables 6 min read Python Operators 5 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 7 min read Python Functions 8 min read Recursion in Python 6 min read Python Lambda Functions 6 min read Python Data StructuresPython String 6 min read Python Lists 6 min read Python Tuples 6 min read Dictionaries in Python 7 min read Python Sets 10 min read Python Arrays 9 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 6 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 11 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 7 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 10 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like