Michael A Heap

iTunes Library Parser

September 6, 2009 Code, Featured, Music, PHPNo Responses on this article

I found my old minidisc player the other day, and I can't stop listening to it. The sound it produces is so much better than what I'm used to nowadays. I checked out my itunes library and as I expected, a lot of songs in there were at 128kbps. That's bearable but I also discovered some tracks that were at 64kbps or lower (I've been building up this collection for the last 5 years. Lower bitrates = more music on a 128mb player :) ) Seen as I was going to rip the low bitrate CD's again, I decided to do everything below 256kbps.

However, I'm too lazy to sort through itunes and make a list of everything that I need to dig out. This is where iTunes' XML export and PHP came in handy. First, I exported my library to XML by going to File->Library->Export Library, then copied it to my server for processing.

I wrote a little script that converts the XML into a nice collection of objects. There's one iTunesLibrary object, and that contains lots of track objects.

The iTunesLibrary has three methods:

function getTracks(){ return $this->_tracks; }
function addTrack($__t){ $this->_tracks[] = $__t;}
function removeTrack($__k){ unset($this->_tracks[$__k]); }

They're all quite self explanatory, but here's a usage example anyway. It will remove all tracks with the word "dark" in the title from the library object

$library = new iTunesLibrary("Library.xml");
foreach($library->getTracks() as $k => $track){
	if (stristr("dark", $track->Name)){
		$library->removeTrack($k);
	}
}

If you have any more uses for the library, let me know in the comments :) You can also grab a copy of the class from github, where I've enclosed the script I wrote for generating a list of mp3's that I needed to re-rip as example.php

Related posts:

  1. Codeigniter Theme Library
  2. 8 Albums I Couldn’t Live Without
  3. My First Git Experience
  4. Codeigniter Setup

Tags: , ,
« »

Leave a Reply