Sunday, May 20, 2007

Using Powershell to move from iTunes to Windows Media Player (WMP11)

Update Oct 2011: If this helps, you may also be interested in Exporting songs in Windows Media Player by star rating.

This is only for the techies but this is what I spent my day on. I have been annoyed w/ iTunes for awhile now and since I finally killed my iPod it is time to try WMP again. I also wanted to play w/ powershell (which rulez) and this seemed like a good task.

This is a simple powershell script to take all of your music in your itunes library and import them into the WMP library. I deved this on vista x64, no idea if it works elsewhere. I don't see why it wouldn't. I also bring across the rating, which was frankly the point of this whole project. Everything else I was looking for seemed to be in the mp3 tags. If you want playcounts and similar, you can modify the script slightly to add the extra attributes. The logic is very simple, if we find something in a few days that needs to be added, it should be simple to fix (or at least, wipe and push new)

--snip: begin iTunes-WMP.ps1
#get itunes
$itunes=new-object -com itunes.application
$iTunesTracks = $itunes.LibraryPlaylist.Tracks

#get WMP
$wmp = New-object -COM WMPlayer.OCX
$WMPLibrary= $wmp.mediaCollection

#set vars
$rated = 0
$processed = 0
$added = 0
$1star = 0
$2star = 0
$3star = 0
$4star = 0
$5star = 0
$defstar = 0
$ctr = 0

#loop through tracks

foreach ( $ITSong in $iTunesTracks){
$processed++
if ($ITSong.Location){
#add to WMP library
$newWMPTrack = $WMPLibrary.add($ITSong.Location)
$added++

#set rating
if ($ITSong.Rating) {

switch ($ITSong.Rating) {
"20" {
$newWMPTrack.setItemInfo("UserRating", 1)
$1star++
$rated++
}
"40" {
$newWMPTrack.setItemInfo("UserRating", 25)
$2star++
$rated++
}
"60" {
$newWMPTrack.setItemInfo("UserRating", 50)
$3star++
$rated++
}
"80" {
$newWMPTrack.setItemInfo("UserRating", 75)
$4star++
$rated++
}
"100" {
$newWMPTrack.setItemInfo("UserRating", 99)
$5star++
$rated++
}
default {
#so I have something to query for random nums
$newWMPTrack = 42
$defstar++
}
}
}
}

#so I have something to watch
$ctr++
if ($ctr%500 -eq 0) { Write-Host $ctr}

if ($ctr%50 -eq 0) { Write-Host -NoNewline $ctr "... "}

}

#output results
Write-Host "Processed: " $processed
Write-Host "Rated: " $rated
Write-Host "Added: " $added
Write-Host "1: " $1star
Write-Host "2: " $2star
Write-Host "3: " $3star
Write-Host "4: " $4star
Write-Host "5: " $5star
Write-Host "def: " $defstar

1 comment:

  1. I enjoy the pleasures of never having to deal with Apple, but nice script!

    ReplyDelete

analytics