Converting iTunes Playlists to M3U Playlists on a Mac

So I wasn’t content with just one way of creating M3U playlists out of iTunes. I went and wrote another. There’s more than one way to do it, as I learned writing Perl programs.

Pity I’m writing in Python.

Anyway, this method only works on a Mac, because it uses Applescript. Actually, it uses appscript, an event bridge that lets you do what Applescript does, only in a real language like Python. It’s called iTunesm3u.py, and requires that you install py-appscript first. To use it, have iTunes open. Select a playlist. Then run the script (or double-click the script if you turn it into an app with py2app) and it’ll create an .m3u file in the same directory as the script.

#!/usr/bin/env python

# Using the appscript Python-to-applescript bridge, convert an iTunes
# playlist into m3u format.
#
# Author: Stephen Granade
# Date: 2 January 2009

import math, re, sys, os
from appscript import *

# Search-and-replace strings to adjust the mp3's locations if necessary.
sandrStrs = { "/Volumes": "smb://sargent" }

# For more information about what classes etc. are available from iTunes
# via appscript, see
# http://appscript.sourceforge.net/objc-appscript/iTunes-objc/index.html

# Get ahold of iTunes
iTunes = app('iTunes')

# The browser window's view is the currently-selected playlist
try:
    playlist=iTunes.browser_windows[1].view()
except CommandError, detail:
    if detail.errornumber == -1731:
        print "There is no current playlist."
        sys.stdout.write('\a')
        sys.stdout.flush()
        sys.exit()
    else:
        raise CommandError, detail

# Use the playlist's name as that of the m3u file
outfn = os.path.abspath(__file__)
outfn = os.path.join(os.path.dirname(outfn),playlist.name()+".m3u")

outf = open(outfn, "w")

outf.write("#EXTM3U\n")

for track in playlist.tracks():
    # Skip any files whose locations aren't currently available
    # If the file doesn't exist on disk, then trying to access the
    # location will throw an AttributeError exception.
    try:
        fileloc = track.location().path
        if not os.path.isfile(fileloc):
            throw(AttributeError)
        # Perform search-and-replace on the file location
        for old, new in sandrStrs.iteritems():
            fileloc = fileloc.replace(old, new)
        durstr = "%d" % math.floor(track.duration())
        outf.write("#EXTINF:"+durstr+","+track.name()+"\n")
        outf.write(fileloc+"\n")
    except AttributeError:
        print """+track.name()+"" isn't available on disk. Skipping."

3 Comments

  1. Gary Johnson
    on November 28, 2009 at 11:34 am | Permalink

    I have the opposite problem. I wan to take windows m3u playlists (not Itunes) and play them on my MAC Mini

    See my post here
    http://discussions.apple.com/thread.jspa?threadID=2240820&tstart=210
    Any suggestions would be appreciated.

  2. on November 29, 2009 at 4:55 pm | Permalink

    Sorry, I’ve no good idea of what might be going on.

  3. fserw
    on January 16, 2010 at 7:08 am | Permalink

    if you need a drm remover,you can find what you need here.
    [ed: link removed]

Post a Comment

Comments are moderated according to our moderation policy. Sometimes comments are delayed by our spam filter. We try to release them as soon as possible.

Your email is never published nor shared. Required fields are marked *

*
*