Episode Title from File Name

scanner-agent-dev

#1

Not really a scanner, but a quick way to replace all those unnamed episode titles in Plex with the name of the underlying file -- I use it for things like special features that aren't listed in TheTVDB but that I have named on disk. Thought I'd share it in case anyone else thinks it's useful.



The script will only rename files named "Episode XX", and will only rename them if the underlying file has a 2+ character name that does not contain an "Episode XX". So it's safe to run repeatedly and will not continuously rename already-named file, or screw up the name of properly scanned/matched episodes.



The script is designed for use in a TV Show hierarchy and probably does not work for Movies/etc. It also does not support episodes that contain more than one media file (i.e. ISOs, multi-part episodes, etc.).



It's a perl script (in spite of the filename extension -- the forum doesn't like .pl files). Usage is simple:


perl ./renameFromDisk.txt host:port username password section_number





The username and password can probably be empty/garbage if you're in the "local" zone where the PMS HTTP GUI allows access without authentication, though I haven't tested that.



This script doesn't support MyPlex auth tokens (only 0.93-style local username/password auth), but it should be fairly easy to patch if you need that feature.



--

EDIT

--



Forget the attachment. Use the always-current version from GitHub:

https://github.com/profplump/Plex-Rename-from-Disk


#2

A question from a complete newbie ...



I guess this could be very useful for me, I have a lot of music videos in a TV-section;



/Gary Moore

/Season 1

Gary Moore - s01e01 - After the War.m4v

Gary Moore - s01e02 - Before I Run.m4v

Gary Moore - s01e03 - Military Man.m4v



In the PMS they appear as Episode 01, Episode 02 ... which I would like to replace with the proper names.



However, I don't understand how to use the script, I get the "Unable to fetch data for section ..." message.



I've no experience or perl-scripts, so maybe I'm doing it all wrong, running the script from the terminal.

Is "section_number" in the command line supposed to be replaced with a real number?


#3

[quote name='Maze' timestamp='1325705882' post='228320']


A question from a complete newbie ...



I guess this could be very useful for me, I have a lot of music videos in a TV-section;



/Gary Moore

/Season 1

Gary Moore - s01e01 - After the War.m4v

[/quote]




This script will work for that, but you'll need to add a filter to get rid of the " - s01e01" part if you don't want that to show up. This regex would work:


$file =~ s/\s+\-\s+S\d+E\d+//i;





If you don't care about the sXXeYY bit in your episode names you can ignore this change.


[quote name='Maze' timestamp='1325705882' post='228320']


I've no experience or perl-scripts, so maybe I'm doing it all wrong, running the script from the terminal.

Is "section_number" in the command line supposed to be replaced with a real number?

[/quote]




Yes, the section number for your TV section. If you're not sure what that is, take a look at the URL when you're browsing the section in the PMS web GUI. It will be something like:

http://my.pms.ip:32400/manage/index.html#/library/sections/4



The section number this script needs is that digit from the end.


#4

[quote name='profplump' timestamp='1326185870' post='230212']


This script will work for that, but you'll need to add a filter to get rid of the " - s01e01" part

[/quote]




In fact, I've posted a new version that contains that change, and moved the source to GitHub in case I make future improvements:



https://github.com/profplump/UberZach-Video/blob/master/renameFromDisk.pl


#5

I am interested in your solution as it would be very useful for me in order to catalogue all shows that do not exist in thetvdb and that are not suitable to be uploaded to this page because they are personal. It would spare me many hours of work if I do not have to edit the episodes one by one and they can be substituted by the name of the file they refer to.

The problem is that my programming notions are zero and I am not able to understand how to make your solution work. I would therefore be very grateful if you could explain in a few steps how to install it and make it work for someone like me who knows nothing about Perl programming, scripts execution, etc.


#6

[quote name='profplump' timestamp='1326186523' post='230215']


In fact, I've posted a new version that contains that change, and moved the source to GitHub in case I make future improvements:



https://github.com/p...nameFromDisk.pl

[/quote]




Thanks for the script. A few comments:



The script must be run from Perl which uses the same filename syntax as the Plex server. Otherwise, basename() will fail to remove paths from filenames. For example, it happens when using a Windows Plex server and Cygwin perl. I used "$file =~ s/^.\//;" as a workaround.



It will fail to remove the last character of 4 character file extensions such as .mpeg or .divx. I changed the file extension removal pattern to:

$file =~ s/.\w{2,4}$//;



It's easy to alter the Perl code to do other stuff, such as reading descriptions from text files. However, **warning
*: don't put invalid UTF-8 into the database. The web interface won't be able to edit it, and in this script XMLin() will kill the script with a "Cannot decode string with wide characters" error. I got around this problem via "use Encode;" and "XMLin(encode("UTF-8", $content))". That's wrong, but it allows the script to work well enough to re-write what I needed to remove invalid XML.


#7

[quote name='dreamlayers' timestamp='1333384435' post='251712']


Thanks for the script. A few comments:



The script must be run from Perl which uses the same filename syntax as the Plex server. Otherwise, basename() will fail to remove paths from filenames. For example, it happens when using a Windows Plex server and Cygwin perl. I used "$file =~ s/^.\//;" as a workaround.



It will fail to remove the last character of 4 character file extensions such as .mpeg or .divx. I changed the file extension removal pattern to:

$file =~ s/.\w{2,4}$//;



It's easy to alter the Perl code to do other stuff, such as reading descriptions from text files. However, **warning
*: don't put invalid UTF-8 into the database. The web interface won't be able to edit it, and in this script XMLin() will kill the script with a "Cannot decode string with wide characters" error. I got around this problem via "use Encode;" and "XMLin(encode("UTF-8", $content))". That's wrong, but it allows the script to work well enough to re-write what I needed to remove invalid XML.

[/quote]




I appreciate your input.



Yes. I am assuming File::basename() "just works". I'll give some thought to better cross-platform support; there's probably some module that provides a cross-platform basename() function. But for the moment I'm assuming everyone runs UNIX on all their systems; after all, I did write this thing in perl.



As for the media file names themselves, yes, there are many cases the will not be covered. The 4-character extension is reasonable and pretty common, so I'll add that, but generally speaking the extension-truncator will fail for non-standard extensions, so I'm not terribly interested in supporting more exotic extensions (unless you've got a suggestion to handle a more general case).



And yes, I'm aware the script doesn't handle wide characters well. Heck, it doesn't even handle inbound XML well. If you ever get to a patch to do real XML parsing/encoding/etc. I'd be all for it. I've thought about fixing it, but "support use cases other than my own for the plex-rename-script" is around item 187 on my to-do list, so I'll probably never add that myself (unless it becomes a problem in my personal usage).


#8

The script has been moved (by profplump), I found it again here:



https://github.com/profplump/UberZach-Integration



I missed a perl package but managed to install it using help from here: http://stackoverflow.com/questions/10524840/cant-locate-lwp-simple-pm-in-inc



Then I ran the script, and it worked, it changed the names of the unnamed episodes ! Awesome, thx profplump.



Cheers, Alex


#9

This script is a godsend, thank you!

I made a small tweak to remove the series name from the description written to Plex -- I found it a bit redundant since the series title and season is usually at the top of the screen on my clients anyway.

I just split the contents of $file on / - / and used the second array element as the argument to uri_escape instead of just $file:

                        my @fields = split(/ - /, $file);

                        my $encoded = uri_escape($fields[1]);

That turned this file:

Michigan Football - S2011E1126 - Ohio State.mp4

Into the episode title:

Ohio State

Instead of:

Michigan Football - Ohio State

Thank you again for your great work on this!

--Steve.


#10

Does this still work? The plex URLs seem to be named differently now so if it still works how do you get the section number?

Or is there another way to do this?


#11

My absolute series scanner does provide the episode title from the filename until the agent updates it, output readable custom logs and support season numbering too. Please give it a try and feedback


#12

I discovered Plex just recently and found it a very good platform to build my media library on especially for it's look and feel, however it does have it's drawbacks, it works well with tv's and movies, but it does have the drawback with .iso, .ts files in such, and Plex's resolve not to support an tried and true existing format is quite frankly disturbing and the thought of having to convert them over to mp4 files (and loose some features) is an absolute NO GO....however the "Plex Series Scanner with Disc Image Support" overcomes that hurdle, then the fact that the Plex Media Player..doesn't support those files, again overcome with "External Plex Player Support" program, now that those hurdles have been overcome....I'm shot down again, Plex and most databases out there don't know how to handle "Music Videos" . I have an incredible amount of music videos (Concerts, documentaries, individual performances, etc). so based on what I needed, I set up my Music Videos Library as a "TV Shows" type library and renamed it "Music Videos", with the "Artist" now as the "TV Show". the files are now the episodes. With Shows hidden so it just shows the number of episodes or "files / videos" by an individual artist
Folder: Simply Red (Artist instead of TV Show)
Episode 1: Simply Red - 1x01 - Discovering Simply Red (2015).mp4
Episode 2: Simply Red - 1x02 - Home (Live In Sicily) (2014).iso
The problem I'm facing now is that I have to rename all the episode titles in Plex manually when all I want is a script or program to run for the specific library and not the entire media database that will take the filename and insert it into the title minus if possible the "Simply Red - 1xXX -" and of course the ".ext", so it leaves
Artist (Folder) Simply Red
Episode 1: Discovering Simply Red (2015)
Episode 2: Home (Live In Sicily) (2014)
But I don't really care if it put's the whole filename and I have to edit...it's just the shear thought of cut/paste/edit every sing title and title sort field individually sucks.
After reading your description of your script and seeing that you had something that might help "Plex-Rename-from-Disk", but the link in your article doesn't work. Any thoughts. Thanks for any help you may provide.


#13

my absolute series scanner does take the episode title in filename s to populate it and support season numbering


#14

How does absolute series scanner rename the episode title in Plex to the name of the file on my hard-drive?

I've set up the database as "Music Videos" (It is based on a TV Shows Library format, where artist replaces TV Show and the filename is the episode). Note: I hide "Show Season" so I don't get the "Season" listed, Just the Artist with the number of episodes (files) listed below.
.
Example: I have the Artist "10cc"
The layout is
Folder: 10cc
Filename: 10cc - 1x01 - Clever Clogs (Featuring Graham Gouldman And Friends) (2008)
10cc - 2x02 - In Concert BBC Studios (1974-08-21)
10cc - 3x03 - In Concert BBC Studios (1974-08-21)
Note: The filename is listed as using this structure Artist - SeasonxEpisode - Filename
Note: In my case the Season happens to be which Hardrive the file is stored on.

Plex reads it as 10cc (Artist) on Main Library Page and lists the number of episodes (video files) attributed to that artist. When clicking on the artist, I move to the sub-directory where all the episodes (Files) are listed. However Plex lists them as, Episode 1, Episode 2, etc....however I want the episode listed as the filename. Preferably like the following

Clever Clogs (Featuring Graham Gouldman And Friends) (2008)
In Concert BBC Studios (1974-08-21)
In Concert BBC Studios (1974-08-21)

Any thoughts


#15

The scanner keeps all after the ep number but remove the serie name and all in brackets and crcs...
then it adds the ep with the title: tv_show = Media.Episode(show, season, epn, title, year)
Did my scanner not add the title like "In Concert BBC Studios (1974-08-21)" ?


#16

Excellent, The Scanner worked perfectly, what I made a mistake was choosing the wrong type agent. Re-ran it again and I got the titles.

Thanks