How to get video duration in seconds?

75

31

How can I get video duration in seconds?

What I've tried:

ffmpeg -i file.flv 2>&1 | grep "Duration"
  Duration: 00:39:43.08, start: 0.040000, bitrate: 386 kb/s


mediainfo file.flv | grep Duration
Duration : 39mn 43s

This what close, but it's not so accurate, 2383 is 39.71 minutes

ffmpeg -i file.flv 2>&1 | grep "Duration"| cut -d ' ' -f 4 | sed s/,// | sed 's@\..*@@g' | awk '{ split($1, A, ":"); split(A[3], B, "."); print 3600*A[1] + 60*A[2] + B[1] }'
2383

user2783132

Posted 2013-09-25T19:35:33.800

Reputation: 1 059

Answers

35

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.

Robert Harvey

Posted 2013-09-25T19:35:33.800

Reputation: 1 826

105

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

30.024000

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

0:00:30.024000

Video stream duration

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

The above commands are from FFmpeg Wiki: FFprobe Tips.

With ffmpeg

You may need to completely decode the input if you find the container or stream duration to be missing or inaccurate (possibly due to a damaged or truncated file).

$ 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:

How long this will take will of course depend on the decoding complexity and duration of your input and the capabilities of your computer.

llogan

Posted 2013-09-25T19:35:33.800

Reputation: 31 929

These do not work for .m2v files do you have a solution for them aswell – utdev – 2017-01-05T09:47:50.647

@utdev See the "With ffmpeg" section. – llogan – 2017-01-05T18:39:36.427

Your third solution gives me a wrong time, my video has a length of 01:19:00 but the command returns me time=01:09:15.32, do you have a guess why this is happening – utdev – 2017-02-15T10:59:05.033

Very useful the media stream duration. Thank you. – We are Borg – 2018-04-20T09:40:33.733

For anyone using avprobe, the parameters are slightly different: avprobe -v error -show_format_entry duration .\Sample.mp4 – Brad – 2019-06-18T22:25:34.290

34

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.

evilsoup

Posted 2013-09-25T19:35:33.800

Reputation: 10 085

how can I remove the fraction part of seconds? – Tina J – 2017-08-18T13:42:47.740

@TinaJ Just pipe it with printf, like | xargs printf %.0f - this will return an integer value. – Ilia Rostovtsev – 2018-08-14T09:21:21.773

3

mediainfo --Output='General;%Duration%' file.flv

This outputs the duration in milliseconds as a single integer value. No need for grep/cut/sed/...

Josef Kufner

Posted 2013-09-25T19:35:33.800

Reputation: 141

Thanks - this was just what I was looking for. Can be installed on MacOS with brew install mediainfo – Alex K – 2018-11-22T17:32:54.720

3

Solution with mplayer that gives seconds directly:

mplayer -identify -frames 0 -vo null -nosound file.flv 2>&1 | awk -F= '/LENGTH/{print $2}'

micke

Posted 2013-09-25T19:35:33.800

Reputation: 3 001

3

For my ffmpeg-0.6.5-1.el6.rf.x86_64, to get just the second, ffprobe command format is:

ffprobe <file> -show_format 2>&1 | sed -n 's/duration=//p' 

Sunry

Posted 2013-09-25T19:35:33.800

Reputation: 149

how can I remove the fraction part of seconds? – Tina J – 2017-08-18T13:42:57.903

0

I came across the issue of getting some strange and incorrect metadata from some video files I was working with and I couldn't succeed on finding a pattern or any type of handling using code and tools like ffmpeg, mp4box, ffprobe, mediainfo, mplayer, to get the real duration of the video.

Identifying the real duration of the video was a requirement for a project I was working and the only way I found to get it always right was to reencode the video file using ffmpeg and forcing encoded files to ignore original file's metadata, like:

ffmpeg -i INPUT_FILENAME -acodec copy -vcodec copy -map_metadata -1 OUTPUT_FILENAME"

(This might run faster than you expect. I got surprised, for the type of content and on the environment I was using, average time was 2 seconds)

... and then get duration using a tool at your choice. I like mediainfo, btw: - "mediainfo FILE --Inform="Video;%Duration%" gives you duration in miliseconds.

satirio

Posted 2013-09-25T19:35:33.800

Reputation: 1

You did not provide more information or better solution than other answers – Sam – 2017-06-01T07:36:35.793

0

If you only need to query metadata:

ffprobe -hide_banner -v quiet -show_streams -print_format flat video.mp4

[...]

streams.stream.0.duration="5221.146009"

[...]

So you can parse it:

while read -r; do
  if [[ "$REPLY" =~ ^streams\.stream\.([0-9])+\.duration=\"([^"]+)\"$ ]]; then
    echo -E Duration of stream "${BASH_REMATCH[1]}": "${BASH_REMATCH[2]}"
  fi
done < <(ffprobe -hide_banner -v quiet -show_streams -print_format flat video.mp4)

But if you want to get the effective container's duration, you need to decode it:

AV_LOG_FORCE_NOCOLOR=y ffmpeg -nostdin -hide_banner -nostats -loglevel info -i video.mp4 -f null -c copy - 2>&1 | tail -n 2

It will take some CPU time to decode it, until:

[...]

frame=130527 fps=53271 q=-1.0 Lsize=N/A time=01:27:01.12 bitrate=N/A speed=2.13e+03x
video:152014kB audio:40790kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown

Then, you can parse it:

if [[ "$(AV_LOG_FORCE_NOCOLOR=y ffmpeg -nostdin -hide_banner -nostats -loglevel info -i video.mp4 -f null -c copy - 2>&1 | tail -n 2 | head -n 1)" =~ \ time=([0-9]+):([0-9]{2}):([0-9]{2})\.([0-9]+) ]]; then
  declare duration=0 us="${BASH_REMATCH[4]}" t
  for t in "${BASH_REMATCH[@]:1:3}"; do
    ((duration *= 60))
    ((duration += ${t#0} ))
  done
  while [ ${#us} -lt 6 ]; do us+=0; done
  ((us >= 500000)) && ((duration++))
  ((duration)) || ((duration++))
fi
echo -E Duration: "$duration"

Luchostein

Posted 2013-09-25T19:35:33.800

Reputation: 121

-6

If you want ACCURATE duration, forget FFmpeg. It only gives an estimate based on filesize and average bitrate. I've found, in some case, the estimated duration time off by 2x!

On Linux, if file is created and modified only during the said video recording, an alternate solution would be to use creation and last modification time to calculate duration, as in:

stat -c "%X %Y" video.mpg | awk '{print $2 - $1}'

codechimp

Posted 2013-09-25T19:35:33.800

Reputation: 95

5Where did you find that ffmpeg is inaccurate? – aleb – 2015-04-29T10:55:52.233

4%X is time of last access. %Y is time since last modified. This says nothing about the video length. – Joe Hillenbrand – 2015-10-14T06:41:05.463

1This seems to be a troll – George Chalhoub – 2018-06-11T13:39:35.883