[FFmpeg-user] How to get video duration in a script using ffmpeg?

Reino Wijnsma rwijnsma at xs4all.nl
Thu Jan 20 02:22:28 EET 2022


On 2022-01-19T12:23:07+0100, Bo Berglund <bo.berglund at gmail.com> wrote:
> ffprobe -hide_banner -i input.mp4 | grep Duration
ffprobe -hide_banner input.mp4 2>&1 | grep Duration

- 2>&1, because ffprobe (as well as ffmpeg) send the information to stderr.
- Unlike ffmpeg, -i isn't needed for ffprobe and ffplay.

On 2022-01-19T13:56:41+0100, Bo Berglund <bo.berglund at gmail.com> wrote:
> On Wed, 19 Jan 2022 12:59:45 +0100, Bo Berglund <bo.berglund at gmail.com> wrote:
>> Thanks, this returns the following output:
>> 0:43:26.384000
> [...]
>
> Results in this output:
> 0:43:26
>
> Exactly what is needed...

Is it? What if ffprobe returned 0:43:26.584000 (or 2606.584000 without -sexagesimal) instead? I'd figure you'd then want to end up with 00:43:27, right?

$ ffprobe -hide_banner -v 0 -show_entries format=duration -of compact=p=0:nk=1 input.mp4
2606.584000

$ ffprobe -hide_banner -v 0 -show_entries format=duration -of compact=p=0:nk=1 input.mp4 | awk '{print int($1 + 0.5)}'
2607

$ date -d@$(ffprobe -hide_banner -v 0 -show_entries format=duration -of compact=p=0:nk=1 input.mp4 | awk '{print int($1 + 0.5)}') -u '+%H:%M:%S'
00:43:27

Alternatively you can also use Xidel for this.

$ ffprobe -hide_banner -v 0 -show_entries format=duration -of compact=p=0:nk=1 input.mp4 | \
  xidel -se 'format-time($raw * duration("PT1S") + duration("PT0.5S"),"[H01]:[m01]:[s01]")'
00:43:27

$ ffprobe -hide_banner -v 0 -show_entries format=duration -of compact=p=0:nk=1 input.mp4 | \
  xidel -se 'format-time(($raw + 0.5) * duration("PT1S"),"[H01]:[m01]:[s01]")'
00:43:27

>From the JSON:

$ ffprobe -hide_banner -v 0 -show_format -of json input.mp4 | \
  xidel -se 'format-time(($json/format/duration + 0.5) * duration("PT1S"),"[H01]:[m01]:[s01]")'
00:43:27

Or from the initial log:

$ ffprobe -hide_banner input.mp4 2>&1 | \
  xidel -se 'format-time(time(extract($raw,"Duration: (.+?),",1)) + duration("PT0.5S"),"[H01]:[m01]:[s01]")'
00:43:27

-- 
Reino



More information about the ffmpeg-user mailing list