How do I programtically access and modify music metadata entries through iTunes?
April 7, 2006 6:40 AM   Subscribe

Has anyone ever managed to use iTunes and COM to access the iTunes database and modify MP3 meta-data tags? I need to change the genre for every track by a certain artist and since I have a list of over 800 artists, it is not something I can do by hand.

Genres in my iTunes database (and MP3's) are a mess and so I've decided to try and fix it.

Following some simple coding, I've managed to work out what the most common genre entry in the FreeDB database is for many of the artists in my collection and I'm going to populate every song by that artist with this new genre. For example the most common entry in FreeDB for "Faithless" is "Dance" and for "Groove Armada" is "Lounge". I know this isn't a perfect solution, but it is better than the mess I currently have.

So I have a tab seperated file which contains artist name and genre and what I plan to do is walk through the entire iTunes database looking at the artist for each song. If I find an artist that matches one in my tab seperated file then I'll "correct" the genre entry in iTunes to be the same as that in the text file.

Herein lies the problem. Having looked at the iTunes documentation I haven't a clue how to:

1. Walk through every single song from within iTunes
2. Extract the artist value from that song
3. Write the genre value to that entry (which I assume will also update the ID3 v1 and v2 tag on the actual file, right?)
4. Exit modifications and the database without corrupting anything.

I'm using Visual Basic to perform this. Searching on the web has found me very little so I'm looking to see if anyone on AskMefi has any ideas.
posted by mr_silver to Computers & Internet (13 answers total)
 
What you need is this SDK from Apple. Works with any Windows Scripting Host compatible language (VB, JScript). I've used it to automate retagging some of my music. Works great, if a bit slowly.
posted by dudeman at 6:51 AM on April 7, 2006


Response by poster: Sorry my question wasn't very clear. I have the iTunes SDK and the documentation isn't very clear (for me) on how I do this.

I've looked at the examples and the helpfile and I'm still no clearer on understanding what I do as soon as I've connected to iTunes.

As such, I'm looking for a step-by-step guide or a large number of sample applications (with code) which I can look over.

Would you be prepared to share your automatic retagging code? This would at least show me how to traverse the database, read in a value and write out a new value - which is all I want to do.
posted by mr_silver at 6:57 AM on April 7, 2006


Since this is metadata in the MP3s, you can modify the files directly, if that would be easier. No need to go through iTunes.
posted by smackfu at 7:06 AM on April 7, 2006


Response by poster: Unfortunately my weapon of choice (MP3::Info in Perl) doesn't support writing of ID3v2 tags, what other options do I have?

Also, how would I get the iTunes database to recognise that all these MP3's have been modified? If it doesn't notice, then it'll use the genre information cached in its own database despite the fact that the MP3's have different information.
posted by mr_silver at 7:11 AM on April 7, 2006


Here's some code. Note that I do not iterate over the database, instead I expect the user to select tracks in the Library and then run the script.

/*
File: sample.js

Version: 1.0

*/


var ITTrackKindFile = 1;
var iTunesApp = WScript.CreateObject("iTunes.Application");
var tracks = iTunesApp.SelectedTracks;
var numtracks;
var albumTracks = new Array();


try { numTracks = tracks.Count; }
catch (e) { numTracks = 0; }

if (numTracks == 0)
{
WScript.Echo("Select the tracks in iTunes that you would like to auto-tag");
}


while (numTracks != 0)
{
var currTrack = tracks.Item(numTracks);

// is this a file track?
if (currTrack.Kind == ITTrackKindFile)
{
// now do whatever we want with currTrack
var title = currTrack.Name;
WScript.Echo("Filetitle = " + title);
}
numTracks--;
}
posted by dudeman at 7:23 AM on April 7, 2006


Ummm... Try this 4 step method (it's how I've tackled this in the past):

1. In itunes, sort your library by artist, by clicking on the artist pane at the top of your itunes window.

2. Select all the files with the artist whose genera you want to change. If you're on a mac, you can do this by clicking on the top file of the list, and then holding down the shift key and clicking on the bottom file in the list.

3. Go to the file menu, and select get info.

4. In the get info window, click on 'info'. Fill in your genre in the genre box. All the genre fields in the songs you have selected will now be the new genre.
posted by u2604ab at 7:53 AM on April 7, 2006


Smackfu, you can modify the tunes directly, but iTunes will have to play the files again before it finds that the metadata has changed. In a large library (like, say, one with >10k songs in it) it will take just as long to modify them in an external program as it would for iTunes to do it itself--the delay is in rewriting the .itl and .xml files after modifying the ID3 tag.
posted by Tuwa at 8:19 AM on April 7, 2006


if i understand the situation correctly there is no need for you to do this programmatically, itunes has built in support to do what you would like to do.

simply search for the artists name, this should display all of the artists songs. hit ctrl a to select all the songs. right click on a track and select "get info" from the context menu. most likely you will receive a warning indicating that you will be editing multiple files at once. since this is what you are trying to do ignore the warning. now all you have to do is select the genre you would like and hit ok. it will take a while but itunes will update all the files you had selected.
posted by phil at 8:33 AM on April 7, 2006


I second .

I'm forever changing my tags using Itunes.

posted by the_epicurean at 8:35 AM on April 7, 2006


Response by poster: Thanks for the tips on changing it manually. However as I mentioned in my original posting I have over 800 artists to change!

It has to be done programmatically.
posted by mr_silver at 10:36 AM on April 7, 2006


Have you thought about having MusicBrainz do it for you automagically?
posted by blue_beetle at 1:08 PM on April 7, 2006


my weapon of choice (MP3::Info in Perl) doesn't support writing of ID3v2 tags, what other options do I have?

A bunch of other Perl Modules?

MP3::Tag will do it, I think. I used it myself some time ago and can't remember all the details but I did pretty much what you want to do.
posted by AmbroseChapel at 2:41 PM on April 7, 2006


OK this is what I would do, using VBScript:

Dim iTunes
Set iTunes = CreateObject("iTunes.Application")
Dim library
Set library = iTunes.LibraryPlaylist
Dim tracksByArtist
Dim artistName
Dim track
Dim genre

'For each artist row in your text doc
artistName = 'get the artist's name from the text doc
genre = 'get genre from the text doc

Set tracksByArtist = library.Search(artistName, 2) '2 means search by artist name

For Each track In tracksByArtist
track.Genre = genre
Next

'Next
posted by jeff_w_welch at 3:57 PM on April 28, 2006


« Older Vintage Clothing Needed, Stat!   |   What's the ultimate (fake) book synopsis? Newer »
This thread is closed to new comments.