Welcome to our forums! Please take a few moments to read through our Community Guidelines (also conveniently linked in the header at the top of each page). There, you'll find guidelines on conduct, tips on getting the help you may be searching for, and more!

Episode Title from File Name

profplumpprofplump Posts: 242 Plex Pass
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

Comments

  • MazeMaze Posts: 14
    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?
  • profplumpprofplump Posts: 242 Plex Pass
    Maze wrote:

    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


    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.

    Maze wrote:

    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?


    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.
  • profplumpprofplump Posts: 242 Plex Pass
    edited March 2012
    profplump wrote:

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


    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
  • retroseriestvretroseriestv Posts: 167 Plex Pass
    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.
  • profplump wrote:

    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


    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.
  • profplumpprofplump Posts: 242 Plex Pass

    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.


    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).
  • arbergarberg Posts: 8 Plex Pass
    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
  • sjstevesjsteve Posts: 1 Plex Pass

    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.

  • kamakamadaunkamakamadaun Posts: 11 Plex Pass

    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?

  • ZeroQIZeroQI Posts: 1,158 ✭✭✭

    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

Sign In or Register to comment.