Script for a random pre-roll video

server-freebsd

#1

I couldn't find any solutions for this in the forum, so I wrote a quick script to do this myself. If anyone has ideas for improvements let me know.
Here is the script and explanation/notes follow:
#!/bin/sh

# All echos are for debugging

# Get a count of the files in the pre-roll library minus default

NUM_FILES=ls /media/pre-roll | grep -v default.mkv |wc -l | sed -e 's/^[ ]*//'

#echo "NUM_FILES=$NUM_FILES"

# Now Generate a random number between 1 and NUM_FILES

# I'm getting the random number from www.random.org

RANDNUM=wget -qO- https://www.random.org/integers/?num=1\&min=1\&max="$NUM_FILES"\&col=1\&base=10\&format=plain\&rnd=new

#echo "RANDNUM=$RANDNUM"

# Go to the RANDNUM file and then copy it to default.mkv

FILE=ls /media/pre-roll | grep -v default.mkv | head -"$RANDNUM" | tail -1

#echo "FILE=$FILE"

cp /media/pre-roll/$FILE /media/pre-roll/default.mkv

My Libraries are located under /media for my install.
I created a pre-roll library.
I downloaded some pre-roll video into my library. Then I created a default.mkv file by copying an existing video to that. I had to split the video as Plex recognized that it was a duplicate. Then I copied the url for my default.mkv file and pasted that in the pre-roll location in extras.
The script is located in my root directory in the jail that my plex media server is running in for freenas.
I'm assuming that all my pre-roll video files are mkv. If the formats are different then the script will not work as the default.mkv file will obviously not be the right type.
The script is running as root.
Finally, I set up a cronjob to run the script once a day at 3am:
0 3 * * * /pre-roll_update.sh

Hope you find this useful and feedback is always appreciated!


#2

Quick update, noticed my script wasn't working, which really shocked me because my logic is always flawless. ;)
The path for wget wasn't loaded in non-interactive mode, so just change the line:
RANDNUM=wget -qO- https://www.random.org/integers/?num=1\&min=1\&max="$NUM_FILES"\&col=1\&base=10\&format=plain\&rnd=new
to
RANDNUM=/usr/local/bin/wget -qO- https://www.random.org/integers/?num=1\&min=1\&max="$NUM_FILES"\&col=1\&base=10\&format=plain\&rnd=new

Sorry about the mistake!


#3

How could this be adapted for windows?


#4

I second that. Is there a solution to play a random pre-roll video on windows??


#5

I found a way to do it with vbscript and a scheduled task.

The vbscript has the URLs of the plex pre-roll videos and updates and runs as a scheduled task every 15 minutes. Save the following code as something like RandomizeIntro.vbs and then the scheduled task I have is set to run "cscript RandomizeIntro.vbs" with RandomizeIntro.vbs as the parameter. The start in option should be the path to the script file.

Dim WshShell, intOption,preRoll
Dim LocalClass_StdRegProv,result
Const HKEY_CURRENT_USER = &H80000001

Set WshShell = Wscript.CreateObject("Wscript.Shell")
Randomize()
intOption = Int(3*Rnd())

Select Case intOption
  Case 0
    preRoll = "http://127.0.0.1:32400/web/index.html#!/server/82ca81b724c99deb007abb3782a5e59f9ad072d4/details/%2Flibrary%2Fmetadata%2F2692"
  Case 1
    preRoll = "http://127.0.0.1:32400/web/index.html#!/server/82ca81b724c99deb007abb3782a5e59f9ad072d4/details/%2Flibrary%2Fmetadata%2F3046"
  Case 2
    preRoll = "http://127.0.0.1:32400/web/index.html#!/server/82ca81b724c99deb007abb3782a5e59f9ad072d4/details/%2Flibrary%2Fmetadata%2F3047"
End Select



Set LocalClass_StdRegProv = GetObject("winmgmts:{impersonationlevel=impersonate}!\\.\root\default:StdRegProv")
result = LocalClass_StdRegProv.SetStringValue(HKEY_CURRENT_USER, "SOFTWARE\Plex, Inc.\Plex Media Server", "CinemaTrailersPrerollID", preRoll)

WScript.Quit ' Tells the script to stop and exit.

#6

Good work @MarsenFamily !
What do you have to do/change to add even more pre-rolls to the mix?


#7

Hi @MarsenFamily
This is amazing. Could you do a step-by-step guide for me. I've been wanting this for ages. You'd be a lifesaver!
Thanks!!! :)


#8

The Int(3*Rnd()) part of the script is the part that picks a random pre-roll. Change the 3 to the number of pre-rolls in the script, counting 0 as one of the pre-rolls. So my example had 0 through 2 for a total of 3.
Then the Select Case portion can be as many pre-rolls as you need. Just use the next number for each one. I have already added 2 more to mine since I got it working so mine is now a random video of 5 in the script.

