#!/usr/bin/python # Convert an iTunes-exported XML playlist to m3u format # # Author: Stephen Granade # Date: 2 January 2009 import sys, plistloader, os # Search-and-replace strings to adjust the mp3's locations if necessary. sandrStrs = { "file://localhost/Volumes": "smb://sargent" } m3uHeader = "#EXTM3U\n" try: xmlFile = sys.argv[1] except IndexError: print "No xml file passed on the command line." sys.exit() if not os.path.isfile(xmlFile): print "File %s doesn't exist." sys.exit() # Load the playlist using the plistloader module playlist = plistloader.load(xmlFile) # Base the output filename on the input one, stripping off any '.xml' # or similar from the right and adding in .m3u outfn = xmlFile.rsplit('.',1)[0]+'.m3u' outf = open(outfn, 'w') # Write the M3U header outf.write(m3uHeader % pname) # Iterate through the tracks to get each one's location and name for k, v in playlist['Tracks'].iteritems(): # The key in this case is the track ID number. The value is # a dict of all information associated with the track fileloc = v['Location'] for old, new in sandrStrs.iteritems(): fileloc = fileloc.replace(old, new) outf.write(fileloc+"\n") outf.close()