READING TIME: 5 MINUTES
Have you ever wanted to randomize your Plex movie pre-rolls? How about schedule Thanksgiving prerolls in November, but Christmas prerolls in December? What about change prerolls every single day?
With this awesome Plex-Automated-Preroll script, you can!
The only things you need to get started are:
- Plex server
- Python 3.7
- A few required Python packages
For this guide, I have my Plex server configured on Windows Server 2019, but the steps in this guide will work for Windows 10 users as well. This guide will walk you through how to set this up step-by-step, so don’t worry if you don’t have any Python experience.
UPDATE: New version with GUI has been created! Tutorial here: https://smarthomepursuits.com/how-to-setup-plex-automatic-pre-roll-gui-in-windows/
Step 1: Download (or upgrade) Python
Before you begin, you’ll need to make sure you have at least Python 3.7 installed. Note: if you are using Tautulli (as of December 2020), you are probably on 2.7.1. and will need to latest version of Python.
If you aren’t sure which version of Python you have, open Command Prompt (type CMD into Start) and type
If you’re on an older version, download Python 3.8 from here: https://www.python.org/downloads/release/python-380/
Click the Windows x86-x64 executable installer.
Once downloaded, double-click the .exe to install it. Make sure to select both checkboxes below:
Step 2: Download Plex Automated Preroll Script
Download the Github script from here: https://github.com/TheHumanRobot/Plex-Automatic-Preroll/tree/develop
Next, click the green “Code” button > Download zip.
Extract the files to a temporary folder (Downloads is fine).
Step 3: Install Required Packages
One of the extracted files is REQUIREMENTS.txt. That is the file that shows all the required packages you need to install. To install them, open CMD and change the directory to the Python directory. The default directory is:
Change to that directory by opening Command Prompt and typing:
Install each package with the
- pip install MarkupSafe==1.1.1
- pip install PlexAPI==4.2.0
- pip install python-dateutil==2.8.1
- pip install PyYAML==5.3.1
- pip install bs4
- pip install lxml
- pip install requests>=2.4.2
- pip install ruamel.yaml
- pip inistall urllib3==1.26.2
Step 4: Copy the Plex_Trailers.py and config.yml file to Python Directory
In the extracted folder, copy & paste the Plex_Trailers.py and config.yml files to the Python directory:
Step 5: Create Monthly Preroll Folders
If you don’t have monthly paths setup yet, I recommend creating them like mine.
For this example, I just dropped 1 preroll .mp4 file into the November folder and 1 into the December folder.
Step 6: Edit Config.yml file
If you want to just use Monthly trailers, simply edit the config file you just copied over. Right-click Config > Edit.
Change the URL, Token, and Monthly paths like my screenshot below:
To find your Plex token, open the Plex UI. Find a Movie> Get Info > View XML. In the URL, scroll to the far right and copy the Plex-Token= XYZ field
Next, edit the URL, add your token, and edit the paths to your preroll videos. Save the config file once complete.
Step 7: Run Plex_Trailers.py
From the Scripts directory, run the Plex_Trailers.py file. If you need to the path again, it is:
Step 8: Test Your Setup
Start a movie from the Plex app on you phone. (I wasn’t able to get prerolls to load from Plex browser UI)
If all goes well, it should play the preroll in the corresponding directory. Since it’s December, it should play the HDR1.mp4 preroll located in C:\Prerolls\December\.
Now, let’s change your Date and Time on Windows back to November to see if it’ll work on a different month. Open Control Panel > Clock and Region. Set the Date and Time > Change date and time. Pick a day in November.
Click OK to save. Then, re-run the Plex_Trailers.py file.
Launch the movie again. This time, it should play my fire.mp4 file located in C:\Prerolls\November folder.
Step 10: Create Scheduled Task to Update Monthly
If you’ve made it this far, awesome! Everything is setup and running as expected. The next step to run the Plex_Trailers.py script monthly using a scheduled task.
Open Task Scheduler. Right-click Task Scheduler Library > Create Basic Task. Create your task like mine below:
In the Program/script box, enter this path (change the USERNAME of course):
Then click Finish.
That’s it! You can now play Plex pre-rolls randomly each month without needing to go into Plex and editing the path. On the first day of each month, the Schedule Task will run, changing the directory the prerolls will be running from.
If you’d like to randomly play prerolls from the same Monthly folder, simply add a semicolon between the paths in the config like this and run the Plex_Trailers.py again for the change to take effect.
Hi there! Thanks for the great tutorial! What settings need to be made or changed within Plex? In Plex, there is a field for prerolls (settings/extras/prerolls) – should that be left blank?
You’re welcome! No need to change anything within Plex if you’re using this script to automatically change Plex prerolls.
So, first off, THANK YOU for making such a simple to follow guide! Secondly, I want to know, can we mix commas and semicolons? like, can we have a pre roll for silence and one for Social distancing ALWAYS PLAY, and a random Feature Presentation chosen?
I’m also searching for an answer to this question. Would be great if this was possible.
I know you can in plex itself, so it would make sense you would be able to on here/
Thank you for the great instructions. Just wondering if you would be able to make this for a docker. I have just moved everything over to my Unraid server and looking for a way to automate this with in the docker container for Plex.
I use Docker for a lot of things just not Plex since I use my GPU for hardware transcoding, so I probably won’t be a creating a guide on how to do that. Good luck though, I’m sure there’s a way to do it if you Google a little more!