How can I find duration of a video file in miliseconds i.e. in integer in deterministic way. I have used ffprobe to get the duration but it doesn't give duration for all file formats.
4 Answers
Use the following commands:
i) To get the duration of video stream:
$ mediainfo --Inform="Video;%Duration%" [inputfile]
ii) To get the duration of the media file:
$ mediainfo --Inform="General;%Duration%" [inputfile]
iii) To get the duration of audio stream only:
$ mediainfo --Inform="Audio;%Duration%" [inputfile]
iv) To get values of more than one parameter:
$ mediainfo --Inform="Video;%Width%,%Height%,%BitRate%,%FrameRate%" [inputfile]
Output would be something like this:
1280,720,3000000,30.0

- 45,586
- 12
- 116
- 142

- 2,338
- 4
- 21
- 36
As offered by iota to use mediainfo --Inform="Video;%Duration%" [inputfile]
, possible but returns weird results.
For example, for video with duration 31s 565ms the output of given command would be:
31565
It wasn't suitable for me and I came up to the following solution:
mediainfo --Inform="Video;%Duration/String3%" inputExample.webm
Returned value is:
00:00:31.565
After all, you could just format returned value with, let's say PHP, to convert it to seconds, e.g.:
$parsed = date_parse( '00:00:31.565' );
echo $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second'];

- 13,086
- 11
- 53
- 88
I use the below command on my xubuntu machine and it does exactly what the OP wants to accomplish.
mediainfo --Output="Video;%Duration%\n" *.mp4 | awk '{ sum += $1 } END { secs=sum/1000; h=int(secs/3600);m=int((secs-h*3600)/60);s=int(secs-h*3600-m*60); printf("%02d:%02d:%02d\n",h,m,s) }'

- 30,962
- 25
- 85
- 135

- 160
- 1
- 11
we can also use ffmpeg to get the duration of any video or audio files.
To install ffmpeg follow this link
import subprocess
import re
process = subprocess.Popen(['ffmpeg', '-i', path_of_media_file], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, stderr = process.communicate()
matches = re.search(r"Duration:\s{1}(?P<hours>\d+?):(?P<minutes>\d+?):(?P<seconds>\d+\.\d+?),", stdout, re.DOTALL).groupdict()
print matches['hours']
print matches['minutes']
print matches['seconds']

- 201
- 2
- 4
-
Nice, it is same as ffprobe which I found, supports lesser formats than mediainfo. And mediainfo is made specifically for these purposes however whole ffmpeg framework is built for transcoding and heavy to install . – Harit Vishwakarma Feb 23 '16 at 09:31