How to get video duration in seconds?

Solution 1:

To get minutes, you have to divide 2383 seconds by 60.

39.7167

and then multiply the fractional part .7167 by 60 to get the remaining seconds.

43.002

So it's 39 minutes, 43 seconds. The application appears to be giving you an accurate value.

Solution 2:

Just use ffprobe directly. No need for sed, grep, etc. There are several "durations" you can acquire (depending on your input).

Format (container) duration

ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 input.mp4

Result:

30.024000

Adding the -sexagesimal option will use the HOURS:MM:SS.MICROSECONDS time unit format:

0:00:30.024000

Video stream duration

If you want the duration of a particular video or audio stream:

ffprobe -v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 input.mp4

Result:

30.000000

The above commands are from FFmpeg Wiki: FFprobe Tips.

With ffmpeg

You can use ffmpeg to get duration by decoding the input:

ffmpeg -i input.mp4 -f null -
…
frame= 1587 fps=0.0 q=0.0 Lsize=N/A time=00:01:03.48 bitrate=N/A

In this example time=00:01:03.48 is the duration.

This may take a long time depending on your input file.

Solution 3:

If you have ffmpeg, you should also have ffprobe:

ffprobe -i input.file -show_format | grep duration
ffprobe -i input.file -show_format -v quiet | sed -n 's/duration=//p'

This will also give fractions of seconds, if that's a problem you can further process that away with sed.