How to get the duration of a video in Python?

You can use the external command ffprobe for this. Specifically, run this bash command from the FFmpeg Wiki:

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)

Leave a Comment