ffprobe can read a remote file's duration

I needed to get the duration of thousands of MP3s hosted on a remote server. Downloading them just to get one number would be cumbersome. Turns out it can be done with good old ffprobe.

It accepts an HTTP URL instead of a path:

ffprobe -v error -show_entries format=duration -of csv=p=0 "https://example.com/audio/chapter-01.mp3"

It fetches the headers and just enough of the stream to probe the file, without downloading the whole thing.

For MP3s without a Xing/VBRI header ffprobe estimates the duration from from the bitrate and file size. And prints “Estimating duration from bitrate, this may be inaccurate”. For CBR that’s close enough, for VBR it can be off.

In Elixir we can use the ffmpex package. It runs ffprobe -show_format on whatever string you give it, so an URL works too.

A file ffprobe can’t parse comes back as :no_duration, not as an error:

case FFprobe.duration(url) do
  duration when is_float(duration) -> duration
  :no_duration -> nil
  {:error, _reason} -> nil
end

Static ffmpeg binaries in Docker

To get ffmpeg and ffprobe into the image, we don’t need a distro package with its pile of shared libraries. The mwader/static-ffmpeg image contains statically linked binaries, so we can copy them in from a build stage:

Dockerfile
FROM mwader/static-ffmpeg:7.1 AS ffmpeg

FROM debian:bookworm-slim
COPY --from=ffmpeg /ffmpeg /ffprobe /usr/local/bin/

Because the binaries are static, the same two lines work on both Alpine (musl) and Debian (glibc) images.

← All notes