Get .mp4 file length Python

2.8k views Asked by At

I want to get the video length metadata of .mp4 files, using either a module that comes with Python or a module available on PyPi and doesn't require external software. I've searched all over the internet, and all I could find was either modules that don't support .mp4 files, outdated and non-functioning modules, and modules that require external software.

It only has to be able to run on Windows*

1

There are 1 answers

4
Mureinik On

You could use moviepy.

First, install it using pip:

$ pip install --user moviepy

Then, from python, make sure you have the ffmpeg binary installed (only required once, but harmless if you call it multiple times):

>>> import imageio
>>> imageio.plugins.ffmpeg.download()

Then you can use VideoFileClip to query and manipulate the file:

>>> clip = VideoFileClip('/path/to/file.mp4')
>>> duration_in_sec = clip.duration
>>> duration_in_sec
14.0