ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 input.mp4
returns:
30.024000
NOTE: The output duration is in seconds.
Adding the -sexagesimal option will use the HOURS:MM:SS.MICROSECONDS time unit format:
0:00:30.024000
Duration of the first video stream:
ffprobe -v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 input.mp4
returns:
30.000000
NOTE: Not all formats, such as Matroska and WebM, store duration at the stream level resulting in duration=N/A.
Get the duration by fully decoding the file.
ffmpeg -i input.mkv -f null -
returns:
... frame=206723 fps=1390 q=-0.0 Lsize=N/A time=00:57:28.87 bitrate=N/A speed=23.2x
NOTE: This will report the correct duration in case the methods shown above using ffprobe are incorrect or missing due to corrupt, truncated, or damaged files.