How to find the duration of a video file in Python
Sometimes, you might be required to find the duration of a video. In this tutorial, I will tell you how to find the duration of a video file in Python.
Find the duration of a video file in Python
Today I will tell you about two ways you can find the duration of a video file :
- With openCV.
- With pymediainfo.
- Using moviepy.
With openCV
To enable the usage of openCV, I have imported the package into my code. If you don’t have it installed already, check out this link. I have used the VideoCapture()
function which takes the name of the video file you want to work on. Now I want to retrieve the frame rate and the total number of frames present in the video. Dividing these two values I will get the time duration of the video file in seconds. To get these values I have used the get()
function. However to retrieve the frame rate I have passed CAP_PROP_FPS
as an argument and CAP_PROP_FRAME_COUNT
as an argument to retrieve the total number of frames.
Code :
import cv2 video = cv2.VideoCapture("C:\preview.mp4") frame_rate = video.get(cv2.CAP_PROP_FPS) total_num_frames = video.get(cv2.CAP_PROP_FRAME_COUNT) duration = total_num_frames / frame_rate print(duration)
After getting these values divide the total number of frames by frame rate. Now print, this provides you with the duration of the video file in seconds.
Output :
14.134
With pymediainfo: duration of a video file
First, open your command prompt in Windows or Terminal in MacOS/ Linux. Now install the pymediainfo dependency by running the following command.
pip install pymediainfo
After successfully installing it, make sure to import this package to your code. I have specifically imported the MediaInfo library into my code. Using the parse()
function, I’ve passed the file’s path as an argument and stored it in a temporary variable, video. To get the duration, we have to index the video variable, to get the duration from the first index of tracks. I’ve divided the duration_secs
variable by 1000 to get the secs and the milliseconds separated by a decimal point.
Code :
from pymediainfo import MediaInfo video = MediaInfo.parse("C:\preview.mp4") duration_secs = video.tracks[0].duration / 1000 print(duration_secs)
Now print the duration_secs variable.
Output :
14.134
You know how to find the duration of a video file in Python.
moviepy package to find the duration of a video
Handling the video file in its raw binary format will make things complicated instead, we will use an external library based on Python called moviepy.
The first step is to install moviepy and for that, we will use the package manager, pip. To install moviepy, run the following command in your command prompt.
pip3 install moviepy
This is all you need to proceed with the actual code. The package moviepy is based on ffmpeg, which will be installed as one of the dependencies so you need not worry about it. The library supports most of the common video formats that are currently in use.
To test if the package installed correctly, run the following code on the Python shell.
import moviepy
If you have no errors, you are good to proceed.
Python program to find the duration of a video file
Once we are done with the installation of the packages, we are already done with most of the work. The actual code required for this is extremely short and simple which will be discussed now.
We need to import the moviepy library or specifically the editor class of the moviepy module.
import moviepy.editor
Now, create an object of the VideoFileClip class by referencing the location of the video file as a parameter.
video = moviepy.editor.VideoFileClip("D:\path\to\video.mp4")
We can now access the duration member of the class which will contain the duration of the video file in seconds which can be formatted into our desired format.
video_duration = int(video.duration)
The complete code which only consists of a few lines is mentioned below
import moviepy.editor # Converts into more readable format def convert(seconds): hours = seconds // 3600 seconds %= 3600 mins = seconds // 60 seconds %= 60 return hours, mins, seconds # Create an object by passing the location as a string video = moviepy.editor.VideoFileClip("D:\path\to\video.mp4") # Contains the duration of the video in terms of seconds video_duration = int(video.duration) hours, mins, secs = convert(video_duration) print("Hours:", hours) print("Minutes:", mins) print("Seconds:", secs)
I hope this article was useful in helping you to find the duration of a video file. In case you need to discover more functionalities of the moviepy library, you can refer to its documentation here.
You may also read:
Leave a Reply