Despite my affinity to Linux and free software, I’m a sucker for Apple hardware. In particular, I have an iPhone and a few old iPods. To keep these devices reliably synchronized with my music collection, I’m stuck using iTunes in a Windows VM. I’ve built up some playlists I like, so I wanted to export these playlists from iTunes (version 10.6) and import them into Rhythmbox (2.96) on Ubuntu.
I put together a Python script that converts an exported iTunes playlist into Rhythmbox’s format. It works well if you move entire collection from Windows to Linux, preserving the directory structure. I just wanted to share it here with some brief instructions on how to use it.
Step 1: Export the iTunes playlist (before killing your Windows partition!). Simply right click the playlist and pick “Export…”. Then, save it to a text file. Move this file with your music collection to your Linux box.
Step 2: Download the script (importpl.py, 1.5 KB) and modify the “win_path” and “lin_path” variables (lines 5 and 6 below) to reflect the proper paths. The script will take care of changing Windows’ backslashes to forward slashes.
#!/usr/bin/python ############################################################ #USER CONFIGURATION: SET YOUR PATHS win_path = 'C:\\Users\\Scott\\Music\\' #be sure to use double backslashes! lin_path = '/home/scott/Music/' ############################################################ import codecs import csv import sys import os file_in = sys.argv[1] file_out = file_in + ".pls" filename = file_in.split('/')[-1] parts = filename.split('.') if len(parts) > 1: pl_name = ".".join(parts[:-1]) else: pl_name = filename f1 = codecs.open(file_in,'r','utf-16') lines = [] for line in f1: lines.append(line.split('\t')) f1.close() f2 = codecs.open(file_out,'w','utf-8') f2.write('\n') f2.write('X-GNOME-Title={0}\n'.format(pl_name)) f2.write('NumberOfEntries={0}\n'.format(len(lines)-1)) count = 0 for i,l in enumerate(lines[1:]): win_file = l[-1].strip() title = l[0].strip() lin_file = win_file.replace(win_path,lin_path) lin_file = lin_file.replace('\\','/') if os.path.exists(lin_file): count += 1 try: lin_uri = 'file://{0}'.format(lin_file.replace(' ','%20')) #write it out f2.write('File{0}={1}\n'.format(count,lin_uri)) f2.write('Title{0}={1}\n'.format(count,title)) except: print "Couldn't write a file " + title else: print 'Could not find ' + lin_file f2.close() print "Wrote {0} songs".format(count)
Step 3: Run the script to convert the iTunes playlist to the Rhythmbox “.pls” format. Here’s an example usage of the Python script:
$ python importpl.py iTunesPlaylist.txt LinuxPlaylist.pls
Step 4: Import your playlist into Rhythmbox. Just go to Music > Playlist > Load From File…
That got my playlists migrated to Ubuntu without too much trouble. Your mileage may vary. Leave a reply if you came up a better technique!
Is there anyway to do the same for Banshee Player ?
Yes, Banshee should be able to read the .pls file created by this script. If you test it, let me know how it goes.
OMG Thank you so much for this script! You’ve just saved me a week’s worth of nagging from my wife.
I’ve just used this to migrate 25 playlists from iTunes 11.4 (Windoze 7) to Rhythmbox 3.0.2 (Ubuntu 14.04).
I just had to make two slight alterations to the script (probably because my files are on a Samba share using a ‘smb://’ path):
(1) Line 46:
if os.path.exists(lin_file):
to:
if 1==1:
so that it was always true. For some reason, no matter what I did, it said the file couldn’t be found, even though the path was correct. Could well be because of (2) below.
(2) Line 49:
lin_uri = ‘file://{0}’.format(lin_file.replace(‘ ‘,’%20′))
to:
lin_uri = ‘{0}’.format(lin_file.replace(‘ ‘,’%20′))
so that it wasn’t prefixing each file’s path with ‘file://’.
The only issues I ran into where a few cases of special symbols but I was able to deal with each one at a time (mainly with a global find and replace).
There were a few odd album and/or song titles (and therefore paths in some cases too) and a few artists with accents and such like in their names.
Other than that, all was good. Saved us day’s of effort.
Thank you!
The iTunes I want to migrate playlists from is on a Mac. Would I need an entirely different script? (I’ve used Ubuntu but am still a novice when it comes to scripts and code and the like)