Files
pivoine.art/scripts/compress-videos.sh
T
valknar 2b3cf8bada fix: redirect ffmpeg stdin from /dev/null in compress-videos.sh
FFmpeg inside a find|while pipe was consuming the remaining filenames
from stdin as interactive commands, causing parse errors. Adding
< /dev/null isolates ffmpeg from the pipe so find can feed all paths
to the while loop uninterrupted.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-11 18:45:10 +02:00

48 lines
1.3 KiB
Bash
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.
#!/bin/sh
# Compress MP4 page-bundle videos with FFmpeg before Hugo builds.
#
# Usage: sh scripts/compress-videos.sh [content-dir]
#
# Encoding choices:
# H.264 CRF 28 — good web quality, ~60-80% smaller than camera/AI originals
# preset slow — best compression for a given CRF (fine in CI/Docker)
# faststart — moov atom moved to front for progressive streaming
# aac 64k — low-bitrate audio (videos are muted in-player by default)
#
# The script replaces each source file in-place only when the compressed
# output is actually smaller, so already-lean videos are left untouched.
set -e
CONTENT_DIR="${1:-content}"
find "$CONTENT_DIR" -name "*.mp4" | while IFS= read -r src; do
tmp="${src%.mp4}.__tmp.mp4"
echo "$src"
ffmpeg -i "$src" \
-c:v libx264 \
-crf 28 \
-preset slow \
-movflags +faststart \
-c:a aac \
-b:a 64k \
-loglevel error \
-y "$tmp" < /dev/null
src_size=$(stat -c%s "$src" 2>/dev/null || stat -f%z "$src")
tmp_size=$(stat -c%s "$tmp" 2>/dev/null || stat -f%z "$tmp")
if [ "$tmp_size" -lt "$src_size" ]; then
mv "$tmp" "$src"
saved=$(( (src_size - tmp_size) / 1024 ))
echo "${src_size}${tmp_size} bytes (${saved} KB)"
else
rm -f "$tmp"
echo " — already optimal, skipped"
fi
done
echo "Done."