mrsilver76

Export m3u playlists from Plex Media Server

Jun 28th, 2025 (edited)
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VBScript 4.01 KB | Source Code | 0 0
  1. '
  2. ' Simple script to export all playlists from Plex and save them in the
  3. ' same folder that this script is run from.
  4. '
  5. ' Instructions for use:
  6. ' 1. Modify serverUrl to match that of your Plex server.
  7. ' 2. Set token to be your Plex API token.
  8. ' 3. Save this as "export_plex_playlists.vbs".
  9. ' 4. Double-click on the file, click on Okay and it'll run silently. You'll get a pop-up at the end.
  10. '
  11. ' Notes:
  12. ' - Runs on Windows only.
  13. ' - Existing m3u files in the same folder will be deleted first.
  14. ' - Playlists are exported in UTF-8 format.
  15. ' - Playlists are simple M3U format only. Extended M3U would require a lot more code!
  16. '
  17. ' Configuration starts here:
  18. '
  19. ' serverUrl = The address and port number of your Plex server. Only http:// is supported, so you need
  20. '             to have "Preferred" set for "Secure Connections" within Plex. Use localhost if you are
  21. '             running this script on the same machine that hosts your Plex server.
  22.  
  23. Dim serverUrl : serverUrl = "http://localhost:32400"
  24.  
  25. ' token = Your Plex API token, required to authenticate with Plex.
  26. '         See https://support.plex.tv/articles/204059436-finding-an-authentication-token-x-plex-token/
  27.  
  28. Dim token : token = "YOUR_PLEX_TOKEN"
  29.  
  30. ' You don't need to change anything below here!
  31. ' ------------------------------------------------------------------------
  32.  
  33. Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
  34.  
  35. Dim answer
  36. answer = MsgBox("This script will export all playlists from Plex at " & serverUrl & vbCrLf & vbCrLf & "Are you ready to start?", vbYesNo + vbQuestion, "Export Playlists")
  37. If answer <> vbYes Then WScript.Quit
  38.  
  39. DeleteExistingM3UFiles
  40. ExportPlaylists
  41.  
  42. MsgBox "All playlists successfully exported from Plex.", vbInformation, "Export successful"
  43.  
  44. Set fso = Nothing
  45. WScript.Quit 0
  46.  
  47.  
  48. Sub ExportPlaylists()
  49.     Dim playlistsXml, playlistNodes, playlistNode, i
  50.     Set playlistsXml = CallPlexApi("/playlists")
  51.     If playlistsXml Is Nothing Then
  52.         MsgBox "Unable to retrieve playlists from Plex." + vbCrLf + vbCrLf + "Have you configured your serverUrl and token correctly?", vbExclamation, "Export Failed"
  53.         WScript.Quit 1
  54.     End If
  55.  
  56.     Set playlistNodes = playlistsXml.getElementsByTagName("Playlist")
  57.  
  58.     For i = 0 To playlistNodes.Length - 1
  59.         Set playlistNode = playlistNodes.Item(i)
  60.         ExportPlaylist playlistNode.getAttribute("key"), playlistNode.getAttribute("title")
  61.     Next
  62. End Sub
  63.  
  64. Sub ExportPlaylist(key, title)
  65.     Dim xml, items, item, i, mediaPath, utf8File
  66.     Set xml = CallPlexApi(key)
  67.     If xml Is Nothing Then
  68.         MsgBox "Failed to get playlist items for: " & title, vbExclamation, "Export Error"
  69.         WScript.Quit 1
  70.     End If
  71.  
  72.     Set items = xml.getElementsByTagName("Part")
  73.  
  74.     Set utf8File = CreateObject("ADODB.Stream")
  75.     utf8File.Type = 2 ' Text
  76.     utf8File.Charset = "utf-8"
  77.     utf8File.Open
  78.  
  79.     For i = 0 To items.Length - 1
  80.         Set item = items.Item(i)
  81.         mediaPath = item.getAttribute("file")
  82.         If Not IsEmpty(mediaPath) Then
  83.             utf8File.WriteText mediaPath & vbCrLf
  84.         End If
  85.     Next
  86.  
  87.     utf8File.SaveToFile SanitizeFilename(title) & ".m3u", 2 ' Overwrite
  88.     utf8File.Close
  89.     Set utf8File = Nothing
  90. End Sub
  91.  
  92. Function CallPlexApi(endpoint)
  93.     Dim http, fullUrl
  94.     Set http = CreateObject("MSXML2.XMLHTTP")
  95.     fullUrl = serverUrl & endpoint & "?X-Plex-Token=" & token
  96.  
  97.     On Error Resume Next
  98.     http.Open "GET", fullUrl, False
  99.     http.Send
  100.     If http.Status = 200 Then
  101.         Set CallPlexApi = http.responseXML
  102.     Else
  103.         Set CallPlexApi = Nothing
  104.     End If
  105.     On Error GoTo 0
  106. End Function
  107.  
  108. Sub DeleteExistingM3UFiles()
  109.     Dim folder, files, file
  110.     Set folder = fso.GetFolder(fso.GetAbsolutePathName("."))
  111.     Set files = folder.Files
  112.  
  113.     For Each file In files
  114.         If LCase(fso.GetExtensionName(file.Name)) = "m3u" Then
  115.             file.Delete True
  116.         End If
  117.     Next
  118. End Sub
  119.  
  120. Function SanitizeFilename(name)
  121.     Dim invalidChars, c, i
  122.     invalidChars = Array("\", "/", ":", "*", "?", """", "<", ">", "|")
  123.     SanitizeFilename = name
  124.     For i = 0 To UBound(invalidChars)
  125.         c = invalidChars(i)
  126.         SanitizeFilename = Replace(SanitizeFilename, c, "_")
  127.     Next
  128. End Function
  129.  
Advertisement
Add Comment
Please, Sign In to add comment