Changing TV Show Naming Conventions

Changing TV Show Naming Conventions

From Plex

Jump to: navigation, search

There are two alternatives to have your TV shows correctly scanned by Plex:

1. rename your files to comply with the default naming structure.

2. change the way Plex scans your files by adding your own naming structure.


The first one is usually easier to achieve using the batch renaming features of the Automator. For example, if you have your files named "Lost 1-12.avi", replacing the "-" by an "x" in all the files will make them scannable ("Lost 1x12.avi").


The second solution is more complex as it requires you to understand regular expressions. Its advantage is that it enables you to scan your current library without making changes to it.

This is how you can do that :

Contents

Setting up/creating advancedsettings.xml

To begin with, we are going to use a file called advancedsettings.xml, which will allow us to talk to Plex in an easy way without having to delve into too much programming! The advancedsettings.xml file does not exist by default, and so it needs to be created by yourself. Navigate to:

~/Library/Application Support/Plex/userdata/

This is the location for the advancedsettings.xml file. If it is not there, then copy and paste the Sources.xml file, and change the name to Advancedsettings.xml. You will need to open this file and clear the contents. Once all this is done and you have a clean Advancedsettings.xml file, we can get started.


Theory behind how this works

What we are going to be doing is supplying Plex with a string, which it will use to search through our directory structure and return some information about the Season and Episode numbers for the TV Show. To begin with, Plex absolutely needs to have the Show Name as the top level folder under the source, so it can read it. So, for example, this is correct:


Source
:TV Show Name

And this is not ok:


Source
:Some other folder
::TV Show Name

Seems logical enough, it makes sense to group your video files by show name.

So, we are going to ask plex to have a look and pick out certain bits of information, and it already has the TV Show Name from the folder. What other information is required? Well, the season number and the episode number, and this will allow plex to find the information we need. Keep that in mind when we move on to the next part.

Regular Expressions

We are going to use something called regular expressions to tell plex where the Season Number and Episode Number is kept. You can read up on them more here:

Regular Expressions

Basically, we are going to create a string which Plex will use to search through the path to our TV Shows and get some information from it. Plex will try and match a pattern we give it.

Using advancedsettings.xml and Regular Expressions

To get started, open your new advancedsettings.xml file, and copy and paste the following:

<advancedsettings>


</advancedsettings>

Since the Advanced Settings file is an xml file, we will be writing everything in xml. We are going to be concentrating on the <tvshowmatching> part of things, but if you would like to know more about the other exciting things you can put in the advanced settings, head on over here: Advancedsettings.xml

Next, we need to tell Plex we are talking to it about TV Show Matching, so between your <advancedsettings> brackets, put the following:


<tvshowmatching>
</tvshowmatching>

We then need to tell Plex the expressions to try and match when it is looking for information. We start this out with:


<regexp></regexp>

So, now we should have the following:


<advancedsettings>

:<tvshowmatching>

::<regexp></regexp>

:</tvshowmatching>

</advancedsettings>

We can add as many <regexp> expressions as we want, and Plex will use them one by one until it finds one that matches. So if you are using more than one, it is best to put the structure you most often use at the top of these expressions, to minimise incorrect matches.

Regular Expression Strings

We now need to think about what we need to search on, and this is the real meat of the issue. It is best shown through an example:


:<regexp>Season[\._ ]([0-9]+)[\\/]Episode[\._ ]([0-9]+)[^\\/]*</regexp>  <!-- foo.103 -->

Seems like a lot! Lets work through it.

This expressions is pattern matching for the following file and folder structure:


Source
:The West Wing
::Season 1
:::Episode 1.m4v

As previously discussed, Plex figures out the name of the TV Show, so this expression is only concerned with /Season 1/Episode 1.m4v, so we need to figure out how to get information from this. The folder is called Season X, so we start with that:

Season[\._ ]([0-9]+)

Using the "Season" word, we are defining this is never changing. The section in square brackets is a character class. Plex will match any of the characters it contains. In this case we have a dot, underscore or space between "Season" and the next part. Where did the backslash go? The backslash has special meaning in a regular expression. It tells Plex to interpret the next character literally. So \. turns into a literal dot.

Then we have a number, which we define as ([0-9]+). The dash within the character class says that Plex should match any character from '0' through '9'. The plus sign is a repetition operator, it tells Plex that the preceding element should be matched 1 or more times. The parentheses say that this part of the string should be captured and returned to Plex. Plex goes from left to right, and the expressions it meets within parentheses it pulls out, leftmost first. It needs to pull 2 numbers, the first it will assume as the season number, the second the episode number.

Next, we have reached the end of the parent folder's name, so we are going into this folder. POSIX paths use a forward slash to indicate this. Windows paths use a backslash. We want to match either one so we're going to create a character class. Remember how we learned that the backslash has special meaning in a regular expression? We have to tell Plex to interpret it literally. As before we'll do this by preceding it with a backslash. So a literal backslash is written using two backslashes. So now the character class looks like this:

[\\/]

So that defines our folder separator, we are now in the Season X folder. What is in here? Well, we have an Episode X.m4v file, so lets define that.


Episode[\._ ]([0-9]+)[^\\/]*

Again, we have defined "Episode" as a constant, as I know that this is the naming convention for my library. Then we have a dot, space or underscore, so again we use [\._ ]. Then I know that we have the episode number, so I define it in the same way as before, ([0-9]+), sending the number back to Plex. At the end, we are ensuring that the section of the path we've been examining is part of the file name. We do that by checking that none of the characters that follow are folder separators. The ^ at the beginning of the character class indicates that Plex should take the complement of the class. In other words Plex will accept any character other than the fowardslash and backslash characters. The * is another repetition operator. It indicates that the previous element could repeat from zero to an infinite number of times.

So, now you have the tools to create your own expressions, just work through your file and folder structure.

Debugging

If you are finding that your expression is not finding the relevant information, then you may need to debug. In Plex, go an find a TV Show in your TV Show Source you are having issues with. Hit I on your keyboard, and hit refresh, which should get Plex to go and find the information from the internet again. You will need to hit Yes to any prompt asking if you want to get the info for those episodes. Open the log file at ~/Library/Logs/Plex.log after you have rescaned the TV Show, you need to look for something like this:


12:34:58 T:2693148576 M: 28639232   DEBUG: running expression season[\._ ]([0-9]+)[\\/]episode[\._ ]([0-9]+)[^\\/]* on label /volumes/media/tv shows/coupling/season 2/episode 1.m4v
12:34:58 T:2693148576 M: 28639232   DEBUG: found match /volumes/media/tv shows/coupling/season 2/episode 1.m4v 2 1

This tells us the string that was matched, and the regular expression that it matched to. The second line tells us the string which was matched, and you will see at the end of the line there are 2 numbers, the first is the season number, the second is the episode number. If you are missing either of these numbers and you are having problems with the season, this is the reason! Either that, or thetvdb.com doesn't have your tv show.

Hopefully that should cover all your bases!