If you have a load of MP3s on your computer, you might be surprised at what the MP3 tags contain. All kinds of weird stuff from the internet, hopefully none of it incriminating. You never see these tags unless you use dedicated software, because iTunes uses a more modern system.

Rate this article:

(not rated yet)

MP3.png
ID3 Tags

iTunes and MP3 tags

iTunes does not even use version 1 MP3 tags, which are the original and most simple tags embedded in a song file. id3v1 seems to still be universally used to this day, probably for backward compatibility. Older MP3 desktop players and in-dash units use id3v1 tags to display song and artist. That was the impetus to do this project, to clean up the song names and artists for playing in my Kenwood in-dash stereo.

All scripts on this page assume your iTunes is set to "Keep iTunes Music folder organized", and "Copy files to iTunes Music folder when adding to library". This means files will have a path like this: ~/Music/iTunes/iTunes Music/New Order/Power, Corruption and Lies/Blue Monday.mp3

You can view this setting in iTunes Preferences>General tab. It is the default, so if you have a different system you may need to hack.

Version 1 tags

Using the simple shell tool id3tool, I was able to view the contents of id3v1 tags and set them to what I wanted.

The easiest way to install id3tool on Mac OSX is to first install MacPorts. You may need to first install Xcode Tools minimal tools. <prove this part> your shell path may need to be set to include the MacPorts commands location. If that was successful, open Terminal and run this command line (you can drag or copy-paste):

sudo port -d selfupdate

then install id3tool:

sudo port install id3tool

Viewing tags

When id3tool is installed, you can now dump the contents of all id3v1 tags in your iTunes Music folder:

cd ~/Music/iTunes/iTunes\ Music/
find . -name *.mp3 -print0 | xargs -0 id3tool

You might want to dump the tag contents to a text file for future reference:

find . -name *.mp3 -print0 | xargs -0 id3tool > -Tags.txt
open ./-Tags.txt

If you don't like what you see, you might prefer to totally remove all id3v1 tags and start from scratch.

Deleting tags

The id3tool shell tool cannot remove tags, only set them. You will need to install a more powerful tool called id3v2. I chose to use two different tools because id3v2 sets version 2 tags along with setting version 1 tags, which I don't like. It is easy to install using Computer/Shell/MacPorts:MacPorts. You apparently can't remove individual data fields, so deleting the entire tag segment and starting from scratch works well. Note that this command only removes MP3 id3v1 tags, it does not touch the id3v2 tags that iTunes uses.

/!\ I strongly recommend backing up your files before running any file modifying scripts.

I had no problem with this procedure, even after running it multiple times under experimental conditions. But backups are always a good idea anyway. I use an external FireWire disk.

First make sure you are in the iTunes Music folder as your working directory:

cd ~/Music/iTunes/iTunes\ Music/

Delete all tags with this command:

find . -name *.mp3 -print0 | xargs -0 id3v2 -s

The next section creates only the data fields we specify, without the empty ones.

Setting tags

This one line Terminal script sets MP3 version 1 tags for song title and artist as taken from the filepath components. It is meant for files arranged in iTunes' default filesystem format, where filepaths are as explained at the top of this page.

The script runs from either the artist folder or the iTunes Music folder, which makes it easy to test on artists befory you hit your entire library.

find . -name *.mp3 | while read path ; do echo $path ; title=$(echo $path | sed -e 's|^.*/||' -e 's|\.mp3$||') ; id3tool -t "$title" "$path" ; artist=$(echo "$path" | awk 'BEGIN{FS="/"}{print $2}') ; id3tool -r "$artist" "$path" ; id3tool -n Gnarlodious "$path" ; done

The above script also sets the note field to Gnarlodious, which is how I keep track of which files are already processed. You should replace it with whatever you want.

You may notice that the script uses a chained sed command rather than a more compact REGEX. The reason is, the REGEX version choked on my Hebrew filepaths, which is probably a unicode thing. So I stuck wih something that could handle unicode filepaths.

YAAY! Now I can burn MP3 CDs for my in-dash unit and have the song title and artist scroll by!

Applescript

If you want more control over the process, you can use Applescript. This script takes a while to run, about 5 minutes on my 1200 song library using a 2.4GHz Intel Mac. But it will allow more customization of the data fields.

tell application "Finder"
  if not (exists file ":opt:local:bin:id3tool" of the startup disk) then -- may also be at /usr/local/bin/id3tool
    display dialog "Script requires id3tool to be installed"
    return
  end if
  
  set thisLibrary to (home as string) & "Music:iTunes:iTunes Music"
  set artistList to folders of folder thisLibrary
  repeat with thisArtist in artistList
    
    set albumList to folders of thisArtist
    repeat with thisAlbum in albumList
      
      set songList to get (files of thisAlbum whose name ends with ".mp3")
      repeat with thisSong in songList
        
        set songName to name of thisSong
        set shellPath to the quoted form of POSIX path of (thisSong as alias)
        set titleCmd to "/opt/local/bin/id3tool -t " & quoted form of (text 1 thru -5 of songName) & space & shellPath
        do shell script titleCmd
        
        set artistCmd to "/opt/local/bin/id3tool -r " & quoted form of (name of thisArtist as string) & space & shellPath
        do shell script artistCmd
        
      end repeat
    end repeat
  end repeat
end tell

tell application "Terminal"
  if not (exists window 1) then do script ""
  do script "find " & quoted form of the POSIX path of (thisLibrary as string) & " -name '*.mp3' -print0 | xargs -0 id3tool" in front window
end tell

This Applescript is also downloadable here: MP3_Tags_Script.dmg

Version 2 tags

to be continued...

Comments

Smiley:

305 page views. CategoryApplescript CategoryShell