Problem Formulation: Given a video file. How to get its duration in seconds using Python code?
The video can have any of the following video formats:
Method 1: Using Subprocess + ffprobe
Within your Python shell, you can run any external command with the module subprocess. The external tool ffprobe helps you determine many video statistics such as the length of the video.
“ffprobe gathers information from multimedia streams and prints it in human- and machine-readable fashion.”
import subprocess def get_length(filename): result = subprocess.run(["ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", filename], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) return float(result.stdout)
The code is obtained from here and here.
You can download ffprobe for Windows, macOS, and Linux here.
Method 2: Using the moviepy Module
This code is even more concise—and it retrieves the duration of a given video very quickly using the
from moviepy.editor import VideoFileClip clip = VideoFileClip("my_video.mp4") print(clip.duration)
This code is obtained from here. However, it is reported that method 1 with
ffprobe is 10x faster than the method with
Method 3: Using the pymediainfo Module
An alternative to the previous method is provided by the
from pymediainfo import MediaInfo clip_info = MediaInfo.parse('my_video.mov') duration_ms = clip_info.tracks.duration
This code is obtained from here and here.
Method 4: Using the opencv Module
One of the fastest method to retrieve the duration of a video is provided by the
import cv2 video = cv2.VideoCapture('my_video.webm') duration = video.get(cv2.CAP_PROP_POS_MSEC)
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.