#!/usr/bin/env python3 """Emit web-only WebP/MP3 copies. Original game assets are never changed.""" import argparse import json import subprocess from concurrent.futures import ThreadPoolExecutor from pathlib import Path from PIL import Image parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('build', type=Path, nargs='?', default=Path('build/web')) args = parser.parse_args() assets = args.build / 'assets/assets' sources = sorted([*assets.rglob('*.png'), *assets.rglob('*.wav')]) before = sum(p.stat().st_size for p in sources) def convert(path): if path.suffix == '.png': target = path.with_suffix('.webp') with Image.open(path) as image: # Keep full-size notebook textures; illustrations need fewer pixels. max_size = (1536, 1536) if 'page' in path.name else (1024, 1024) image.thumbnail(max_size, Image.Resampling.LANCZOS) image.save(target, 'WEBP', quality=86, method=6) else: target = path.with_suffix('.mp3') subprocess.run([ 'ffmpeg', '-nostdin', '-v', 'error', '-y', '-i', str(path), '-codec:a', 'libmp3lame', '-b:a', '64k', str(target), ], check=True) size = target.stat().st_size path.unlink() return size with ThreadPoolExecutor(max_workers=4) as pool: after = sum(pool.map(convert, sources)) print(f'Web media: {before / 1024**2:.1f} → {after / 1024**2:.1f} MiB ' f'({100 * (1 - after / before):.0f}% smaller; {len(sources)} files).')