Normalizing your MP3 collection with mp3gain

Carthik Sharma from Ubuntu Blog suggests the following method to normalize all the MP3 files in your collection:

find . -type f -iname '*.mp3' -print0 | xargs -0 mp3gain -r -k

Unfortunately, this seems to have problems with very large collections (I suspect that the command line is being filled up), so here’s what I came up with:

find . -iname '*.mp3' -execdir mp3gain -r -k "{}" \; &

If you’d rather use album normalization (and you have your files separated by album such as “~/Music/David Bowie/Hunky Dory”/):

find . -iname '*.mp3' -execdir mp3gain -r -k -a "{}" + &

How to use it

Simply open a terminal, browse to your music directory (using cd), and type it in.

Break it down

What the command does:

  1. find . -iname '*.mp3' finds all MP3 files in the current directory and all subdirectories.
  2. -execdir mp3gain -r -k "{}" \; & executes “mp3gain -r -k” on each file (the filename goes where {} is found). The semi-colon indicates to execute the command once per file, but since ; is also a significant character for bash, you need to escape it with a backslash. The final ‘&’ tells bash to run the command in the background.

Comments 9

  1. Carthik wrote:

    Thank you. I have added a link to this article for those who might be interested in your way of doing things.

    Posted 20 Sep 2006 at 3:44 pm
  2. Fird wrote:

    Thank you for your guide It really helps!

    Posted 11 Dec 2006 at 9:31 pm
  3. John wrote:

    I just use:

    for file in `find . -type f | grep mp3`; do mp3gain -q -k -p -r “$file”; done

    Posted 23 Feb 2007 at 5:32 am
  4. Mike wrote:

    I’m curious what the ‘+’ in the album command does? I think I understand all the rest of the command, and the -a is fairly easy to guess, but the change from ‘\;’ to ‘+’ is a mystery to me.

    Posted 29 Aug 2008 at 5:38 pm
  5. Porges wrote:

    Mike: Check out the ‘find’ man page; you can use “-execdir … ;” which runs the command one file at a time, or “-execdir … +”, runs the command on all files in a directory at once.

    Posted 01 Sep 2008 at 2:19 pm
  6. Adam wrote:

    I’ll try resubmitting the comment now that I know what the problem is.

    Porges, your trackwise replaygain command will work fine but the album-based method will not do what you expect it to. In fact, it will do the exact same thing the trackwise command will. John’s for-loop command will work if there are no spaces in the file or directory names. Otherwise it will fail — or at least it did for me. After much mucking around, the following command did it for me:

    find . -type d | while read i; do mp3gain -a -k "$i"/*.mp3 ; done

    Basically what it does it run mp3gain on EVERY subdirectory but only on the *.mp3 files contained therein. This is necessary because mp3gain will not accept a directory as an argument (unlike vorbisgain). The “read” command is there to handle spaces in directory and file names. The equivalent command for vorbisgain is:

    find . -type d -execdir vorbisgain -a -f {} +
    Posted 15 Jun 2009 at 12:04 am
  7. Porges wrote:

    The comments are moderated, so I have to approve them before they will show

    Thanks for pointing that out. The find manpage makes it seem like this is what should happen…

    Given this structure:

    dir1/
        file1.mp3
        file2.mp3
    dir3/
        file3.mp3
        file4.mp3

    … with ‘;’ the command is executed once for every file, and with ‘+’ the command is executed on "file1.mp3 file2.mp3" and then "file3.mp3 file4.mp3".

    Whereas, as you say, the change seems to do nothing!

    Edit: After more research, it seems that this is partly a bug in findutils, and partly a misunderstanding of the ‘+’ modifier. It doesn’t guarantee that all files in the directories will be collected, and currently performs the exact same task as ‘;’!

    Posted 16 Jun 2009 at 4:51 pm
  8. Adam wrote:

    The comments are moderated, so I have to approve them before they will show

    I realised that as soon as I submitted it with firefox. I immediately got some feedback from the site that my comment was awaiting approval. With Opera I got nothing at all… And yes I have cookies enabled (but they are deleted when I shut opera down). That’s where my confusion sprang from.

    Posted 17 Jun 2009 at 12:37 pm
  9. Adam wrote:

    Oh just one more thing. For those who say that vorbisgain has a “-r” switch eliminatinf\g the need for find completely, it will only work if your collection is comprised of oggs only. It will choke (and quit) on the first non-ogg/vorbis file it sees.

    Posted 17 Jun 2009 at 1:25 pm

Trackbacks & Pingbacks 2

  1. From Normalize the Gain (Playback Volume) of your MP3s « Ubuntu Blog on 20 Sep 2006 at 3:44 pm

    [...] Edit: for an alternative command, with explanation, and a way to use album-gain for songs from albums, visit porges The following command, executed from the directory where I store my music normalized all the files in my collection: $find . -type f -iname ‘*.mp3′ -print0 | xargs -0 mp3gain -r -k [...]

  2. From apt-get install mp3gain | apt-get install on 09 Mar 2007 at 10:26 am

    [...] replaygain, and found a link to mp3gain. Once that was installed, I then modified instructions from porg.es as [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *