Un-Bonus Tracks

Written by J David Smith
Published on 19 December 2014

Since this task is most easily done with mpc (which has a searchable list of my collection) and metaflac (a command line flac metadata editor), I turned to shell scripting. The full script to accomplish this was pretty trivial:

#!/usr/bin/zsh
IFS=$'\n';
suffix=" (Bonus Tracks Version)";
root="/home/emallson/Music/";
for songpath in $(mpc search album "$suffix"); do
    echo $songpath;
    tag=`metaflac --show-tag=ALBUM "$root$songpath"`;
    metaflac --remove-tag=ALBUM "$root$songpath";
    metaflac --set-tag="ALBUM=${${${(C)tag#ALBUM=}%$suffix}/The /the }" \
        "$root$songpath";
done

This few lines of code let me fix the six or seven albums that I'd been putting off fixing for months. The heart of it is in the nested substitutions being performed in the metaflac –set-tag command. It looks pretty insane, but actually is quite simple.

It breaks down into 4 separate transformations (listed in order of application):

The <code>#</code> tells zsh to remove the string <code>ALBUM=</code> from the *beginning* of the
string if it is present. This is removed because the next transformation
will muss it up.
Capitalizes the first letter of each word (and lowercases the rest) in the
string.
Removes the value in the <code>$suffix</code> variable from the *end* of the
string. Note that this is an exact match &#x2013; not a regular expression &#x2013; so
the parentheses in the `$suffix` are fine.
Replaces all instances of <code>The</code> with <code>the</code> in the string. If there is one
word I am picky about capitalization for, it is `the`.
This analogous to doing `sed -ie s/The /the /g $file`

The result of all these transformations is an album title that is (more or less) properly capitalized and missing the "(Bonus Tracks Version)" suffix!

Before:

plain
Breaking the void (Bonus Tracks Version)
Empty (Bonus Tracks Version)
Gotta get mad (Bonus Tracks Version)
Letting go (Bonus Tracks Version)
Moving on (Bonus Tracks Version)
Return (Bonus Tracks Version)
Winter Cities (Bonus Tracks Version)

After:

Breaking the Void
Empty
Gotta Get Mad
Letting Go
Moving On
Return
Winter Cities

The simplicity of this script means that I will likely be able to extend it in the future to fix common music tagging ailments.

Of course, I'll have to make sure I never run it on a Skinny Puppy album. Ye ever seen one o' those?