{"id":2263,"date":"2009-01-02T20:50:04","date_gmt":"2009-01-03T02:50:04","guid":{"rendered":"http:\/\/granades.com\/?p=2263"},"modified":"2009-06-15T10:01:46","modified_gmt":"2009-06-15T15:01:46","slug":"converting-itunes-playlists-to-m3u-playlists","status":"publish","type":"post","link":"https:\/\/granades.com\/?p=2263","title":{"rendered":"Converting iTunes Playlists to M3U Playlists"},"content":{"rendered":"<p>Because I have a crazy byzantine digital music setup, there are times when I want to take an iTunes playlist and move it to our Xbox running XBMC, because the Xbox has nice speakers whereas none of our iTunes-running laptops do. XBMC, like all sane media players, accepts <a href=\"http:\/\/en.wikipedia.org\/wiki\/M3U\">M3U playlists<\/a>. iTunes, however, will only export playlists as plain text files or in their plist XML format. Why? Because Apple. So I&#8217;m left needing to convert the XML files to M3U format.<\/p>\n<p>Luckily, I know Python. Even more luckily, so do a lot of better programmers than I.<\/p>\n<p>Digging through some mailing lists turned up a <a href=\"http:\/\/online.effbot.org\/2005_03_01_archive.htm\">plist XML format parser<\/a> that Frederik Lundh wrote. Sadly, that link I just gave no longer works. Thank goodness for the Wayback Machine! To keep you from having to re-create his code, I stuck it into a <a href=\"\/downloads\/plistloader.py\">Python module called &#8220;plistloader&#8221;<\/a>.<\/p>\n<pre><code>\r\ntry:\r\n    from xml.etree.cElementTree import iterparse\r\nexcept ImportError:\r\n    from xml.etree import iterparse\r\nimport base64, datetime, re, os\r\n\r\nunmarshallers = {\r\n\r\n    # collections                                                                \r\n    \"array\": lambda x: [v.text for v in x],\r\n    \"dict\": lambda x:\r\n        dict((x[i].text, x[i+1].text) for i in range(0, len(x), 2)),\r\n    \"key\": lambda x: x.text or \"\",\r\n\r\n    # simple types                                                               \r\n    \"string\": lambda x: x.text or \"\",\r\n    \"data\": lambda x: base64.decodestring(x.text or \"\"),\r\n    \"date\": lambda x:\r\n        datetime.datetime(*map(int, re.findall(\"\\d+\", x.text))),\r\n    \"true\": lambda x: True,\r\n    \"false\": lambda x: False,\r\n    \"real\": lambda x: float(x.text),\r\n    \"integer\": lambda x: int(x.text),\r\n\r\n}\r\n\r\ndef load(file):\r\n    parser = iterparse(file)\r\n    for action, elem in parser:\r\n        unmarshal = unmarshallers.get(elem.tag)\r\n        if unmarshal:\r\n            data = unmarshal(elem)\r\n            elem.clear()\r\n            elem.text = data\r\n        elif elem.tag != \"plist\":\r\n           raise IOError(\"unknown plist type: %r\" % elem.tag)\r\n    return parser.root[0].text\r\n<\/code><\/pre>\n<p>Do you see what he did there? Frederik used a dictionary filled with anonymous functions that convert each kind of XML data the plist might contain. Sexy!<\/p>\n<p><a href=\"\/downloads\/xmltom3u.py\">My code that uses plistloader is far more prosaic<\/a>.<\/p>\n<pre><code>\r\nimport sys, plistloader, os\r\n\r\n# Search-and-replace strings to adjust the mp3's locations if necessary.         \r\nsandrStrs = { \"file:\/\/localhost\/Volumes\": \"smb:\/\/sargent\" }\r\nm3uHeader = \"#EXTM3U\\n\"\r\n\r\ntry:\r\n       xmlFile = sys.argv[1]\r\nexcept IndexError:\r\n       print \"No xml file passed on the command line.\"\r\n       sys.exit()\r\n\r\nif not os.path.isfile(xmlFile):\r\n       print \"File %s doesn't exist.\"\r\n       sys.exit()\r\n\r\n# Load the playlist using the plistloader module                                 \r\nplaylist = plistloader.load(xmlFile)\r\n\r\n# Base the output filename on the input one, stripping off any '.xml'            \r\n# or similar from the right and adding in .m3u                                   \r\noutfn = xmlFile.rsplit('.',1)[0]+'.m3u'\r\n\r\noutf = open(outfn, 'w')\r\n\r\n# Write the M3U header                                                           \r\noutf.write(m3uHeader)\r\n\r\n# Iterate through the tracks to get each one's location and name                 \r\nfor k, v in playlist['Tracks'].iteritems():\r\n       # The key in this case is the track ID number. The value is               \r\n       # a dict of all information associated with the track                     \r\n       fileloc = v['Location']\r\n       for old, new in sandrStrs.iteritems():\r\n              fileloc = fileloc.replace(old, new)\r\n       outf.write(fileloc+\"\\n\")\r\n\r\noutf.close()\r\n<\/code><\/pre>\n<p>To use the script, export your iTunes playlist as an XML file. Pass the XML filename to the script. If you&#8217;re on a Mac, take a look at <a href=\"http:\/\/svn.pythonmac.org\/py2app\/py2app\/trunk\/doc\/index.html\">py2app<\/a>. It&#8217;ll let you turn the script into an application, which means you can then drop the XML file on the script and it&#8217;ll auto-process it.<\/p>\n<p>Note that the script includes a dict of search-and-replace strings in case you need to fiddle with the mp3 files&#8217; locations as reported by iTunes. As you can see in the example above, I needed to change iTunes&#8217;s &#8220;localhost&#8221; reference to match my SMB share.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Because I have a crazy byzantine digital music setup, there are times when I want to take an iTunes playlist and move it to our Xbox running XBMC, because the Xbox has nice speakers whereas none of our iTunes-running laptops do. XBMC, like all sane media players, accepts M3U playlists. iTunes, however, will only export &hellip; <a href=\"https:\/\/granades.com\/?p=2263\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Converting iTunes Playlists to M3U Playlists<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[],"tags":[],"class_list":["post-2263","post","type-post","status-publish","format-standard","hentry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/granades.com\/index.php?rest_route=\/wp\/v2\/posts\/2263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/granades.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/granades.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/granades.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/granades.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2263"}],"version-history":[{"count":7,"href":"https:\/\/granades.com\/index.php?rest_route=\/wp\/v2\/posts\/2263\/revisions"}],"predecessor-version":[{"id":3025,"href":"https:\/\/granades.com\/index.php?rest_route=\/wp\/v2\/posts\/2263\/revisions\/3025"}],"wp:attachment":[{"href":"https:\/\/granades.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/granades.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/granades.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}