Get video duration using Python - OpenCV Last Updated : 03 Jan, 2023 Comments Improve Suggest changes Like Article Like Report OpenCV is one of the most popular cross-platform libraries and it is widely used in Deep Learning, image processing, video capturing, and many more. In this article, we will learn how to get the duration of a given video using python and computer vision. Prerequisites: Opencv moduledatetime moduleInstallation Opencv can be downloaded by running the given command on the terminal: pip install OpencvApproach To get the duration of a video, the following steps has to be followed: Import required modules.Create a VideoCapture object by providing video URL to VideoCapture() method. Syntax: VideoCapture("url")Count the total numbers of frames and frames per second of a given video by providing cv2.CAP_PROP_FRAME_COUNT and cv2.CAP_PROP_FPS to get() method.Calculate the duration of the video in seconds by dividing frames and fps.Also, calculate video time using timedelta() method. Syntax: timedelta(time) Implementation: Python3 # import module import cv2 import datetime # create video capture object data = cv2.VideoCapture('C:/Users/Asus/Documents/videoDuration.mp4') # count the number of frames frames = data.get(cv2.CAP_PROP_FRAME_COUNT) fps = data.get(cv2.CAP_PROP_FPS) # calculate duration of the video seconds = round(frames / fps) video_time = datetime.timedelta(seconds=seconds) print(f"duration in seconds: {seconds}") print(f"video time: {video_time}") Output : duration in seconds: 28 video time: 0:00:28 Comment More infoAdvertise with us Next Article Get video duration using Python - OpenCV abhijitmahajan772 Follow Improve Article Tags : Technical Scripter Python Technical Scripter 2020 OpenCV Python-OpenCV +1 More Practice Tags : python Similar Reads Creating a Slow Motion Video Using OpenCV - Python In this article, we will try to create a slow-motion video using OpenCV( Open Source Computer Vision) library in Python. OpenCV ( is an open-source computer vision and machine learning software library. Now a video is basically a set of moving pictures moving at a rate of about 24 frames per second 4 min read Python | Play a video using OpenCV OpenCV (Open Source Computer Vision) is a computer vision library that contains various functions to perform operations on Images or videos. OpenCV library can be used to perform multiple operations on videos. Letâs see how to play a video using the OpenCV Python. To capture a video, we need to crea 2 min read Python | Create video using multiple images using OpenCV Creating videos from multiple images is a great way for creating time-lapse videos. In this tutorial, weâll explore how to create a video from multiple images using Python and OpenCV. Creating a video from images involves combining multiple image frames, each captured at a specific moment in time, i 5 min read Display date and time in videos using OpenCV - Python OpenCV-Python is a library of Python bindings designed to solve computer vision problems. It can process images and videos to identify objects, faces, or even the handwriting of a humanNote: For more information, refer to Introduction to OpenCV Display date and time in videos It sometimes becomes n 2 min read Pafy - Getting Duration of the video In this article we will see how we can get the duration of the given youtube video in pafy. Pafy is a python library to download YouTube content and retrieve metadata. Pafy object is the object which contains all the information about the given video. Duration of the video is the time period of the 2 min read Converting Color video to grayscale using OpenCV in Python OpenCV is a huge open-source library for computer vision, machine learning, and image processing. It can process images and videos to identify objects, faces, or even the handwriting of a human. In this article, we will see how to convert a colored video to a gray-scale format. Approach: Import the 1 min read Python OpenCV - resizeWindow() Function resizeWindow() method in Python OpenCV is used to resize window displaying images/videos to a specific size. The specified window size is for images excluding toolbars. This only works for created windows having flags other than CV_WINDOW_AUTOSIZE. Syntax: cv2.resizeWindow(window_name, width, height 1 min read Animate image using OpenCV in Python In this article, we will discuss how to animate an image using python's OpenCV module. Let's suppose we have one image. Using that single image we will animate it in such a way it will appear continuous array of the same image. This is useful in animating the background in certain games. For example 3 min read Python OpenCV - selectroi() Function In this article, we are going to see an interesting application of the OpenCV library, which is selectROI(). With this method, we can select a range of interest in an image manually by selecting the area on the image. Syntax: cv2.selectROI(Window_name, source image) Parameter: window_name:  name of 3 min read Python OpenCV - waitKey() Function waitkey() function of Python OpenCV allows users to display a window for given milliseconds or until any key is pressed. It takes time in milliseconds as a parameter and waits for the given time to destroy the window, if 0 is passed in the argument it waits till any key is pressed. Example: Pythonim 2 min read Like