Home Page
  • October 09, 2024, 12:18:45 am *
  • Welcome, Guest
Please login or register.

Login with username, password and session length
Advanced search  

News:

Official site launch very soon, hurrah!


Author Topic: Plex Playlist Importer  (Read 122738 times)

Dakusan

  • Programmer Person
  • Administrator
  • Hero Member
  • *****
  • Posts: 544
    • View Profile
    • Dakusan's Domain
Plex Playlist Importer
« on: November 10, 2014, 11:20:12 am »


Description: Import playlists into Plex
Information: My music directories have been growing for over 2 decades in a folder based hierarchy, often using playlists for organization. Plex’s music organization is counterintuitive to this organizational structure, and Plex currently does not have an easy way to import external playlists. Hence this script was born :-)
Languages: Python
Logged

Dakusan

  • Programmer Person
  • Administrator
  • Hero Member
  • *****
  • Posts: 544
    • View Profile
    • Dakusan's Domain
Re: Plex Playlist Importer
« Reply #1 on: November 11, 2014, 01:11:48 pm »

If running in Cygwin with the Windows install of Python 3, the following could be put into a shell script to automatically convert the cygwin path to the windows path. This helps with tab completion when typing a path

Code: [Select]
./PlexPlaylistImporter.exe "`cygpath -wp "$1"`" "$2"
So this could be called like:

Quote
./SCRIPT_NAME /cygdrive/c/playlist.path "Plex Playlist Name"
« Last Edit: November 11, 2014, 01:13:21 pm by Dakusan »
Logged

Dakusan

  • Programmer Person
  • Administrator
  • Hero Member
  • *****
  • Posts: 544
    • View Profile
    • Dakusan's Domain
Re: Plex Playlist Importer
« Reply #2 on: November 27, 2014, 04:54:52 am »

You just made me realize something very important that [I think] I need to add in to the documentation. The songs you want to play have to already exist in the Plex database.

When you add a song to Plex (through Plex), it stores the song's full file path in the Plex database. What my script does is derive the full paths for files in a playlist file you give it, checks those paths against plex's database, and then adds the matches in the proper order into the Plex playlist you specified.

So for this reason, the script must be ran on the same computer running the Plex server so that it can match the paths.

So to more specifically answer your question, if Plex supports loading files in through smb shares (accessed via "\\" in Windows), then I believe my program should be ok with it too.

I have not specifically tested this, so let me know if you think my program is not handling it correctly and I'll look into it.

Out of curiosity, was that MortScript thing you mentioned previously related to Plex or Python?
Logged

clowg

  • Jr. Member
  • **
  • Posts: 12
    • View Profile
Re: Plex Playlist Importer
« Reply #3 on: November 28, 2014, 09:15:27 pm »

I am running the script on the Plex Media Server computer (Windows 7).
The media in question is all in the PMS library, but it is present on a different server.

When I run your script I get:

C:\7MC>plexplaylistimporter "top 501 songs.m3u" "Top500Rock" "D:\Media3\AppData\Local\Plex Media Server"
ListImporter 'Winamp playlist': Cannot find file listed in playlist (must be relative to the playlist): ÿþ\ \ D o r i s 4 \ M u s i c \
 R O C K \ D a v i d   B o w i e \ Y o u n g   A m e r i c a n s \ 0 8   -   F a m e . m p 3

Does that command line and subsequent output make sense to you?
Logged

clowg

  • Jr. Member
  • **
  • Posts: 12
    • View Profile
Re: Plex Playlist Importer
« Reply #4 on: November 28, 2014, 09:17:36 pm »

Out of curiosity, was that MortScript thing you mentioned previously related to Plex or Python?

Sorry about that.  I mixed up two different sources for Plex Playlist import scripts.  mortscript.exe is used by the other one I am trying to get working at the same time as yours.  But that is failing as well becuase mortscript.exe wont run on my Windows 7 32-bit computer.
Logged

Dakusan

  • Programmer Person
  • Administrator
  • Hero Member
  • *****
  • Posts: 544
    • View Profile
    • Dakusan's Domain