Each preRoll = "http........." part of the Case statement needs to be a URL of a pre-roll in your plex system. These are the same values that you would put in the settings of Plex Settings>Server>Extras but this way it will change that value periodically (via the scheduled task) to a random pre-roll video.


#9

Thanks @MarsenFamily . I understand that. But how do I lodge that into Task Scheduler to use it?


#10

Cheers @MarsenFamily
Just tested the script and it works exactly as I wanted it too.

Good work, thanks for sharing.


#11

I am working on modifying this script to work on Unraid 6.3.2 using a PLEX Docker as well. Seems like this should work but I still need to test it all out overnight. I am not very proficient with Linux but I am fairly proficient vbscript and powershell. It would be great if someone could just make this an app or a plugin for Unraid that does this!

I created a folder called "plex.custom.scripts" in the plugins folder:
\\servername\flash\config\plugins\plex.custom.scripts

I then create a text file in that folder called "plex.random.pre-roll.cron":
\\servername\flash\config\plugins\plex.custom.scripts\plex.random.pre-roll.cron

Placed this text in the cron file. This file will automatically load when the unraid server boots and it will run the script at 3 AM daily as indicated above:
# Plex Random Pre-Roll Video:
0 3 * * * /mnt/user/appdata/scripts/random-pre-roll.sh

I already have a cache only share called appdata and on there a scripts folder where I store other scripts. So using NotepadX I created a Unix file called \servername\appdata\scripts\random-pre-roll.sh and here is that code which is slightly different than above:
#!/bin/bash
# All echos are for debugging
# Get a count of the files in the Pre-Roll library minus default
NUM_FILES=$(ls /mnt/user/Media/Pre-Roll/ | grep -v default.mkv | wc -l | sed -e 's/^[ ]*//')
#echo "NUM_FILES=$NUM_FILES"
# Now Generate a random number between 1 and NUM_FILES
# I'm getting the random number from www.random.org
RANDNUM=$(wget -qO- https://www.random.org/integers/?num=1\&min=1\&max="$NUM_FILES"\&col=1\&base=10\&format=plain\&rnd=new)
#echo "RANDNUM=$RANDNUM"
# Go to the RANDNUM file and then copy it to default.mkv
FILE=$(ls /mnt/user/Media/Pre-Roll/ | grep -v default.mkv | head -"$RANDNUM" | tail -1)
#echo "FILE=$FILE"
cp /mnt/user/Media/Pre-Roll/$FILE /mnt/user/Media/Pre-Roll/default.mkv

I then had to make the script executable by running this command:
chmod +x random-pre-roll.sh

I removed the remarks on the echos and manually ran the script and it worked. Here is the output.
root@servername:/mnt/user/appdata/scripts# random-pre-roll.sh
NUM_FILES=22
RANDNUM=8
FILE=hd_dolby_countdown_v1_lossless.mkv

I rebooted my server and verified the cron job loaded ok. Run this command to check if the cron job loaded.
cat /etc/cron.d/root

Now to see if it works over the next few days.


#12

I added one more line of code at the end of the script for my unraid server to notify me when this changes and what file it changes to for the next few days.

/usr/local/emhttp/webGui/scripts/notify -i normal -s "Pre-Roll Changed" -d " Pre-Roll Video was changed ondateto $FILE"

Mine is setup to email me notifications and the email looks like this:

Event: unRAID Status
Subject: Pre-Roll Changed
Description: Pre-Roll Video was changed on Sun Mar 26 13:34:15 EDT 2017 to hd_dolby_atmos_amaze_lossless-DWEU.mkv
Importance: normal


#13

I know this is old, but i just wanted to thank @MarsenFamily for the base code. I essentially used it and just pointed to the local files on my Plex sever:

    Dim WshShell, intOption,preRoll
    Dim LocalClass_StdRegProv,result
    Const HKEY_CURRENT_USER = &H80000001

    Set WshShell = Wscript.CreateObject("Wscript.Shell")
    Randomize()
    intOption = Int(3*Rnd())

    Select Case intOption
      Case 0
        preRoll = "C:\PlexPreRoll\Cineplex.mp4"
      Case 1
        preRoll = "C:\PlexPreRoll\DD-Amaze.mp4"
      Case 2
        preRoll = "C:\PlexPreRoll\DD-Countdown1.mp4"
    End Select

    Set LocalClass_StdRegProv = GetObject("winmgmts:{impersonationlevel=impersonate}!\\.\root\default:StdRegProv")
    result = LocalClass_StdRegProv.SetStringValue(HKEY_CURRENT_USER, "SOFTWARE\Plex, Inc.\Plex Media Server", "CinemaTrailersPrerollID", preRoll)

    WScript.Quit ' Tells the script to stop and exit.

Going to add the rest of my files to the list.

Tested and working!! This is an awesome little add-on.

Thanks again and thanks to all the other posters and their comments.