update scripts

This commit is contained in:
Waylon Walker 2025-10-12 12:48:25 -05:00
parent b1a74f24ae
commit e57ffee038

View file

@ -6,65 +6,88 @@
# "Pillow", # "Pillow",
# ] # ]
# /// # ///
import os, glob, shutil """
Mostly done by gpt-5
"""
import os
import glob
import shutil
from rich import print from rich import print
from PIL import Image
import random
base_dir = "/mnt/media/youtube" 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
for channel_dir in os.listdir(base_dir): # find all video thumbnails (jpg/png)
full_path = os.path.join(base_dir, channel_dir) thumbs = sorted(
if not os.path.isdir(full_path): glob.glob(os.path.join(full_path, "**/*.jpg"))
continue + glob.glob(os.path.join(full_path, "**/*.png")),
key=os.path.getmtime,
reverse=True,
)
# find all video thumbnails (jpg/png) 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( thumbs = sorted(
glob.glob(os.path.join(full_path, "**/*.jpg")) + glob.glob(os.path.join(base_dir, "**/*.jpg"))
glob.glob(os.path.join(full_path, "**/*.png")), + glob.glob(os.path.join(base_dir, "**/*.png")),
key=os.path.getmtime, key=os.path.getmtime,
reverse=True reverse=True,
) )[:50]
random.shuffle(thumbs)
if not thumbs: if not thumbs:
print(f"No thumbnails found for {channel_dir}") raise SystemExit("No thumbnails found")
continue
latest_thumb = thumbs[0] # Make a 5x5 grid collage (adjust as desired)
poster_path = os.path.join(full_path, "poster.jpg") rows, cols = 5, 5
tile_size = 256
grid = Image.new("RGB", (cols * tile_size, rows * tile_size))
if not os.path.exists(poster_path) or os.path.getmtime(latest_thumb) > os.path.getmtime(poster_path): for i, thumb in enumerate(thumbs[: rows * cols]):
shutil.copy2(latest_thumb, poster_path) img = Image.open(thumb).convert("RGB").resize((tile_size, tile_size))
print(f"Set poster for {channel_dir}{os.path.basename(latest_thumb)}") r, c = divmod(i, cols)
grid.paste(img, (c * tile_size, r * tile_size))
## Grid collage grid.save(poster_path, quality=85)
from PIL import Image print(f"Created {poster_path}")
import glob, os, random
# base_dir = "/mnt/main/media/YouTube"
poster_path = os.path.join(base_dir, "poster.jpg")
# Collect recent thumbnails from subfolders if __name__ == "__main__":
thumbs = [] base_dirs = [
# for root, dirs, files in os.walk(base_dir): "/mnt/media/youtube/shared",
# for f in files: "/mnt/media/youtube/waylon",
# 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: for base_dir in base_dirs:
raise SystemExit("No thumbnails found") set_posters(base_dir)
grid_collage(base_dir)
# 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}")