Help Getting The Bitrate of an Audio File

I wanted to make an app to display the bit rate of an audio file.

I can get Automator to pass the list of selected files. I can get the file name printed. I cannot get the bit rate or display it.

I also would like to have a window pop up with the results. Not sure what to do there, haven’t gotten that far.

Here is the script in the do shell script part:

#! /bin/bash
cat
for FILE1 in "$@"
    do 
    FN="${FILE1##*\/}"    
    #echo $FILE1 | sed 's/^.*\//'
    BR=$(afinfo $FILE1 | grep "bit rate")
    echo -e $FN "\t" $BR
done

HELP please.

Hi,

you could retrieve the information with Spotlight metadata


set audioFile to choose file
tell application "System Events"
	set fileKind to kind of audioFile
	set fileName to name of audioFile
end tell
if fileKind contains "audio" then
	set bitRate to do shell script "mdls -name kMDItemAudioBitRate -raw " & quoted form of POSIX path of audioFile
	display dialog "Audio Bit Rate of file " & fileName & " is " & bitRate buttons {"Cancel", "OK"} default button "OK"
else
	display dialog "file " & fileName & " seems to be no audio file"
end if


Thank you!

Even though I came across your post from over 9 years ago I found the script to be useful.

I was wondering if you could modify it to show the bitrate of the selected audio file directly in the Finder as a Quick Action.

This would save me the trouble of having to browse for the file every time I want to check its bitrate.

Thank you in advance for your help.

Hey Timofey,

Personally I’d never fool with a Quick Action, because I use FastScripts and Keyboard Maestro for scripting and automation on my Macs.

** Note – this script operates on the selected file in the Finder instead of using a choose-file dialog.

So the way I’d change the script for my purposes would be something like this:

property LF : linefeed

tell application "Finder"
   set selectedFileList to selection as alias list
   if selectedFileList ≠ {} then
      set audioFile to item 1 of selectedFileList
   else
      error "No files were selected!"
   end if
end tell

tell application "System Events"
   set fileKind to kind of audioFile
   set fileName to name of audioFile
end tell

if fileKind contains "audio" then
   set bitRate to do shell script "mdls -name kMDItemAudioBitRate -raw " & quoted form of POSIX path of audioFile
   display dialog "Audio Bit Rate of file:" & LF & LF & fileName & LF & LF & bitRate buttons {"Cancel", "OK"} default button "OK"
else
   display dialog "file " & fileName & " seems to be no audio file"
end if

An example of creating a Quick Action Script:

Here’s how to write the above as a Quick Action:

--------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2023/04/30 23:11
# dMod: 2023/04/30 23:11 
# Appl: Finder Quick Action
# Task: Get BitRate of Selected Audio File.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, 
--------------------------------------------------------

on run {input}
   
   set LF to linefeed
   
   set audioFile to item 1 of input
   
   tell application "System Events"
      set fileKind to kind of audioFile
      set fileName to name of audioFile
   end tell
   
   if fileKind contains "audio" then
      set bitRate to do shell script "mdls -name kMDItemAudioBitRate -raw " & quoted form of POSIX path of audioFile
      display dialog "Audio Bit Rate of file:" & LF & LF & fileName & LF & LF & bitRate buttons {"Cancel", "OK"} default button "OK"
   else
      display dialog "file " & fileName & " seems to be no audio file"
   end if
   
end run

--------------------------------------------------------

Tested on macOS 10.14.6 Mojave.

-Chris