In my last post, I described tools I had written that would:
- Swap names of artists when they had been rendered with a comma. Holly, Buddy became Buddy Holly, etc.
- Update the metadata for the files based on their locations using the old structure.
- Remove the track number from the start of the file names.
- If the file was part of a collection, attempt to extract artist name from the file name and move it to a directory for that artist.
I ran this on the M:\ drive and it worked (even though I had to designate the M drive using \\wd-netcenter\Music rather than “M:\”.
This is all well and good, but it left me with a couple of problems:
- iTunes put a lot of crap into the library that was NOT on the M drive. I don’t want this crap in the library.
- I wanted to give all the tracks a default Rating of two stars. That way I can change only the ones I want to be other than “normal”.
- Previously, there had been a lot of track in the library that did not in fact exist on the disk. I needed some way to expunge those.
I decide to try to write code to do this.
I wondered if I could do this my editing playlists. These could be in either tab-separated text or XML. I created a Playlist and added a track to it. I then exported the playlist to a text file, modified it to change the rating and then imported it again. The import worked, but it did not change the rating.
Working with Library.xml
I learned that I could export the entire library as an XML file (but not a text file) by using File -> Library -> Export. It was easy to spot fields for Location and Rating.
According to Francisco Balena, there are two ways to manage XML. I started with what he said would be the easier one: using an xml reader and an xml writer. I figured out how to use them, but I also soon learned that this is handling the xml at a very low level and would require a huge amount of coding.
Instead, I loaded the whole thing into an xml document and then figured out how to navigate down to the information on the individual tracks. I did this pretty much by trial and error – looking at the library.xml file as text in one window and opening nodes in Visual Studio to figure out which child of each node I wanted to use.
I did run into one interesting problem. Visual Studio would not allow me to use “M:\” as the root of my music server. Instead, I had to call it \\wd-netcenter\Music. I have no idea why.
I put a feature into Music Manager to update the library.xml file. It does two things:
- If the Location is other than on the M drive, it removes the track from the DOM.
- If there is no Rating, it adds a rating of “40” which corresponds to two stars, my default.
I then started iTunes while holding the shift key. This allows me to specify the XML library I want to enter. I had to key in the file names, since the file-open dialog only shows .ITL files. It took two or three hours to complete this and when it was done, it told me that the file was incompatible and offered to try to repair it. This appears to be taking even longer. I think it is trying to reload album artwork or something like that.
Well, this DID work. It took about 6 hours to grind through everything, but in the end, it appears to have worked. Still, I am not interested in waiting for 6 hours to do a small update. Plus, it appears that there are still a lot of duplicate files. I can probably find them fairly easily by scanning, but I am not looking forward to deleting them one by one.
I think the iTunes SDK will be better suited to what I want to do
Repairing the damage done by the XML efforts
By searching the Internet, I found numerous articles on how to link to the iTunes COM object. It looked pretty easy – except for one thing. I could not initiate the App object. It kept telling me that: The folder containing “LibraryOut” cannot be found, and is required. Please choose or create a new iTunes library.
I wrestled with this for several hours. I could not find a library.itl file that iTunes seemed to be using. This was supposed to be in MyMusic\iTunes, but the one I found there did not have a late enough data on it and did not seem to change when I made changes to the library. Also, I renamed it and iTunes still seemed to start when invoked by itself. I finally gave up and went out to split some wood and think about it.
I have now decided that the problem must be that when I tried to start iTunes using the output file from my XML efforts, it changed the directory in which it was looking for the íTunesLibrary.itl file as well as the name of the file it was looking for.
Here is what I will do:
- Change registry so that \HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ShellFolders\MyMusic now points to “M:\”. Also changed ..\User Shell Folders\My Music.
- Made sure there was a directory named m:\iTunes.
- I note that there is an iTunes Library.itl file in there and it is dated from this morning before I tried to do the import from the XML efforts. I held the shift key down while starting iTunes so that it prompted me to select the library.
- I now note that the date on my iTunes Library.itl is set to current time.
- I tried to run the program again and this time I got a different error.
- I closed iTunes and tried again. This time I was prompted to specify the name of the library which I did. The program now seems to work as expected.
- BUT – it seems that the location information is gone from most of the files. I am going to just empty the library and reload the whole thing.
Getting iTunes reloaded
Well, I got to where I wanted to – at least to step 1.
Here is what I did:
- Cleared the entire library by selecting everything in it and clicking Delete.
- Reloaded it by File -> Add Folder to Library.
- That took many hours.
- I then wrote a program to set all the Ratings to 2 stars (40). I ran it and that worked, but again took a couple of hours.
- I went through the entire list and changed the ratings of the tracks that I liked better than 2 stars. I added some smart playlists.
- I tried to write one of them to a CD (as an “MP3 CD”). It was going to be 200 files with 600+ MB of data. It has been grinding for a couple of hours. I don’t know if it is hung or not.
Conclusion:
iTunes seems to be pretty cool, but it is severely lacking in a couple of aspects. Here is no easy way to:
· Find and remove duplicate music.
· Copy sets of tracks (i.e. a playlist) to another directory for eventual movement to a phone.
· Move files to a different directory.
· Set the rating on a group of files at the same time.
As a result, I expect that I will write a program to assist with using iTunes. I will come up with a rough spec on this and post it.
Notes on using the iTunes SDK
While waiting around for the library to load, I did some internet searching and found that there is an SDK for iTunes. I registered as a Free apple developer (see PasswordSafe) and downloaded the SDK. Mostly, it consists of a help file. Turns out that the DLL is already installed with iTunes.
Here are some useful sites that I found:
http://www.robertprice.co.uk/robblog/archive/2006/8/iTunes_Now_Playing_In_C_.shtml
http://www.ohscope.com/2009/04/02/itunes-com-api-in-c/
http://www.codeproject.com/KB/cs/itunes_and_net.aspx
http://ratnakarg.wordpress.com/tag/c-itunes-sdk/
The following tells how to iterate over the tracks in the library and assign artwork to them. Should be easy to modify to make other modifications as well.
http://www.diller.ca/?p=40
The following tells how to add names to a playlist
http://www.itunessdk.com/blog/itunes-sdk-examples/
And this one that deletes tracks where the source is no longer available:
http://www.hanselman.com/blog/RemovingDeadTracksDuplicatesThatDontExistFromITunesUsingC.aspx
This one shows how to build a simple viewer/player based on the iTunes library.
http://www.codeproject.com/KB/audio-video/iTuneit.aspx
And this one shows how to make an audio recorder in c# based on iTunes
http://www.codeproject.com/KB/audio-video/MP3WaveAudioRecorder.aspx