Using a Regular Expression to Isolate the Bitrate from ffprobe Data

I commonly want to find the bitrate of the selected MP3 file in finder. My current solution is to drag the file into terminal, use the command ffprobe, and see the bitrate there.

I think this could be automated quite easily with Keyboard Maestro. However the final step is to take just the bitrate from ffprobe's messy output. Here is an example:

 ffprobe version 4.4 Copyright (c) 2007-2021 the FFmpeg developers
  built with Apple clang version 11.0.0 (clang-1100.0.33.17)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.4_2 --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-avresample --enable-videotoolbox
  libavutil      56. 70.100 / 56. 70.100
  libavcodec     58.134.100 / 58.134.100
  libavformat    58. 76.100 / 58. 76.100
  libavdevice    58. 13.100 / 58. 13.100
  libavfilter     7.110.100 /  7.110.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  9.100 /  5.  9.100
  libswresample   3.  9.100 /  3.  9.100
  libpostproc    55.  9.100 / 55.  9.100
[mp3 @ 0x7faa72800000] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from '/Users/zacharytodd/Documents/Sample File.mp3':
  Metadata:
    genre           : Spoken Word
    encoded_by      : Fission
    TLEN            : 7853479
  Duration: 02:10:53.48, start: 0.000000, bitrate: 64 kb/s
  Stream #0:0: Audio: mp3, 44100 Hz, mono, fltp, 64 kb/s
  Stream #0:1: Video: png, pal8(pc), 1480x1480, 90k tbr, 90k tbn, 90k tbc (attached pic)
    Metadata:
      comment         : Other

I only want the section that reads "bitrate" and all the way to the end of that particular line. In the above example I would get "bitrate: 64 kb/s". I have tried to filter the output with a regular expression "bitrate$" but have not had luck so far.

Hey there, if the output is consistently the same format (particularly the lines starting with Duration and Stream), then this simple RegEx should work for you.

\d{1}.\d{6}, (.*)\n

The simple macro below extracts the appropriate information when I run it. Test it and see if it works for you.

-Chris

Search for bitrate using Regular Expression.kmmacros (3.6 KB)

Personally, I'd use an even simpler regex

(bitrate\: .*)

Thanks, that did the trick!

Hey Guys,

Here's what is probably the simplest way to do that, although you might not like the output format of mdls.

-Chris


Finder ⇢ Get Bitrate of Selected MP3 File v1.00.kmmacros (5.0 KB)

Macro-Image

Keyboard Maestro Export

2 Likes

Oh, you can get specific items with ffprobe too:

ffprobe -v error -show_entries stream=bit_rate -of default=noprint_wrappers=1 <YourFilePath>

-Chris

3 Likes

Thank you Chris. This seems much more efficient than using ffprobe's messy output. I fell victim to the XY problem.

2 Likes

Thanks for the link – a good summary on that page ...

and the classic symptom of a foot caught in the xy trap is a thread title involving the phrases (Regex|Regular Expression).