Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the duration of a video in Python?

Tags:

python

video

I need to get the video duration in Python. The video formats that I need to get are MP4, Flash video, AVI, and MOV... I have a shared hosting solution, so I have no FFmpeg support.

like image 437
eos87 Avatar asked Oct 02 '10 04:10

eos87


2 Answers

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) 
like image 92
SingleNegationElimination Avatar answered Oct 23 '22 16:10

SingleNegationElimination


As reported here https://www.reddit.com/r/moviepy/comments/2bsnrq/is_it_possible_to_get_the_length_of_a_video/

you could use the moviepy module

from moviepy.editor import VideoFileClip clip = VideoFileClip("my_video.mp4") print( clip.duration ) 
like image 35
mobcdi Avatar answered Oct 23 '22 16:10

mobcdi