12.07.2005

Fixing iTunes 6 library with Ruby

Like many people, when I upgraded iTunes to V6, I had problem with my iTunes library. For some reason, in the upgrade process iTunes lost the location of many mp3 files in its library.

I was unable to use the fix proposed by Apple as I do not immediately found out about the problem. I added stuff to my library and would loose all me new playlists and ratings if I followed it. Also, I was not able to find a version of my library dated at the time of the upgrade.

I have something like 1200 songs in my library, more than 200 of them lost the location of the actual mp3 file. Clicking around to try to find them within iTunes would have take me days.... And bored me to death.

So I wanted to add all these files back to my library while keeping their rating (I did not care much for the last play time and play count).

I've started to play with Ruby a few weeks ago. I use it to test web application using Watir. I thought that fixing my iTunes library with a small Ruby script would be a fun thing to try.

So I did... I wrote a small Ruby script that would connect to iTunes using the Win32 OLE interface. The script is fairly simple, it check each tracks in the library and if it's location is an empty string, it try to find the music file (mp3 or m4a), add it back to the library and set it's rating to the same rating as the current track.

Note that I could not just set the Location property of the track as this property is read only using the iTunes OLE interface.

A few minutes later, all the music files that matched to a song name were added back to my library with the proper rating. I did have to get rid of the duplicates entries in my library. But at least iTunes allows one to easily show duplicate songs. It was just a matter of selecting songs with the (i) icon, right-clicking and pressing clear.

I also wrote another Ruby script that would scan my music folder and add music file that were not already present in my library. This was necessary as my first script missed some files. I lost my rating for these files but there was'nt that many of those.

So here are the Ruby script, customize them to your liking (like the music base folder) and use them to fix your own library. But do so at your OWN RISK. I do not garantee that they will work for you!

Script 1 :

require 'win32ole'

iTunes= WIN32OLE.new('iTunes.Application')

tracks= iTunes.LibraryPlaylist.Tracks

totalTracks= 0;
tracks.each {|t|
  totalTracks+= 1
  if t.Location == ''
    puts t.Name
   
    name= t.Name
    Dir.glob(File.join("D:\\My Music", "**", "*#{name}*.{mp3,m4a,MP3}")) {|l|
      puts "Adding #{l} to iTunes"
      status= iTunes.LibraryPlaylist.addFile(l)
      while (status.InProgress) do
        sleep(0.5)
      end
      status.Tracks.each {|track|
        track.rating= t.rating
      }
    }
  end
}

puts totalTracks

Script 2 :

require 'win32ole'

iTunes= WIN32OLE.new('iTunes.Application')

tracks= iTunes.LibraryPlaylist.Tracks

totalTracks= 0

Dir.glob(File.join("D:\\My Music", "**", "*.{mp3,m4a,MP3}")) {|l|
  found= false
  tracks.each {|t|
    if t.Location.rindex(File.basename(l)) then
      found= true
      break
    end
  }
  if (!found) then
    totalTracks+= 1
    puts "Adding #{l} to iTunes"
    status= iTunes.LibraryPlaylist.addFile(l)
    while (status.InProgress) do
      sleep(0.5)
    end
  end
}
puts totalTracks

Have fun!

1 comment:

Ernest said...
This comment has been removed by the author.

AdSense Links