Get Media Duration using FFmpeg

Get Media Duration using FFmpeg

FFmpeg provides several command-line tools for examining multimedia files and extracting information from their streams. Media duration is one of the most useful properties when analyzing a video or audio file, especially for verifying file characteristics, calculating playback length, or processing media programmatically. This tutorial shows how to get media duration using FFmpeg.

Download a sample file for testing:

curl -sSo test.mp4 https://raw.githubusercontent.com/mediaelement/mediaelement-files/master/big_buck_bunny.mp4

The ffprobe utility can read a media file and display its total duration. Use the following command to extract the duration:

ffprobe -v error -show_entries format=duration -of csv=p=0 test.mp4

The command produces:

60.095011

The value represents the media duration in seconds. For a more readable result, ffprobe can display the duration in HH:MM:SS style notation by using the -sexagesimal option:

ffprobe -v error -show_entries format=duration -of csv=p=0 -sexagesimal test.mp4

The output is:

0:01:00.095011

Command breakdown:

  • ffprobe - inpects the media file and extracts its metadata.
  • -v error - suppresses informational output, displaying only errors, if any.
  • -show_entries format=duration - requests the duration value from the file metadata.
  • -of csv=p=0 - outputs only the requested value without the field name or extra formatting.
  • -sexagesimal - changes the duration representation from seconds to a time-based format.
  • test.mp4 - specifies the media file to analyze.

Leave a Comment

Cancel reply

Your email address will not be published.