ReplayGain, SoundCheck and metadata
Posted in Uncategorized on June 7th, 2011 by saintdevA couple of days ago I finally got fed up with some of my library being different volumes. I had previously run mp3gain on some of my mp3s, and aacgain on most of my aac files. So I had a strange mishmash of normalized and non-normalized files in my library. I decided, screw it, I’m just going to normalize everything. So I called up our good friend find to help me out.
find music/ -iname '*.mp3' -print0 | xargs -0 mp3gain -r -k
There, that should have taken care of the mp3s, except it didn’t (also this won’t work if you want album gain). It seems that if mp3gain encounters an error in a batch of arguments, it just quits, instead of continuing to the next file. So it would get 50-60 files out of 1000+, encounter an error and xargs would go on to the next group of 1000+. So I did some searching and found this post. Which has the fix for both issues above (assuming your albums are each in their own subdirectory).
find music/ -iname '*.mp3' -execdir mp3gain -r -k "{}" +
If you run this without the -r switch, it will calculate both track and album gain and only write the tags to the mp3. With the -r switch it will modify the files permanently. Yay!
Now I did the same with my AAC files, which presented more issues. First it seems aacgain 1.8 has a problem with some tags iTunes writes, and will render the metadata unreadable by some tag readers. All the metadata is still there and some applications can read it, but others can’t and don’t give any errors. So I had to revert those changes (thanks Unison). I ended up compiling aacgain 1.9, which takes an updated copy of mp4v2 for it’s tag writing (building aacgain was a PITA by the way). This seems to have fixed the incompatibility.
Now, the only real issue I have left is that the iTunes SoundCheck tags no longer match what they should be. So, in iTunes my volumes are going to be all over the place still. This is unacceptable for me, so I went in search of something that could remedy this. I knew that there was iVolume. I had played with the trial once. It’s actually a excellent program. I would highly recommend it, except $30 in my opinion is a little too much for something like this and doesn’t work on linux. Eventually I came across Richard van den Berg’s rg2sc script. It only works with mp3s, which was ok with me, as I have for more of those than AAC files. I tried it out on one file, just to check before I used it on my whole library. I checked the tags just to be sure it worked….and now there is only a iTunNORM tag on the file! For some reason it removes all the other ID3 tags from a file when it writes the new one. Looking at the script it seems to be doing what it should, but I don’t know perl all that well.
Another issue I didn’t know about but is mentioned on Richard’s rg2sc page is by default mp3gain creates APEv2 tags. I don’t like ID3 and APE tags in the same file. I found Rasher’s rgfix.py that converts APEv2 ReplayGain tags to ID3v2 tags.
I’ve been meaning to try to learn Python a little better for quite a while. I know there’s a high quality tagging library for Python – Mutagen. This seemed like a good chance to learn a little more (and I could add support for MP4 while I’m at it).
My result is available on github. It handles both mp3 and mp4 files. As an added bonus, it doesn’t erase all existing tags in a file! Please yell at me for anything you find wrong with it. This was really more of a fun project to learn python, so I probably did a lot of stupid things.
So finally, what I should have been using above can now be simplified to:
find music/ -iname '*.mp3' -execdir mp3gain -s i -r -k "{}" +
find music/ -iname '*.m4a' -execdir aacgain -r -k "{}" +
find music/ -iname '*.mp3' -print0 -or -iname '*.m4a' -print0 | xargs -0 rg2itunnorm.py
The -s i switch to mp3gain tells it to write ID3 tags instead of APE tags.



