scripts/encode.sh
scripts/encode.shBrowse 22 files
1,021 tokens
3,222 bytes
Token encoding: o200k_base
Snapshot 24fd22b
← Back to SKILL.md
1#!/usr/bin/env bash2# scrollcraft: encode a clip for scrubbing, not for playback.3#4# A normal web encode puts a keyframe every 2-5 seconds. Scrubbing seeks to an5# arbitrary time, and the decoder must walk from the previous keyframe to get6# there, so a sparse-GOP file feels like mud under the wheel while playing back7# perfectly. Dense keyframes cost file size and buy responsiveness. That trade8# is the entire point of this script.9#10# ./encode.sh in.mp4 out.mp4 desktop master (1080p, -g 8, crf 20)11# ./encode.sh in.mp4 out-m.mp4 mobile phone variant (720p, -g 4, crf 24)12#13# CRF override, for the grain-heavy worlds assets.md warns about (marine snow,14# bioluminescence, smoke, film grain). A dense GOP doubles the cost of grain, so15# a world like that wants 22-23 rather than the default 20:16#17# ./encode.sh in.mp4 out.mp4 desktop 23 fourth positional argument18# SCROLLCRAFT_CRF=23 ./encode.sh in.mp4 out.mp4 or the env var19#20# The positional argument wins over the env var; with neither, the defaults21# below are unchanged.22#23# Audio is stripped: these clips are scrubbed, never played, and a muted track24# is dead weight plus an autoplay-policy hazard.25set -euo pipefail26 27# Resolve a FULL ffmpeg build. Several toolchains (Remotion, some Electron apps)28# put a stripped ffmpeg on PATH that carries maybe 50 filters and silently lacks29# scale/fps/tile. It fails with "No option name near ..." on any filter chain,30# which reads like a syntax error in your command rather than a missing filter.31# Count the filters and go looking for a real build if the count is low.32pick_ffmpeg() {33 local cand34 for cand in \35 "${SCROLLCRAFT_FFMPEG:-}" \36 "$(command -v ffmpeg 2>/dev/null || true)" \37 "${HOME:-/nonexistent}"/AppData/Local/Microsoft/WinGet/Packages/Gyan.FFmpeg_*/ffmpeg-*-full_build/bin/ffmpeg.exe \38 /usr/local/bin/ffmpeg /opt/homebrew/bin/ffmpeg /usr/bin/ffmpeg /snap/bin/ffmpeg39 do40 [ -n "$cand" ] && [ -x "$cand" ] || continue41 if [ "$("$cand" -hide_banner -filters 2>/dev/null | wc -l)" -gt 200 ]; then42 echo "$cand"; return 043 fi44 done45 echo "ERROR: no full ffmpeg build found. Set SCROLLCRAFT_FFMPEG to one." >&246 return 147}48FFMPEG="$(pick_ffmpeg)"49FFPROBE="$(dirname "$FFMPEG")/ffprobe"50[ -x "$FFPROBE" ] || FFPROBE="$(command -v ffprobe)"51 52IN="${1:?usage: encode.sh <in> <out> [mobile|desktop] [crf]}"53OUT="${2:?usage: encode.sh <in> <out> [mobile|desktop] [crf]}"54MODE="${3:-desktop}"55 56if [ "$MODE" = "mobile" ]; then57 SCALE="scale=-2:720"; GOP=4; CRF=2458else59 SCALE="scale=-2:1080"; GOP=8; CRF=2060fi61 62# Positional beats env var beats the mode default.63CRF="${4:-${SCROLLCRAFT_CRF:-$CRF}}"64case "$CRF" in65 ''|*[!0-9]*) echo "ERROR: crf must be an integer, got '$CRF'" >&2; exit 1 ;;66esac67 68mkdir -p "$(dirname "$OUT")"69 70"$FFMPEG" -y -hide_banner -loglevel error -i "$IN" \71 -an \72 -vf "${SCALE}:flags=lanczos,format=yuv420p" \73 -c:v libx264 -profile:v high -preset slow -crf "$CRF" \74 -g "$GOP" -keyint_min "$GOP" -sc_threshold 0 \75 -movflags +faststart \76 "$OUT"77 78SIZE=$(du -h "$OUT" | cut -f1)79DUR=$("$FFPROBE" -v error -show_entries format=duration -of csv=p=0 "$OUT")80echo "$OUT ${SIZE} ${DUR}s gop=${GOP} crf=${CRF}"81