brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: ffprobe -of default=nw=1:nk=1 prints a video's duration as a bare number

Tools
~/tips/tip-ffprobe-duration-bare-number

ffprobe -v error -show_entries format=duration -of default=nw=1:nk=1 input.mp4

Prints only the length of a media file in seconds, with no labels around it.

When a script needs the length of a video, the normal ffprobe output is too chatty. A few writer options cut it down to the single number, which is easy to use in a shell script.

ffprobe sorts what it knows into sections. -show_entries picks the section and the fields, written as section=field, and -of (short for -output_format) chooses the writer. The default writer has two options that trim the layout: nw (noprint_wrappers) drops the section header and footer, and nk (nokey) drops the key in front of each value.

ffprobe -v error -show_entries format=duration -of default=nw=1:nk=1 input.mp4

The result is a line like 1834.512000, the length in seconds, and nothing else. -v error limits the log to errors, so nothing but the value lands in the variable if all goes well. Handy for a variable:

secs=$(ffprobe -v error -show_entries format=duration -of default=nw=1:nk=1 input.mp4)

If you want something readable for a person, the docs list -sexagesimal, which prints time values as HH:MM:SS.MICROSECONDS:

ffprobe -v error -sexagesimal -show_entries format=duration -of default=nw=1:nk=1 input.mp4

For other fields, change the entry. stream=codec_name lists the codec of every stream, and the same options keep it to bare words. The writers also include json, csv, flat, ini and xml when a script wants structure instead of a single value.

A caveat on the number itself: format=duration is the duration of the container as ffprobe reads it, not a count of the frames. For an odd file, a broken download for example, it can be missing or off, so a script should check that it got a value before it divides by it.

Further reading: ffprobe documentation

More Tools tips shuf -n 3 films.txt xmllint --format feed.xml pdftotext -layout invoice.pdf invoice.txt qrencode -t ANSIUTF8 'https://example.com/a/long/path?id=12345' where python All Tools tips (14) All topics

Link