encode.sh 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env bash
  2. # 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 an
  5. # arbitrary time, and the decoder must walk from the previous keyframe to get
  6. # there, so a sparse-GOP file feels like mud under the wheel while playing back
  7. # perfectly. Dense keyframes cost file size and buy responsiveness. That trade
  8. # 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, so
  15. # a world like that wants 22-23 rather than the default 20:
  16. #
  17. # ./encode.sh in.mp4 out.mp4 desktop 23 fourth positional argument
  18. # SCROLLCRAFT_CRF=23 ./encode.sh in.mp4 out.mp4 or the env var
  19. #
  20. # The positional argument wins over the env var; with neither, the defaults
  21. # below are unchanged.
  22. #
  23. # Audio is stripped: these clips are scrubbed, never played, and a muted track
  24. # is dead weight plus an autoplay-policy hazard.
  25. set -euo pipefail
  26. # Resolve a FULL ffmpeg build. Several toolchains (Remotion, some Electron apps)
  27. # put a stripped ffmpeg on PATH that carries maybe 50 filters and silently lacks
  28. # scale/fps/tile. It fails with "No option name near ..." on any filter chain,
  29. # which reads like a syntax error in your command rather than a missing filter.
  30. # Count the filters and go looking for a real build if the count is low.
  31. pick_ffmpeg() {
  32. local cand
  33. for cand in \
  34. "${SCROLLCRAFT_FFMPEG:-}" \
  35. "$(command -v ffmpeg 2>/dev/null || true)" \
  36. "${HOME:-/nonexistent}"/AppData/Local/Microsoft/WinGet/Packages/Gyan.FFmpeg_*/ffmpeg-*-full_build/bin/ffmpeg.exe \
  37. /usr/local/bin/ffmpeg /opt/homebrew/bin/ffmpeg /usr/bin/ffmpeg /snap/bin/ffmpeg
  38. do
  39. [ -n "$cand" ] && [ -x "$cand" ] || continue
  40. if [ "$("$cand" -hide_banner -filters 2>/dev/null | wc -l)" -gt 200 ]; then
  41. echo "$cand"; return 0
  42. fi
  43. done
  44. echo "ERROR: no full ffmpeg build found. Set SCROLLCRAFT_FFMPEG to one." >&2
  45. return 1
  46. }
  47. FFMPEG="$(pick_ffmpeg)"
  48. FFPROBE="$(dirname "$FFMPEG")/ffprobe"
  49. [ -x "$FFPROBE" ] || FFPROBE="$(command -v ffprobe)"
  50. IN="${1:?usage: encode.sh <in> <out> [mobile|desktop] [crf]}"
  51. OUT="${2:?usage: encode.sh <in> <out> [mobile|desktop] [crf]}"
  52. MODE="${3:-desktop}"
  53. if [ "$MODE" = "mobile" ]; then
  54. SCALE="scale=-2:720"; GOP=4; CRF=24
  55. else
  56. SCALE="scale=-2:1080"; GOP=8; CRF=20
  57. fi
  58. # Positional beats env var beats the mode default.
  59. CRF="${4:-${SCROLLCRAFT_CRF:-$CRF}}"
  60. case "$CRF" in
  61. ''|*[!0-9]*) echo "ERROR: crf must be an integer, got '$CRF'" >&2; exit 1 ;;
  62. esac
  63. mkdir -p "$(dirname "$OUT")"
  64. "$FFMPEG" -y -hide_banner -loglevel error -i "$IN" \
  65. -an \
  66. -vf "${SCALE}:flags=lanczos,format=yuv420p" \
  67. -c:v libx264 -profile:v high -preset slow -crf "$CRF" \
  68. -g "$GOP" -keyint_min "$GOP" -sc_threshold 0 \
  69. -movflags +faststart \
  70. "$OUT"
  71. SIZE=$(du -h "$OUT" | cut -f1)
  72. DUR=$("$FFPROBE" -v error -show_entries format=duration -of csv=p=0 "$OUT")
  73. echo "$OUT ${SIZE} ${DUR}s gop=${GOP} crf=${CRF}"