Last active
July 10, 2023 02:04
-
-
Save lunks/afaa383432e61da8e9bd85b1ab10f701 to your computer and use it in GitHub Desktop.
Plex Pre-roll randomizer: Grabs 10 prerolls from a directory full of videos, being 6 < 30s, 2 30-40s, 2 of the rest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment