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?
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.
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.
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[.
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.
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
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 ,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With