#!/bin/bash # Render a HyperFrames project in ~/videos across exe.dev sandboxes: # plan it here, copy this VM into one sandbox per chunk (the copies carry # the plan), render the chunks at once, fetch them, stitch them, and # delete the sandboxes. # hf-fanout [chunks=4] [quality=standard] # ~/render-api/fanout.env sets EXE_API, the exe.dev API integration's URL, # and optionally SANDBOX_FLAGS (each sandbox's size) and HF_FPS. set -euo pipefail set -a; . "$HOME/render-api/fanout.env"; set +a project=${1:?usage: hf-fanout [chunks] [quality]} chunks=${2:-4} quality=${3:-standard} [[ $chunks =~ ^[0-9]+$ ]] && ((chunks >= 1 && chunks <= 16)) || { echo "chunks: 1 to 16" >&2; exit 2; } [ -d "$HOME/videos/$project" ] || { echo "no project ~/videos/$project" >&2; exit 2; } SANDBOX_FLAGS=${SANDBOX_FLAGS:---cpu=2 --memory=4GB} id=$(date +%m%d-%H%M%S) work=$HOME/renders/.fanout/$id out=$HOME/renders/$project-$id.mp4 base=$(hostname) # One exe.dev command, e.g. "cp", "rm" or "ssh "; prints its output. exe() { curl -fsS -m 3600 -X POST "$EXE_API/exec" --data-binary "$*"; } # Run a script on a VM; base64 carries its quotes through exe.dev's parser. on() { local vm=$1; shift; exe "ssh $vm echo $(printf '%s' "$*" | base64 -w0) | base64 -d | bash"; } sandboxes=() cleanup() { for s in "${sandboxes[@]}"; do exe "rm $s" >/dev/null || echo "could not delete $s" >&2; done rm -rf "$work" } trap cleanup EXIT start=$SECONDS cd "$HOME/render-api" mkdir -p "$work" node hf-chunks.mjs plan "$HOME/videos/$project" "$work/plan" "$chunks" "$quality" >"$work/plan.log" 2>&1 \ || { tail -n 20 "$work/plan.log" >&2; exit 1; } n=$(sed -n 's/^RESULT //p' "$work/plan.log" | jq -r .chunkCount) echo "planned $n chunks in $((SECONDS - start))s" sync # the copies must see the whole plan on disk for ((i = 0; i < n; i++)); do s="$base-$id-$i" exe "cp $base $s --sandbox $SANDBOX_FLAGS" >/dev/null sandboxes+=("$s") done echo "copied to ${#sandboxes[@]} sandboxes in $((SECONDS - start))s" pids=() for ((i = 0; i < n; i++)); do s=${sandboxes[$i]} ( until exe "ssh $s true" >/dev/null 2>&1; do sleep 2; done # exec answers 200 even when the command fails, so look for RESULT. log=$(on $s "cd render-api && PRODUCER_LOW_MEMORY_MODE=false node hf-chunks.mjs chunk $work/plan $i /tmp/chunk.mp4 2>&1 | tail -n 20") grep -q '^RESULT ' <<<"$log" || { printf 'chunk %s failed on %s:\n%s\n' "$i" "$s" "$log" >&2; exit 1; } exe "ssh $s base64 -w0 /tmp/chunk.mp4" | base64 -d > "$work/$i.mp4" [ -s "$work/$i.mp4" ] echo "chunk $i done at $((SECONDS - start))s" ) & pids+=($!) done for p in "${pids[@]}"; do wait "$p"; done chunkfiles=() for ((i = 0; i < n; i++)); do chunkfiles+=("$work/$i.mp4"); done node hf-chunks.mjs assemble "$work/plan" "$out" "${chunkfiles[@]}" >"$work/assemble.log" 2>&1 \ || { tail -n 20 "$work/assemble.log" >&2; exit 1; } echo "rendered $out in $((SECONDS - start))s"