Re: Plex Playlist Importer
« Reply #5 on: November 29, 2014, 12:25:28 am »

The new version I just uploaded should fix the problem, thanks for the report
Logged

clowg

  • Jr. Member
  • **
  • Posts: 12
    • View Profile
Re: Plex Playlist Importer
« Reply #6 on: November 29, 2014, 04:56:20 am »

Thanks.  I just tried the new version.  I still get this error:

C:\7MC\PlexPlaylistImporter>PlexPlaylistImporter.exe  c:\7mc\Top501Songs.m3u Top500Songs "D:\Media3\AppData\Local\Plex Media Server\Plug-in Support\Databases\com.plexapp.plugins.library.db"

ListImporter 'Winamp playlist': Cannot find file listed in playlist (must be relative to the playlist): ÿþ\ \ D o r i s 4 \ M u s i c \ R O C K \ D a v i d   B o w i e \ Y o u n g  A m e r i c a n s \ 0 8   -   F a m e . m p 3

So I tried re-saving the m3u file as UTF-8 format, and that changed the error to:

ListImporter 'Winamp playlist': Cannot find file listed in playlist (must be relative to the playlist): \\Doris4\Music\ROCK\David Bowie\Young Americans\08 - Fame.mp3

See the weird character before the server name?  Is that the problem?  There is no visable character there.  I tried adding a carraige return, but then the program errored with:

ListImporter 'Winamp playlist': Cannot find file listed in playlist (must be relative to the playlist): 

Make any sense?
Logged

clowg

  • Jr. Member
  • **
  • Posts: 12
    • View Profile
Re: Plex Playlist Importer
« Reply #7 on: November 29, 2014, 05:13:46 am »

I just tried saving the m3u file as ANSI encoding, and now its getting further.  I get this error at what looks like the end of the process:

C:\7MC\PlexPlaylistImporter>PlexPlaylistImporter.exe  c:\7mc\Top503Songs.m3u Top500Songs "D:\Media3\AppData\Local\Plex Media Server\Plug-in Support\Databases\com.plexapp.plugins.library.db"

Plex playlist is not already created. Would you like to create it now (y/n)? y
DB Error: unsupported operand type(s) for /: 'NoneType' and 'int'
Logged

Dakusan

  • Programmer Person
  • Administrator
  • Hero Member
  • *****
  • Posts: 544
    • View Profile
    • Dakusan's Domain
Re: Plex Playlist Importer
« Reply #8 on: November 29, 2014, 03:06:56 pm »

Ugh, I was noticing those non-path characters at the beginning of the songs you pasted on your first post and decided to assume it was a problem with your copying from your console and not the file. If you are actually seeing those extra characters before the error path on your console, those are byte order markers, which are placed at the beginning of text files to specify the text encoding the file is in. All of my m3u files were utf-8 encoded with no BOM, so I thought that was the format's official encoding specification. So if you save as UTF8 without the BOM, there shouldn't be a problem (UTF8 is ANSI/ASCII compatible, so what you did is fine too as long as there are no file paths with characters outside the standard ANSI range) . If this happens to other people I may look into fixing it, but dealing with text encodings can be a pita.

I don't know what text editor you are using (notepad++?), but I personally recommend Editpad Pro. It's list of options is inexhaustible, and it lets you specify options like encodings and BOMs on a per file-extension basis. [Edit: I always forget to mention, its Basic version is free, but is missing some important stuff like syntax highlighting :-\ ]

As for the next error, I received that for the first time last night too when adding the absolute paths code. However, when rerunning the script a second time, I was unable to reproduce the error, so I don't know what caused it. Can you try running the same command again and see if it works the second (or third? :-) ) time? If not, I'll try and see if I can reproduce, and if still not, we may want to exchange instant message information to get it figured out. My current first-thought guess is that it's having an issue getting the Playlist's ID after creating it, so since it is already created on the second run, it cannot run into that problem.
« Last Edit: November 29, 2014, 03:11:19 pm by Dakusan »
Logged

clowg

  • Jr. Member
  • **
  • Posts: 12
    • View Profile
