Files
logic-puzzle/scripts/optimize_web_media.py
Hadrian Burkhardt 7e6b203a02 Add browser playtest and improve library, media and input
Bundle web bootstrap, icons, local font fallbacks and hosting instructions.
Add optimized WebP/MP3 packaging, asset checks and a browser narration
regression tool. Support browser audio handoff and ad-free explained hints.

Refresh the responsive bookshelf and reduce unnecessary app, narration and
paper repaint work. Preserve deliberate page swipes while tolerating small
mouse movements during cell and navigation clicks.

Validation: all 77 Flutter tests pass; shell, JavaScript and Python scripts
parse; locale and web manifests are valid JSON; git diff --check passes.
2026-11-06 03:33:26 +01:00

42 lines
1.5 KiB
Python

#!/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).')