render.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env bash
  2. # p5.js Skill — Headless Render Pipeline
  3. # Renders a p5.js sketch to MP4 video via Puppeteer + ffmpeg
  4. #
  5. # Usage:
  6. # bash scripts/render.sh sketch.html output.mp4 [options]
  7. #
  8. # Options:
  9. # --width Canvas width (default: 1920)
  10. # --height Canvas height (default: 1080)
  11. # --fps Frames per second (default: 30)
  12. # --duration Duration in seconds (default: 10)
  13. # --quality CRF value 0-51 (default: 18, lower = better)
  14. # --frames-only Only export frames, skip MP4 encoding
  15. #
  16. # Examples:
  17. # bash scripts/render.sh sketch.html output.mp4
  18. # bash scripts/render.sh sketch.html output.mp4 --duration 30 --fps 60
  19. # bash scripts/render.sh sketch.html output.mp4 --width 3840 --height 2160
  20. set -euo pipefail
  21. # Defaults
  22. WIDTH=1920
  23. HEIGHT=1080
  24. FPS=30
  25. DURATION=10
  26. CRF=18
  27. FRAMES_ONLY=false
  28. # Parse arguments
  29. INPUT="${1:?Usage: render.sh <input.html> <output.mp4> [options]}"
  30. OUTPUT="${2:?Usage: render.sh <input.html> <output.mp4> [options]}"
  31. shift 2
  32. while [[ $# -gt 0 ]]; do
  33. case $1 in
  34. --width) WIDTH="$2"; shift 2 ;;
  35. --height) HEIGHT="$2"; shift 2 ;;
  36. --fps) FPS="$2"; shift 2 ;;
  37. --duration) DURATION="$2"; shift 2 ;;
  38. --quality) CRF="$2"; shift 2 ;;
  39. --frames-only) FRAMES_ONLY=true; shift ;;
  40. *) echo "Unknown option: $1"; exit 1 ;;
  41. esac
  42. done
  43. TOTAL_FRAMES=$((FPS * DURATION))
  44. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  45. FRAME_DIR=$(mktemp -d)
  46. echo "=== p5.js Render Pipeline ==="
  47. echo "Input: $INPUT"
  48. echo "Output: $OUTPUT"
  49. echo "Resolution: ${WIDTH}x${HEIGHT}"
  50. echo "FPS: $FPS"
  51. echo "Duration: ${DURATION}s (${TOTAL_FRAMES} frames)"
  52. echo "Quality: CRF $CRF"
  53. echo "Frame dir: $FRAME_DIR"
  54. echo ""
  55. # Check dependencies
  56. command -v node >/dev/null 2>&1 || { echo "Error: Node.js required"; exit 1; }
  57. if [ "$FRAMES_ONLY" = false ]; then
  58. command -v ffmpeg >/dev/null 2>&1 || { echo "Error: ffmpeg required for MP4"; exit 1; }
  59. fi
  60. # Step 1: Capture frames via Puppeteer
  61. echo "Step 1/2: Capturing ${TOTAL_FRAMES} frames..."
  62. node "$SCRIPT_DIR/export-frames.js" \
  63. "$INPUT" \
  64. --output "$FRAME_DIR" \
  65. --width "$WIDTH" \
  66. --height "$HEIGHT" \
  67. --frames "$TOTAL_FRAMES" \
  68. --fps "$FPS"
  69. echo "Frames captured to $FRAME_DIR"
  70. if [ "$FRAMES_ONLY" = true ]; then
  71. echo "Frames saved to: $FRAME_DIR"
  72. echo "To encode manually:"
  73. echo " ffmpeg -framerate $FPS -i $FRAME_DIR/frame-%04d.png -c:v libx264 -crf $CRF -pix_fmt yuv420p $OUTPUT"
  74. exit 0
  75. fi
  76. # Step 2: Encode to MP4
  77. echo "Step 2/2: Encoding MP4..."
  78. ffmpeg -y \
  79. -framerate "$FPS" \
  80. -i "$FRAME_DIR/frame-%04d.png" \
  81. -c:v libx264 \
  82. -preset slow \
  83. -crf "$CRF" \
  84. -pix_fmt yuv420p \
  85. -movflags +faststart \
  86. "$OUTPUT" \
  87. 2>"$FRAME_DIR/ffmpeg.log"
  88. # Cleanup
  89. rm -rf "$FRAME_DIR"
  90. # Report
  91. FILE_SIZE=$(ls -lh "$OUTPUT" | awk '{print $5}')
  92. echo ""
  93. echo "=== Done ==="
  94. echo "Output: $OUTPUT ($FILE_SIZE)"
  95. echo "Duration: ${DURATION}s at ${FPS}fps, ${WIDTH}x${HEIGHT}"