docent/bin/build-images

77 lines
1.8 KiB
Text
Raw Permalink Normal View History

2026-05-26 11:52:32 -05:00
#!/usr/bin/env bash
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
MANIFEST="${MANIFEST:-$REPO_ROOT/content/audio.yaml}"
SRC_DIR="${SRC_DIR:-$REPO_ROOT/content/images}"
OUT_DIR="${OUT_DIR:-$REPO_ROOT/web/static/images}"
MAX_SIZE="${MAX_SIZE:-1200}"
QUALITY="${QUALITY:-80}"
for tool in yq magick; do
if ! command -v "$tool" >/dev/null; then
echo "error: $tool not in PATH (try: nix develop)" >&2
exit 1
fi
done
mkdir -p "$OUT_DIR"
encoded=0
skipped=0
missing=0
failed=0
NUM_ARTISTS=$(yq '.artists | length' "$MANIFEST")
for ((a=0; a<NUM_ARTISTS; a++)); do
ARTIST_SLUG=$(yq -r ".artists[$a].slug" "$MANIFEST")
NUM_IMAGES=$(yq ".artists[$a].images | length // 0" "$MANIFEST")
for ((i=0; i<NUM_IMAGES; i++)); do
SRC_NAME=$(yq -r ".artists[$a].images[$i]" "$MANIFEST")
SRC_PATH="$SRC_DIR/$SRC_NAME"
OUT_NAME="${ARTIST_SLUG}-$((i + 1)).webp"
OUT_PATH="$OUT_DIR/$OUT_NAME"
if [[ ! -f "$SRC_PATH" ]]; then
printf ' %-45s SKIP (source not found)\n' "$OUT_NAME"
missing=$((missing + 1))
continue
fi
if [[ -f "$OUT_PATH" ]]; then
src_mt=$(stat -c %Y "$SRC_PATH")
out_mt=$(stat -c %Y "$OUT_PATH")
if (( out_mt > src_mt )); then
printf ' %-45s up to date\n' "$OUT_NAME"
skipped=$((skipped + 1))
continue
fi
fi
printf ' %-45s converting ...\n' "$OUT_NAME"
if magick "$SRC_PATH" \
-auto-orient \
-strip \
-resize "${MAX_SIZE}x${MAX_SIZE}>" \
-quality "$QUALITY" \
"$OUT_PATH"; then
size=$(du -h "$OUT_PATH" | cut -f1)
printf ' %-45s done (%s)\n' "$OUT_NAME" "$size"
encoded=$((encoded + 1))
else
printf ' %-45s FAILED\n' "$OUT_NAME" >&2
failed=$((failed + 1))
fi
done
done
printf '\n%d converted · %d up-to-date · %d missing sources · %d failed\n' \
"$encoded" "$skipped" "$missing" "$failed"
if (( failed > 0 )); then
exit 1
fi