Skip to main content
laria.me

Tag: unix

mass-mp3-retag.sh

Published
Tags

I needed to tag some MP3 files to transfer them to my Hi-MD player (I know, I'm a technological caveman sometimes...). Since I have the most important music infos encoded in the directory structure, I could write a little shell script to automate this.

#!/bin/sh

# Mass retagging of mp3 files that don't have ID3 tags.
# The MP3 files must be named like this to use this script:
# ./<first letter of artist>/<artist>/<album>/<track number> - <title>.<file extension(s)>
# ^-- The current working directory

exsomething() { echo "$1" | sed 's#'"$2"'#\1#'; }
exartist() { exsomething "$1" '^\././\([^/]*\)/.*$'; }
exalbum() { exsomething "$1" '^\././[^/]*/\([^/]*\)/.*$'; }
extrack() { exsomething "$1" '^\././[^/]*/[^/]*/0*\([0-9]*\)\s*-.*$'; }
extitle() { exsomething "$1" '^\././[^/]*/[^/]*/[0-9]*\s*-\s*\([^\.]*\)\.[^\.]*$'; }
hasinfo() { id3info "$1" | grep Title >/dev/null; }

retag() { id3tag -a"$(exartist "$1")" -A"$(exalbum "$1")" -s"$(extitle "$1")" -t"$(extrack "$1")" "$1"; }

find -type f | while read fn; do
    if ! hasinfo "$fn"; then
        retag "$fn"
    fi
done

https://gist.github.com/silvasur/4508b9c695a124f239ac

This is why a Unix-style system beats Windows: You can combine simple tools with a simple, yet powerful programming language to automate almost everything if you want to. On Windows you'd probably have to download a 3rd party program for that.