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

Gyan Doshi ffmpeg at gyani.pro
Wed Jan 19 13:40:26 EET 2022

On 2022-01-19 04:53 pm, Bo Berglund wrote:
> This is on Linux:
> I have tried in various ways to limit the output of ffmpeg/ffprobe to the line
> holding the duration but it seems impossible...
> I have a need to display the video duration as h:m:s on a listing of videos in a
> directory.
> So I want to create a small script to be called with the video file as argument
> and the output should be the video duration formatted as hh:mm:ss
> So I have tried various ways of using ffmpeg and ffprobe to extravt the data
> without much success.
> Whatever I do seems to create too much output like this following a
> StackExchange suggestion:
> ffprobe -hide_banner -i input.mp4 | grep Duration
> Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input.mp4':
>    Metadata:
>      major_brand     : isom
>      minor_version   : 512
>      compatible_brands: isomiso2avc1mp41
>      encoder         : Lavf58.45.100
>    Duration: 00:43:26.38, start: 0.000000, bitrate: 452 kb/s
>      Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 852x480
> [SAR 640:639 DAR 16:9], 348 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
>      Metadata:
>        handler_name    : VideoHandler
>      Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo,
> fltp, 94 kb/s (default)
>      Metadata:
>        handler_name    : SoundHandler
> I had assumed that the output would be limited to the requested duration but it
> seems like it spews out all other data no matter what I do.
> The 7th line only is what I expected out of grep but by some mysterious reason a
> lot more clobbers the output.
> What do I need to just get that output I want?

The closest you can get is

      ffprobe -v 0 -of compact=p=0:nk=1 -sexagesimal -show_entries 
format=duration INPUT


More information about the ffmpeg-user mailing list