Re: Plex Playlist Importer
« Reply #9 on: November 30, 2014, 03:49:57 am »

Hi.

Ive tried the same command 2 more times and it produced the same error each time.
Logged

clowg

  • Jr. Member
  • **
  • Posts: 12
    • View Profile
Re: Plex Playlist Importer
« Reply #10 on: November 30, 2014, 04:19:35 am »

I'm happy to arrange a time to IM and see if we can sort it.
What time zone are you in?
Logged

Dakusan

  • Programmer Person
  • Administrator
  • Hero Member
  • *****
  • Posts: 544
    • View Profile
    • Dakusan's Domain
Re: Plex Playlist Importer
« Reply #11 on: December 01, 2014, 02:49:05 pm »

I think I found the problem. The plex database can apparently have NULL values for a song's duration. If you are running from code, replace line 109:
Code: [Select]
DBFileDurations.append(Val[1])with:
Code: [Select]
DBFileDurations.append(Val[1] if Val[1] is not None else 0)
If that doesn't work, or you aren't running from code, I'll shoot you an email with my IM info (I see you provided it to the forum). I am in CST, but I have very odd hours so that doesn't necessarily mean much ^_^.

If that DOES work, let me know so I can make a final code commit.
Logged

clowg

  • Jr. Member
  • **
  • Posts: 12
    • View Profile
Re: Plex Playlist Importer
« Reply #12 on: December 01, 2014, 04:08:08 pm »

I am running from the binary. I'm not a coder  :(

Email me yr contact info - Skype OK?
Logged

clowg

  • Jr. Member
  • **
  • Posts: 12
    • View Profile
Re: Plex Playlist Importer
« Reply #13 on: December 04, 2014, 03:59:03 pm »

Hi there,I have given Plex a couple of days to run background refresh tasks etc, and it hasn’t helped. 

The two Playlists that I have imported cannot display the track listing - either in Web or PHT views. 
PHT tries to load the view for about 30 seconds and then comes up with an error - which I can't remember the exact wording of at the moment. 

It gave me a black screen a couple of times, which I could 'exit' back to the main Playlist list after it timed out on trying to load the track list. 

It can play the Playlist though.  Just select the Playlist with the arrow keys and then press play and it plays in the order in the original playlist file. 

Of course without being able to enter into the Playlist track listing I can't select Random Play.
Is there anything else I can do to track down what has gone wrong with the imported Playlist? There is obviously something different about it compared to the manually created ones which work fine.

Cheers,
Logged

Dakusan

  • Programmer Person
  • Administrator
  • Hero Member
  • *****
  • Posts: 544
    • View Profile
    • Dakusan's Domain
Re: Plex Playlist Importer
« Reply #14 on: December 04, 2014, 04:04:48 pm »

I've only tested this on my 2 home servers, both of  which are running the latest Plex software. I've imported dozens of  playlists just fine, but all the ones I use have the songs stored  locally.
 
My personal guess at the moment from the variables I have been given  (just as likely to be wrong as right): Plex is trying to verify the  existence of all files in the playlist as part of the startup process  before it displays anything, or wanting to pull something  small out of the files for some reason. Each networked file is probably  taking like half a second to verify, so the list ends up timing out.
 
The background stuff you are talking about should only run once the  first time you add new content to plex (e.x. import a directory into  your plex database). You can see when that start and finished on the  plex web server interface, so that shouldn't be the  problem. Once it's loaded, it's in the database for instant lookup.
 
Things I would personally try to debug the problem to start narrowing down problems:
  • Make sure your plex server is up to date
  • Try manually creating a playlist with a few local songs to see what happens (Not using my script)
  • If that works, make a .m3u with the same songs, and see if it imports ok into a new playlist
  • Try doing the same thing with remote songs
  • Do incrementally larger imports into different playlists (50 songs, 100 songs, 200 songs...)
If it fails during one of those steps, that can give a clue to the problem.
 
Oh, it would also help to Google the error you get. That is always the best starting point to debugging a problem.
« Last Edit: December 04, 2014, 04:06:53 pm by Dakusan »
Logged