media-manager/latest_yt_poster.py
2025-10-11 19:13:21 -05:00

70 lines
2.1 KiB
Python
Executable file

#!/usr/bin/env -S uv run --quiet --script
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "rich",
# "Pillow",
# ]
# ///
import os, glob, shutil
from rich import print
base_dir = "/mnt/media/youtube"
for channel_dir in os.listdir(base_dir):
full_path = os.path.join(base_dir, channel_dir)
if not os.path.isdir(full_path):
continue
# find all video thumbnails (jpg/png)
thumbs = sorted(
glob.glob(os.path.join(full_path, "**/*.jpg")) +
glob.glob(os.path.join(full_path, "**/*.png")),
key=os.path.getmtime,
reverse=True
)
if not thumbs:
print(f"No thumbnails found for {channel_dir}")
continue
latest_thumb = thumbs[0]
poster_path = os.path.join(full_path, "poster.jpg")
if not os.path.exists(poster_path) or os.path.getmtime(latest_thumb) > os.path.getmtime(poster_path):
shutil.copy2(latest_thumb, poster_path)
print(f"Set poster for {channel_dir}{os.path.basename(latest_thumb)}")
## Grid collage
from PIL import Image
import glob, os, random
# base_dir = "/mnt/main/media/YouTube"
poster_path = os.path.join(base_dir, "poster.jpg")
# Collect recent thumbnails from subfolders
thumbs = []
# for root, dirs, files in os.walk(base_dir):
# for f in files:
# if f.lower().endswith((".jpg", ".png")) and "poster" not in f:
# thumbs.append(os.path.join(root, f))
# thumbs = sorted(thumbs, key=os.path.getmtime, reverse=True)[:50]
thumbs = sorted(glob.glob(os.path.join(base_dir, "**/*.jpg")) + glob.glob(os.path.join(base_dir, "**/*.png")), key=os.path.getmtime, reverse=True)[:50]
random.shuffle(thumbs)
if not thumbs:
raise SystemExit("No thumbnails found")
# Make a 5x5 grid collage (adjust as desired)
rows, cols = 2, 2
tile_size = 256
grid = Image.new("RGB", (cols*tile_size, rows*tile_size))
for i, thumb in enumerate(thumbs[:rows*cols]):
img = Image.open(thumb).convert("RGB").resize((tile_size, tile_size))
r, c = divmod(i, cols)
grid.paste(img, (c*tile_size, r*tile_size))
grid.save(poster_path, quality=85)
print(f"Created {poster_path}")