Skip to content

Instantly share code, notes, and snippets.

@haplo
Created July 1, 2010 20:18
Show Gist options
  • Save haplo/460514 to your computer and use it in GitHub Desktop.
Save haplo/460514 to your computer and use it in GitHub Desktop.
Transform mp3 files to CBR 128
#!/bin/bash
# This script converts all MP3 files in the current directory to a
# constant bitrate (128 in this case) using lame. I use this to make
# a more compact version of my music library to fit in a CD for my car.
#
# WARNING: search is recursive and files WILL BE OVERWRITTEN. You probably
# want to make a copy of the files to be converted.
#
# Notes:
# * id3 tags are lost when converting with lame.
# * Files are always picked from the current directory, it would be better
# to receive the directory as an argument.
BITRATE=128
IFS="
";
for file in `find . -type f -iname '*.mp3' -print0 \
| xargs -0 mp3info -x -r v 2>/dev/null \
| grep File: \
| cut -d ' ' -f 2-`; do
if mp3info -x -r v "$file" 2>/dev/null | grep kbps | grep -v "128 kbps" >/dev/null
then
name=`echo "$file" | awk -F "*.mp3" '{ print $1 }'`
lame -b $BITRATE -h "$file" "$name-new.mp3"
if [ $? ]; then
mv "$name-new.mp3" "$file"
else
echo "Error processing $file, left unaltered." > /dev/stderr
rm "$name-new.mp3"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment