I love music, and always want more. (Check out MagnaTune, AmieStreet and EMusic for fabulous, non-Apple music stores, and Jamendo for an amazing collection of free Creative Commons licensed music). From these sites I have a ton of music in both MP3 and Ogg Vorbis formats.
The more music you have, the more likely it is you’ll want to replay-gain it, probably in album mode – that is, make it so that all albums appear to be at the same volume when playing. Differences between songs in an album are preserved, so Princess Leia’s theme will be soft and delicate, while the Imperial March will blast you out of your seat. But you won’t have to constantly adjust the volume between albums any more, just because John Williams records proper music and the latest Britney Spears album is loudness-compressed to hell and back. Read the link above to see what I’m talking about, and how it works.
There are a lot of graphical tools to do this on Windows, look for example at the excellent Foobar music player. There is at least one for Linux too, the ExFalso tool included with the QuodLibet music player, installable under Ubuntu with a simple sudo aptitude install exfalso.
But when you have thousands of songs, spread over hundreds of directories, this can become quite a chore. Scripting to the rescue! It’s a slight bit tricky though, since on the command line you have to use mp3gain -a -k *.mp3 to adjust MP3 files, and vorbisgain -a -f *.ogg to do the same for Ogg files. And on top of that, mp3gain only writes APE tags, not ID3 tags which are recognized by many more players.
Luckily I found a good solution at http://zuttobenkyou.wordpress.com/2009/03/26/adding-replay-gain-in-linux-automatically/. There is a bash script that will go recursively through your folders, looking for music files, and also a Python script called by the former script that will copy mp3gain’s APE tags to an ID3v2 tag section in the file. The only problem I had with this setup was that it would only work on MP3 files, while I wanted to have the script work on both formats, so I could just fire and forget the script.
I created my own bash script that looks like this:
#!/bin/bash
originaldir=$(pwd)
#echo $originaldir
find -type d -print0 | while read -d $'' dir ; do
echo -n $dir
files=$(ls "$dir"/*.ogg 2> /dev/null | wc -l)
if [ "$files" != "0" ]
then
echo " ...ogg!"
cd "$dir"
vorbisgain -a -f *.ogg
cd "$originaldir"
else
files=$(ls "$dir"/*.mp3 2> /dev/null | wc -l)
if [ "$files" != "0" ]
then
echo " ...mp3!"
cd "$dir"
mp3gain -a -k *.mp3
rg-ape-to-id3.py *.mp3
cd "$originaldir"
else
echo
fi
fi
done
I saved this somewhere in my $PATH with the name allgain.sh.
Then I took the Shinobu’s python script shown at the link above and saved it next to the bash script, with the name rg-ape-to-id3.py. Now I can simply go to the root folder of my music library and run allgain.sh. After a while, all my music files will be tagged and ready to go.
Thanks to Shinobu for the original script and the Python code!