Files
server-configs/httpserver/data/mew-neocities/library/webp-to-jpg.sh
2026-03-22 00:54:28 -07:00

54 lines
1.4 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# Create JPG versions of all WEBP images in a folder,
# resizing them to roughly 23 megapixels while preserving aspect ratio.
# Uses ffmpeg.
#
# Usage (from any directory):
# OUT_DIR="." ./webp-to-jpg.sh /path/to/webp/folder
#
# Defaults:
# SRC_DIR = first argument (or ".")
# OUT_DIR = $OUT_DIR (or ".")
# MAX_DIM = longest side in pixels (default 2000)
set -euo pipefail
SRC_DIR="${1:-.}"
OUT_DIR="${OUT_DIR:-.}"
MAX_DIM="${MAX_DIM:-2000}" # longest side; typical outputs end up ~23 MP
QUALITY="${QUALITY:-3}" # ffmpeg JPEG quality (24 is usually fine)
if ! command -v ffmpeg &>/dev/null; then
echo "Error: ffmpeg not found. Install it first." >&2
exit 1
fi
shopt -s nullglob nocaseglob
files=("$SRC_DIR"/*.webp)
shopt -u nullglob nocaseglob
if [[ ${#files[@]} -eq 0 ]]; then
echo "No WEBP files found in: $SRC_DIR"
exit 0
fi
mkdir -p "$OUT_DIR"
for in_path in "${files[@]}"; do
base="$(basename "$in_path")"
name="${base%.*}"
out_path="${OUT_DIR}/${name}.jpg"
echo "Converting $in_path -> $out_path"
# Scale so that the longest side is MAX_DIM, preserving aspect ratio.
# This may upscale small images slightly, which is acceptable here.
ffmpeg -y -i "$in_path" \
-vf "scale='if(gt(iw,ih),${MAX_DIM},-2)':'if(gte(ih,iw),${MAX_DIM},-2)'" \
-q:v "$QUALITY" \
"$out_path"
done
echo "Done creating JPG versions in: $OUT_DIR"