media-manager/latest_yt_poster.py
2025-10-12 12:48:25 -05:00

93 lines
2.4 KiB
Python
Executable file

#!/usr/bin/env -S uv run --quiet --script
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "rich",
# "Pillow",
# ]
# ///
"""
Mostly done by gpt-5
"""
import os
import glob
import shutil
from rich import print
from PIL import Image
import random
def set_posters(base_dir: str) -> None:
"""
Set the latest thumbnail as the poster for each channel.
"""
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)}")
def grid_collage(base_dir: str) -> None:
## Grid collage
# base_dir = "/mnt/main/media/YouTube"
poster_path = os.path.join(base_dir, "poster.jpg")
# Collect recent thumbnails from subfolders
thumbs = []
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 = 5, 5
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}")
if __name__ == "__main__":
base_dirs = [
"/mnt/media/youtube/shared",
"/mnt/media/youtube/waylon",
]
for base_dir in base_dirs:
set_posters(base_dir)
grid_collage(base_dir)