Skip to content

Instantly share code, notes, and snippets.

@lunks
Created March 18, 2024 00:56
Show Gist options
  • Select an option

  • Save lunks/cac8eceae2f5b2641c5c5f5008adf12c to your computer and use it in GitHub Desktop.

Select an option

Save lunks/cac8eceae2f5b2641c5c5f5008adf12c to your computer and use it in GitHub Desktop.

Revisions

  1. lunks created this gist Mar 18, 2024.
    45 changes: 45 additions & 0 deletions setup_prerolls.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    #!/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()