Plex dvr is a welcomed addition to this great media server. However, before you start recording your favorite show, you should be aware that recordings can take a lot of space on your server. Plex saves the files as Transport Stream with the extension “ts”. On average one hour show will take about 6 GB of space. If you plan to record more shows and you would like to watch them at later time, you will eventually run into problem of not having enough of space. One option is to purchase larger hard drives. Other option is to compress recordings to smaller formats. Plex does not have this feature yet, however it is capable of compressing files into mp4. It is called optimized version. Once you decide to create optimized version Plex will create special folder within TV Series Season folder where compressed files are stored. This will allow you to stream optimized version to your phone or any other device. This is not an archiving feature. Once you delete original recording, the optimized version is also deleted from the the hard drive. This is where python script comes in. It will move and replace original ts recording with new optimized mp4 version. Before we can run the script we have to prepare computer or server to run the script. The script was created in mind that it will be run on Linux unRaid box. However, since it is python script it should be able to run on any PC that has Python installed.
Select TV Series and from the right top corner pick Optimize
Specify optimization settings. Pick “In folders with original items” for Version storage location
For PC running Windows install Python 2.7 from here
For unRaid install plugin Nerd Tools from Apps tab
Go to Plugin tab and find Nerd Tools icon
Click in the icon and in search field type in “python”. Select version 2.7 to install
To confirm installation open terminal and type in “python –version”. You should see this information. If not, reboot your server.
We are not done yet. This script requires installation of Pandas Python library. In order to do that, we have to enable our Python to be able to download necessary libraries. It is done through Python PIP which is a package management system. Search internet for python script called “get-pip.py”. For convenience I included this script in zip file.
Unpack downloaded file and navigate to this folder in terminal. Type in the name of the script.
Now you can install pandas library too. Type “pip install pandas”
Important. Since pandas are not part of the standard python installation, you will have to repeat pip and pandas installation every time your reboot your unRaid server.
To make changes to the script, you will have to install editing tool. I highly recommend installing Notepad++.
Python is very strict about code line indentation. It is possible that due to incorrect number of spaces or mixing spaces and tabs, entire script will refuse to cooperate. Therefore, be extremely careful when making any changes. Notepad++ is great writing new code, but you should also confirm that the script structure adheres to required standard. If you installed Python 2.7 on your PC too, you have also access to its own tool called IDLE (Python GUI)
First select the area affected by changes. Next, go to Format and select Untabify Region
Type in number 4 and press OK button
To confirm that edited script will run without errors got to Run and select Check Module
Once you see this window, everything should run correctly
Important. You should run this check procedure every time you make some changes to script modules.
Before running script we need to specify the folder that holds our recordings. You can add multiple folders. Open “main.py” module
By default only preview is enabled
In order to actually allow script to move files you will have to uncomment two lines of code. I strongly recommend that you run script in preview mode first.
To make some additional changes open settings.py file
As I mentioned before, Plex dvr records files in ts format, but you can also add other extensions too. Hours to subtruct will prevent processing files that are being currently converted. You can also set minimum and maximum file size
Script in Action
To run the script type “main.py”
Script does a really good job with tv series that have one episode per day. If it’s more than 2 episodes of the same show it’s becoming much more difficult to match correct converted files to their original versions. This is due to naming convention used by Plex. I would say that right now this outcome is still unpredictable.
For now recorded Movies have to placed in their own folders before they are being processed by this script. I’m working on module that will take care of it.