Skip to content

Instantly share code, notes, and snippets.

@lunks
Created March 18, 2024 00:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lunks/cac8eceae2f5b2641c5c5f5008adf12c to your computer and use it in GitHub Desktop.
Save lunks/cac8eceae2f5b2641c5c5f5008adf12c to your computer and use it in GitHub Desktop.
Plex Prerolls Randomizer
#!/usr/bin/python3
import os
import random
import subprocess
import shutil
SRC_DIR = os.getenv('SRC_DIR', '/mnt/user/appdata/plex/source_prerolls')
DEST_DIR = os.getenv('DEST_DIR', '/mnt/user/appdata/plex/prerolls')
def get_duration(filename):
cmd = [
'docker', 'run', '--rm', '-v', f'{SRC_DIR}:/videos', '--entrypoint', 'ffprobe',
'linuxserver/ffmpeg', '-v', 'error', '-show_entries', 'format=duration',
'-of', 'default=noprint_wrappers=1:nokey=1', f'/videos/{filename}'
]
result = subprocess.run(cmd, capture_output=True, text=True)
return float(result.stdout.strip())
def get_files():
categorized_files = { 'short': [], 'medium': [], 'long': [] }
for file in os.listdir(SRC_DIR):
if file.endswith('.mp4'):
duration = get_duration(file)
if duration <= 30:
categorized_files['short'].append(file)
elif 30 < duration <= 40:
categorized_files['medium'].append(file)
else:
categorized_files['long'].append(file)
return categorized_files
def copy_files():
categorized_files = get_files()
selected_files = []
for cat, count in [('short', 6), ('medium', 2), ('long', 2)]:
selected_files += random.sample(categorized_files[cat], count)
for i, file in enumerate(selected_files, 1):
shutil.copy(os.path.join(SRC_DIR, file), os.path.join(DEST_DIR, f'video{i}.mp4'))
if __name__ == "__main__":
copy_files()
@lunks
Copy link
Author

lunks commented Mar 18, 2024

I've always been fond of those random MTV ads and have wanted to add some prerolls on Plex for a while, but I've always found that most of the prerolls online could be more varied, shorter, etc.

So, I had the idea to do MTV-style prerolls. The accompanying script will help you do that, too.

Here's how it works:

You have a folder (SRC_DIR) with random short videos that you think are funny. I strongly recommend downloading all the videos from @psychotronica_ on Twitter, as they are perfect for this.

Weekly (or at the interval you set the script to run), it will pick 10 videos in the SRC_DIR: 6 with 30 seconds or less, 2 with 40 seconds or less and 2 longer than 40 seconds. I'm trying to prevent long videos from being shown to users all the time, but you may be "lucky" to get one of these long ones.

It will output to the DEST_DIR you set in the script. Then all you have to do is add the list of prerolls with the names the script creates, in my case:

/config/prerolls/video1.mp4;/config/prerolls/video2.mp4;/config/prerolls/video3.mp4;/config/prerolls/video4.mp4;/config/prerolls/video5.mp4;/config/prerolls/video6.mp4;/config/prerolls/video7.mp4;/config/prerolls/video8.mp4;/config/prerolls/video9.mp4;/config/prerolls/video10.mp4;

Semicolons make Plex pick one of the prerolls at random.

That's it! I hope someone finds it useful. Occasionally, one of my friends says there is some weird video playing before watching a movie. I pretend I'm unaware of what they are talking about and ask them to describe what they see. It's a very entertaining experience that, unfortunately, doesn't last long as I can't keep my poker face up for too long.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment