return 42;

by Jan Wedel

Cleaning Up Old Mp3s

Some helpful tools and tips to clean up an old mp3 library

The Problem

Recently, I've checked my old mp3 library on my NAS and it was very messy.

Most of the files were organized like this (type A):

./Artist A/Artist A - Song 1.mp3
./Artist B/Artist B - Song 1.mp3
./Artist B/Artist B - Song 2.mp3
./Artist C/Artist C - Song 1.mp3

But then I had something like this containing lots of songs (type B):

./R&B 2004/Artist A - Song 1.mp3
./R&B 2004/Artist B - Song 1.mp3

I wanted to convert all type B songs into type A.

The Solution

So first, I copied those filed to my local SSD because my NAS has slow HDDs and network connection.

Then I'm using TriTag for Mac, not sure if it's still exiting. It allow to add files or directories with mp3s into the App and then do a couple of nice things:

  • If your files are properly namen (e.g. Artist - Title.mp3) but you do not have proper mp3 tags, you can update the tags by a file name pattern.
  • If your tags are correct but the file names are not correct, you TriTag renames the files
  • You have a table editor to rename single properties or do bulk changes (e.g. update artist for all selected songs)
  • TriTag can created a folder structure based on the tags (./Artist/Album/Artist - Title.mp3)

I already did the first three of things, but wanted that feature to migrate from type B to type A.

Removing the Album Level

The next issue was, that, as explained before, TriTag created additional directories for the Album which it didn't want. So I ran a couple of shell scripts:

find . -name "*.mp3" | grep -E "\.\/.*\/.*\/.*" | sed -e 's_\(.*\)_"\1"_g' | xargs -I '{}' rename -n 's/(\.\/.*)\/.*(\/.*\.mp3)/$1$2/' "{}"

This was actually the hardest thing to assemble. Those are the steps

  • Find all mp3s in the current directory
  • Find only those files that are two levels deep (i.e. that have an album directory)
  • Wrap them in to extra quotes because songs contain stuff like single quotes and "&" e.g. that will break xargs
  • that run xargs with a placeholder {} for the file name
  • xargs calls rename (install via home brew on mac) that applies a regex that removes the middle part

the -n parameter on rename is a dry run. When everything looks good, remove it.

Strangly, afterwars files were renamed as ".mp3.mp3" (I probably got something wrong) so I did

find . -name "*.mp3.mp3"  | sed -e 's_\(.*\)_"\1"_g' | xargs -I '{}' rename 's/(.*.mp3).mp3/$1/' "{}"

Removing empty directories

Then, the album directory remained empty so I needed to delete them:

find . -type d -empty -delete

Merging the files by to my NAS

The last thing was to move the files back. However, some artist directories were already existing so I needed to move and merge the directories:

rsync -r --remove-source-files ./ /Volumes/Qmultimedia/music