Code:
#!/bin/bash
set -e
# use this to recursively search directories
# will not work if filenames have spaces!
for FILENAME in $(find . -name "*.mp3"); do
# test if audio file already has bitrate in filename?
# use 'sed' to test the three characters before the extension
# if they are numeric characters or not.
while [[ `echo $FILENAME | sed 's/^.*\(...\)\..*/\1/'` != *[0-9]* ]]
do
break
done
if [[ `echo $FILENAME | sed 's/^.*\(...\)\..*/\1/'` != *[0-9]* ]]; then
BASENAME="${FILENAME%.[^.]*}"
EXT="${FILENAME:${#BASENAME} + 1}"
# id3info does NOT read VBR correctly!
# vbrfix improves bitrate accuraccy but not for every file
BITRATE=`id3info "$FILENAME" | grep "Bitrate" | cut -c 10-12`
# this conditional only works for id3info
if [[ $BITRATE = ??K ]]; then
# Bitrate is less than 100
BITRATE=0${BITRATE:0:2}
fi
echo $BASENAME.$BITRATE.$EXT
# uncomment to actually rename files
#mv -Tv "$FILENAME" "$BASENAME.$BITRATE.$EXT"
fi
done
enjoy!