54 lines
1.4 KiB
Bash
Executable File
54 lines
1.4 KiB
Bash
Executable File
#!/usr/bin/env bash
|
||
# Create JPG versions of all WEBP images in a folder,
|
||
# resizing them to roughly 2–3 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 ~2–3 MP
|
||
QUALITY="${QUALITY:-3}" # ffmpeg JPEG quality (2–4 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"
|
||
|