Export and Import Playlists in Plex


I love Plex!  It is a great media server that has allowed me to have my movie collection on demand (like Netflix) as well as my music collection.  I love being able to play my music at home or on the go.  It's like my own private streaming service.  So, while I do love Plex, I have run into an issue.  It doesn't provide a way to export/import playlists.  Say I've got Plex Media Server installed on a Windows machine and I want to migrate it to a Linux machine.  There isn't a direct migration path but, for most things this isn't an issue - I just add the catalog of movies and music and the Plex Server rebuilds everything... everything but the playlists.  I have playlists with over a hundred songs.  Rebuilding those by hand would be terribly time consuming.  So how do you export your playlists from Windows and import them into Linux?  Well, it's not as easy as it should be but it's not too difficult once you know how. After finally figuring out how to do it, I wanted to share the process.  FYI - It is assumed that all the work will be done on the Linux server. 

Prep Work 

  1. Get all of the items into a library in your new Plex server.  This means copying or otherwise getting all of your media content onto the new Linux Plex server.
  2. Get the Plex Token and the SectionID.  NOTE: If you're using 2 separate Plex accounts (you have to log into each separately), you may need 2 Plex Tokens – 1 for the source Plex server and 1 for the target Plex server.  I have both of my Plex servers on the same account so I only need the one.
    1. In the Plex Application, open a Plex Item (like a song or a movie) and click on the the 3 dots on the far right (this won't work for albums, only individual songs or movies) and select Get Info
    2. Click on the Get XML link at the bottom of the Get Info window.
    3. The URL for the XML page that comes up will have the Plex Token embedded in it.  The end of the URL will have a section that says X-Plex-Token=<jumble of letters and numbers>.  This is the Plex Token.
    4. The first line in the XML file (that starts with <MediaContainer ) will have an entry that says:  librarySectionID=<a number in quotes>  That number is the SectionID. 
  3. Get Postman.  Postman is an API program – it allows you to send a request to the Plex API and it will give you the response.  It will also allow you to update your Plex Server.  This is the tool that will let you import you playlists into your new Plex server.  You download it at https://www.postman.com/downloads/ 
  4. Get xmlstarlet (Linux utility).  This will allow you to pull the needed information out of your playlist exports.

 














Process

Now that you've got your prep work done, let's get to work and export a playlist from our Windows Plex server, update it, and import it into our Linux Plex server.
  1. Start up Postman.
  2. Use Postman to get the list of playlists 
    1. Enter a GET request with the following syntax: http://localhost:32400/playlists?X-Plex-Token=<PlexToken> 
    2. Localhost is the hostname for the SOURCE Plex server (you can use the name or the IP address) 
    3. <PlexToken> is the Plex Token you found in the Prep section
  3. An XML formatted list of all of your playlists will be shown. 
  4. Get the playlistID 
    1. Each playlist has a section of XML code that starts with <Playlist ratingKey=
    2. The ratingKey is the playlistID 
    3. Deeper into the XML, you'll see a tag called title - this is the name of the playlist 
    4. Get the ratingKey for the playlist you want to export 

  5. Enter another GET request into Postman using the playlistID you found in the previous steps.
    1. Use the following syntax: http://localhost:32400/playlists/<playlistID>/items?X-Plex-Token=<PlexToken>  
    2. Localhost is the hostname for the SOURCE Plex server (you can use the name or the IP address) 
    3. <playlistID> is the playlistID/ratingKey you found above 
    4. <PlexToken> is the Plex Token you found in the Prep section

  6. Postman will return an XML file that describes the playlist.  Save this XML to a file. 
    1. Use a name like playlist.xml or whatever.
  7. Open a bash shell
  8. Go to the directory where you saved your file.  Use xmlstarlet to extract just the filenames for each of items in your playlist using the following syntax:
    1. xmlstarlet sel -t -v '/MediaContainer/Track/Media/Part/@file' <inputfile> > <outputfile> 
    2. For example, if I saved the XML to a file called playlist.xml, I would use the following command: xmlstarlet sel -t -v '/MediaContainer/Track/Media/Part/@file' playlist.xml > playlist.m3u 
    3. This creates an m3u file that is a list of the full pathname of your items (songs or movies)
  9. Use a text editor to make any changes necessary to your filenames (If the path is wrong, the import will fail).  Converting from Windows to Linux, at the very least, you'll need to convert your \ to / 
    1. For example – I converted the path names from C:\users\me\Music\ to /home/me/Music 
    2. Using a text editor that has the Replace All command is very helpful 
  10. Using Postman, import the new playlist into your new Plex server.
    1. Use the POST command with syntax like this: 
    2. http://localhost:32400/playlists/upload?sectionID=<sectionID>&path=/home/park/Downloads/<playlistfile>&X-Plex-Token=<PlexToken> 
    3. Localhost is the hostname for the TARGET Plex server (you can use the name or the IP address) 
    4. <sectionID> is the sectionID you found above 
    5. <PlexToken> is the Plex Token you found in the Prep section
Assuming all goes well (Postman will return a value of 1 if it works), you will have your playlist imported into your new Plex server with the name matching the name of the input file.   So, if you used playlist.m3u as your input file, your playlist will be called "playlist".

This may look intimidating but once you get through it, you'll find it's pretty simple.

Good luck!

Comments