Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract duration time from ffmpeg output?

Tags:

linux

bash

ffmpeg

To get a lot of information about a media file one can do

ffmpeg -i <filename> 

where it will output a lot of lines, one in particular

Duration: 00:08:07.98, start: 0.000000, bitrate: 2080 kb/s 

I would like to output only 00:08:07.98, so I try

ffmpeg -i file.mp4 | grep Duration| sed 's/Duration: \(.*\), start/\1/g' 

But it prints everything, and not just the length.

Even ffmpeg -i file.mp4 | grep Duration outputs everything.

How do I get just the duration length?

like image 280
Louise Avatar asked Jun 04 '11 20:06

Louise


People also ask

How do I get video duration in FFmpeg?

Getting the Duration To get the duration with ffprobe , add the -show_entries flag and set its value to format=duration . This tells ffprobe to only return the duration. To convert the above value into minutes and seconds, round it to an integer and divide it by 60.

How can I get video duration in PHP using FFmpeg?

Show activity on this post. $videoFile = 'addicted. mp4'; $duration = $ffprobe ->streams($videoFile) ->videos() ->first() ->get('duration'); echo $duration; Why I can't receive video information? @VipulJethva you probably need absolute path to the video file.

How do I set duration in FFmpeg?

Use the -t option to specify a time limit: `-t duration' Restrict the transcoded/captured video sequence to the duration specified in seconds. hh:mm:ss[.

Is Ffprobe part of FFmpeg?

The FFmpeg library, ffprobe, can rightly be called the Swiss Army Knife of video information extraction or video inspection. As the FFmpeg documentation succinctly puts it, ffprobe gathers information from multimedia streams and prints it in human- and machine-readable fashion.


2 Answers

You can use ffprobe:

ffprobe -i <file> -show_entries format=duration -v quiet -of csv="p=0" 

It will output the duration in seconds, such as:

154.12 

Adding the -sexagesimal option will output duration as hours:minutes:seconds.microseconds:

00:02:34.12 
like image 119
Ivan Neeson Avatar answered Sep 19 '22 20:09

Ivan Neeson


ffmpeg is writing that information to stderr, not stdout. Try this:

ffmpeg -i file.mp4 2>&1 | grep Duration | sed 's/Duration: \(.*\), start/\1/g' 

Notice the redirection of stderr to stdout: 2>&1

EDIT:

Your sed statement isn't working either. Try this:

ffmpeg -i file.mp4 2>&1 | grep Duration | awk '{print $2}' | tr -d , 
like image 45
Louis Marascio Avatar answered Sep 18 '22 20:09

Louis Marascio