# Automatically renaming tvshows for processing by plex media server



## kenney (Feb 6, 2011)

Good Afternoon All,

I am using kmttg to automatically pull all recorded shows off of a TiVo Premier, and wanted to have a way to automatically rename the files for tv shows with something similiar to "tv show name - S01E01 - episode title.mpg" so that they could be brought into Plex media server for distribution through my house.

The biggest problem that I had was determining what the show actually was to rename it. I found a program called tvdb-renamer which does this by attempting to match the name to thetvdb.com's databases. This didnt completely work for me so I decided to write a program that would do if for me, and thought I would share it in case anyone is interested in doing the same.

I use kmttg and plex media server on ubuntu, and have only tried my script on linux and mac. The only requirement is to install the levenshtien distance code from google (I used this to determine the "distance" between two strings as a way to best guess the correct title)

The script takes into account show name, air date, and episode title. It will output a filename similiar to tvdb-renamer unless told otherwise and uses the same basic inputs.

I use it with a command similiar to:

./kmttg-renamer.py "kmttg showname - kmttg tittle (MM_DD_YY).mpg" /nas/TiVo -n

so that it will rename the file, and move it to a directory of /nas/TiVo/showname/showname - SYYE## - title.mpg"

Below are the options.

Usage: ./kmttg-renamer.py [options] KMTTG_FILE.EXT OUTPUT_DIRECTORY(optional)
Additional options: 
-i this will place program in interactive mode
-d do NOT create directories
-t test only, do not run commands
-l value change default Levenshtein value (default=0.5)
-X value change default Levenshtein value if dates match (default=0.1)
-tvdb run against a tvdb-renamer format named file
-f do NOT include original file name in output
-n save in new format "Show - S01E01 - FileInfo.ext" vice tvdb format
-o ss,## override season name (ex -o key+and+peele,255325)
-p pause on fail, good for testing with scripts
-s show all in a series to pick from, if nothing found
-h show these help options

KMTTG_FILE.EXT must be in the format of either:
Phineas and Ferb - What a Croc (06_13_2012).mpg [kmttg format]
Phineas and Ferb.S03E48.What a Croc (06_13_2012).mpg [tvdb-renamer]
* Note you must used -tvdb flag for this format!

Again, just thought I would throw this out there in case anyone is interested.


----------



## jcthorne (Jan 28, 2002)

Videoredo Auto Processor will do the lookup and rename for you. Along with a number of other tasks.


----------



## dlfl (Jul 6, 2006)

jcthorne said:


> Videoredo Auto Processor will do the lookup and rename for you. Along with a number of other tasks.


On windows only. I would have responded earlier but he is using linux and mac it appears.


----------



## kenney (Feb 6, 2011)

In case anyone else uses kmttg under linux. I have slowly updated my scripts to rename the files, this is the current version. I use this to take the output files and rename them and move them into my plex share.


```
KMTTG File Renamer (Version 2.3)
 Invalid Input File
 Usage: /bin/renamer INPUT_FILE1 INPUT_FILE2 ... <options>
 Options:    -dir:dir      Directory to move file to (default=current)
             -log:file     File to log information to
             -db:dir       Where to save database files
             -odb:file     File to store show override values
          -s -show         Do not create show subdirectory undir destination dir
          -q -quiet        Show no information
          -p -parse        print out parsable information
          -i -interactive  If more than one possible episode, user will choose
          -t -test         Test Mode - Make no changes
          -d -debug        Show debuging information
 Example: renamer.py -db:/kmttg/renamer.sav -log:/kmttg/renamer.sav/renamer.log -dir:/share/TiVo -odb:/kmttg/renamer.odb /kmttg/*.mkv -debug -p
```


----------



## jimfaria (Dec 9, 2013)

Any updates to this script? It works most of the time but occasionally doesn't work.. noreason given.. I've tried using the debug option but I don't get any indication of why it fails to work.


----------



## kenney (Feb 6, 2011)

Attached is the updated version, I haven't uploaded here in a while, sorry about that. This version has been very stable for me.


```
KMTTG File Renamer (Version 2.51)
 Usage: /usr/bin/renamer INPUT_FILE1 INPUT_FILE2 ... <options>
 Options:    -dir:dir      Directory to move file to (default=current)
             -log:file     File to log information to
             -db:dir       Where to save database files
             -odb:file     File to store show override values
             -csv:file     Create a results file capable of opening in excel
             -val:###      Minimum Match Value (default=60)
             -lev:###      Minimum Levenshtein distance (default=0.4999)
             -tvdb:file    TVDB Show database (a .sav file)
             -tvid:###     TVDB Show ID (the tvdb show number)
             -time:###     Minimum time length to be valid (default=400)
             -select:input Advanced - used to script renamer for other purposes
             -script:file  Advanced - create a database file of the matches (used with -select)
             -failepi      Advanced - do not extract episode name use filename
             -simple       Advanced - verify selection only, not all choices
             -dupe         Advanced - if file already exists copy anyway and lable as duplicate
          -s -show         Do not create show subdirectory under destination dir
          -q -quiet        Show no information
          -p -parse        print out parsable information
          -i -interactive  If more than one possible episode, user will choose
          -t -test         Test Mode - Make no changes
          -d -debug        Show debuging information
          -e -extend       Show extended debuging (show each episodes value)
          -n -noreload     Do not requery thetvdb (use existing data)
 Example: /usr/bin/renamer -db:/kmttg/renamer.sav -log:/kmttg/renamer.log -dir:/share/TiVo -odb:/kmttg/renamer.odb /kmttg/*.mkv -debug -p
```
Also, within the script is a option to allow it to take the season and episode number from the TiVo created metadata vice the filename for a specific series (as example a lot of BBC shows just come up as the show name like primeval as the filename).


```
OVERRIDE_SEASON_EPISODE = {'mi-5': '78890', 'spooks': '78890', 'primeval':'79809'}
```


----------



## jimfaria (Dec 9, 2013)

Thanks.


----------



## kenney (Feb 6, 2011)

Updated version attached. This has been very stable so I have not updated it for months. Version 2.58.


----------

