22 January 2023

At home my primary machine is a desktop running Windows (I’m a gamer) but my laptop that I use for travel and other stuff runs Linux. I had been surviving on a basic copy-my-music-to-Linux configuration and never bothered to create the playlists. It’s exceedingly annoying that there is no feature to export all of your playlists at once, or import them all at once with Rhythmbox. I have over 100 I’ve created over the years and that’s far too many to do manually. I figured there must be a way to do this via PowerShell so I set out to figure it out.

Mind you, I’m using an old version of iTunes, 12.0.1.26. When they updated the GUI to have that huge empty space I said forget it… rolled back… and ignore the periodic messages to update. I’m blocked from buying through the store because of the version but that’s fine, I don’t actually buy via their store and don’t plan to. But just keep in mind this may not work if they’ve changed the XML structure for the library in newer versions.

Rhythmbox version is 3.4.4

Going into this, I thought it was going to be difficult but it turned out to be quite simple. The longest part was examining the Libary.xml file to figure out it’s structure. Turns out, it’s very well structured and easy to access each entry with dot notation. I built a few dummy playlists in Rhythmbox so I could see it’s structure, as well as find the file where they were stored ( default dir:  /home/USERNAME/.local/rhythmbox/playlists.xml).

Initially I exported each playlist as a separate file in the format Rhythmbox wanted because I had installed a plugin that let you import them as .pls files. This turned out to be way too cumbersome with the amount of playlists I had so I revisited the playlists file for Rhythmbox and decided to write the entire file with the script. I left both methods in the script so you can do either or, in case you have an established Rhythmbox presence you don’t want to overwrite.

First thing you need to do is export your library to some file on your machine, File > Library > Export Library.

Update it’s path in the first line of the script below. Also change the two occurrences of “USERNAME” in the script with whatever your username in Linux is. Or, if you’re not using the standard Music path in your Home dir, update the entire path. Just be sure to keep the beginning f:// intact.

[XML]$file = Get-Content "C:\iTunesToRhythmbox\Library.xml"
$outpath = "C:\iTunesToRhythmbox\"

$xmlPlaylists = $file.plist.dict.array.dict

ForEach ($list in $xmlPlaylists) {
	$playlistName = $list.string[0].Replace("&","%26").Replace(" ","%20")
	If ($playlistName -eq "Library" -OR $playlistName -eq "Music" -OR $playlistName -eq "Podcasts") { continue }
	$trackIds = $list.array.dict.integer
	If ($trackIds.count -eq 0) { continue }
	$plPath = "$outpath\$playlistName.pls"
	$xmlPath = "$outpath\playlists.xml"
	Add-Content $xmlPath "  <playlist name=""$playlistName"" show-browser=""true"" browser-position=""180"" search-type=""search-match"" type=""static"">"
	
	$playListCount = 1
	ForEach ($id in $trackIds){
		$trackIndex = $file.plist.dict.dict.key.indexOf($id)
		$trackData = $file.plist.dict.dict.dict[$trackIndex]
		$trackName = $trackData.string[0]
        	# My iTunes music sits in a folder called iTunes (and I copy all of it's contents over to Linux)
        	# so I'm splitting the path based on that, as everything to the right of it will match the partial path in Linux.
		$filePath = ($trackData.string[-1] -split "iTunes/")[1]
		Add-Content $xmlPath "    <location>file:///home/USERNAME/Music/$filePath</location>"
		$playListCount++
	}
	Add-Content $xmlPath "  </playlist>"
}

If you’re using the method of replacing the entire playlists file, it goes in (default directory):  /home/USERNAME/.local/share/rhythmbox/playlists.xml.  Otherwise, use a plugin to import the .pls files one by one….

The biggest issue I ran into that took a while to figure out why it wasn’t working, was certain characters that Rhythmbox, or Linux, didn’t like. At first when I was modifying the playlist file it wasn’t showing any of my playlists. Then after I closed out it had re-written the file entirely! It turns out if & and spaces are in the names or paths, it doesn’t like it and sees it as corrupt, and recreates it. So I added a couple of character replacements in the script and it finally worked.

Also note that I’m skipping over playlists titled “Library”, “Music”, and “Podcasts”.  These are three of the built-in playlists that iTunes creates and the first two are playlists that contain EVERYTHING.  Definitely don’t need playlists that just include my entire music library.  I also don’t copy over any Podcasts so no need for that, either.

 


There are no comments.



You must be logged in to post a comment.

Links

RSS 2.0 Feed

Support

Brave Rewards
This site supports Brave Rewards. Please consider tipping or adding it to your monthly contributions if you find anything helpful!

For other ways: Support

Support this blog! If you have found it helpfu you can send me crypto. Any amount is appreciated!
ETH: 0xBEaF72807Cb5f4a8CCE23A5E7949041f62e8F8f0 | BTC: 3HTK5VJWr3vnftxbrcWAszLkRTrx9s5KzZ | SHIB: 0xdb209f96dD1BdcC12f03FdDfFAD0602276fb29BE
Brave Users you can send me BAT using the browser.