ID3 Tagging From File Names

The Forester can play MP3 files from a USB flash drive and, given the utter craptitude of radio stations around here, I dumped a bunch of CD tracks onto a drive. For historic reasons, very few of the tracks had ID3 tags, so the Forester’s display showed only gnarly file names for the last few years.

This burst of Bash line noise runs through the directory of album directories, extracts the relevant information from the directory and track names, then pops the tags in place:

for d in * ; do for f in $(ls $d) ; do art=$(echo $d | cut -d- -f1 | tr '_' ' ' | sed 's/-/ - /g') ; alb=$(echo $d | cut --complement -d- -f1 | tr '_' ' ' | sed 's/-/ - /g') ; t=$(echo $f | cut -d- -f1) ; s=$(echo ${f%.*} | cut --complement -d- -f1 | tr '_' ' ' | sed 's/-/ - /g') ; id3tag -2 -a"$art" -A"$alb" -s"$s" -t$t $d/$f ; done ; done

It’s (marginally) easier to see this way:

for d in * ; do 
 for f in $(ls $d) ; do 
  art=$(echo $d | cut -d- -f1 | tr '_' ' ' | sed 's/-/ - /g')
  alb=$(echo $d | cut --complement -d- -f1 | tr '_' ' ' | sed 's/-/ - /g')
  t=$(echo $f | cut -d- -f1)
  s=$(echo ${f%.*} | cut --complement -d- -f1 | tr '_' ' ' | sed 's/-/ - /g')
  id3tag -2 -a"$art" -A"$alb" -s"$s" -t$t $d/$f
 done
done

What’s going on:

  • cut – extracts track number and song title
  • tr – convert underscores to spaces
  • sed – put spaces around hyphens

The id3tag program can install either ID3V1 or ID3V2 tags on each pass, so I just recalled the command, edited the -1 to -2, and ran the whole mess again.

After a bit of manual cleanup, things looked pretty good.

Although the id3ren program seemed as though it could do the trick, it’s really intended to rename files from existing tags. Making it go the other way rapidly became a steel-cage death match; I gave up.

  1. #1 by Raj on 2019-10-29 - 23:38

    As I commute every week long distance I have pen drives with 1000s of songs! I use MP3TAG and Flash Renamer for filename to tag or other way on my Windows machine.

    • #2 by Ed on 2019-10-30 - 07:46

      Reading the MP3TAG feature list triggers a vague memory of trouble with online databases resulting in weird auto-tagging errors. Living here in the future is certainly better, as a random sampling shows some-but-not-all of my files have some-but-not-all tags.

      Not something I’ll fix by hand! [sigh]

Spam comments get trashed, so don't bother. Comment moderation may cause a delay.

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s