Skip to content

programmatic video rendering framework that is actually fast

feat(landing): A/B test the made-of-motion film against the current intro video

Dmitriy Kovalenkof1b16aee6a4a
diff --git a/landing-worker/index.js b/landing-worker/index.js
new file mode 100644
index 00000000..0c0fa3e7
--- /dev/null
+++ b/landing-worker/index.js
@@ -0,0 +1,90 @@
+// A/B test of the landing page intro video. Only the page itself runs through this Worker
+// first (`assets.run_worker_first` in wrangler.jsonc); videos, posters and the rest of
+// landing/ are served as plain static assets.
+//
+// index.html is variant A as is. A visitor gets a variant at random on the first visit and
+// keeps it through the `intro` cookie; `?intro=a` or `?intro=b` shows a variant for that
+// request without changing the cookie. The variant goes to Plausible as the `intro`
+// property of the pageview (`event-intro` on the script tag) and of the custom events sent
+// from index.html, which read it from `<html data-intro>`.
+
+const COOKIE = "intro";
+const COOKIE_MAX_AGE = 60 * 60 * 24 * 90;
+
+/** Attribute and text replacements per variant, applied to index.html. */
+const VARIANTS = {
+ a: null,
+ b: {
+ poster: "made-of-motion-poster.jpg",
+ av1: "made-of-motion.av1.mp4",
+ h264: "made-of-motion.mp4",
+ label: "Made of motion, a film rebuilt with fframes",
+ caption:
+ 'Jordan Watkins’ film, rebuilt in Rust with <strong>fframes</strong> and <a href="https://github.com/dmtrKovalenko/fframes/tree/main/examples/made-of-motion">open source</a>',
+ },
+};
+
+const isVariant = (value) => Object.hasOwn(VARIANTS, value);
+
+function chooseVariant(request, url) {
+ const forced = url.searchParams.get(COOKIE);
+ if (isVariant(forced)) return { intro: forced, persist: false };
+
+ const cookie = request.headers.get("Cookie") ?? "";
+ const stored = new RegExp(`(?:^|;\\s*)${COOKIE}=([a-z])`).exec(cookie)?.[1];
+ if (isVariant(stored)) return { intro: stored, persist: false };
+
+ const keys = Object.keys(VARIANTS);
+ return { intro: keys[Math.floor(Math.random() * keys.length)], persist: true };
+}
+
+function rewrite(response, intro) {
+ const variant = VARIANTS[intro];
+ let rewriter = new HTMLRewriter()
+ .on("html", { element: (el) => el.setAttribute("data-intro", intro) })
+ .on("script[data-domain]", { element: (el) => el.setAttribute("event-intro", intro) });
+
+ if (variant) {
+ rewriter = rewriter
+ .on("figure video", {
+ element(el) {
+ el.setAttribute("poster", variant.poster);
+ el.setAttribute("aria-label", variant.label);
+ },
+ })
+ .on('figure source[data-codec="av1"]', { element: (el) => el.setAttribute("src", variant.av1) })
+ .on('figure source[data-codec="h264"]', { element: (el) => el.setAttribute("src", variant.h264) })
+ .on("figcaption span", { element: (el) => el.setInnerContent(variant.caption, { html: true }) });
+ }
+
+ return rewriter.transform(response);
+}
+
+export default {
+ async fetch(request, env) {
+ const url = new URL(request.url);
+ // Paths that match no asset reach the Worker too; leave their 404 to the assets.
+ const isPage = url.pathname === "/" || url.pathname === "/index.html";
+ if (!isPage || (request.method !== "GET" && request.method !== "HEAD")) {
+ return env.ASSETS.fetch(request);
+ }
+
+ // Ask for the page without the browser's conditional headers: a 304 would let the
+ // browser reuse a cached page of the other variant.
+ const page = await env.ASSETS.fetch(new Request(new URL("/", url), { method: request.method }));
+ if (!page.ok || !page.headers.get("Content-Type")?.startsWith("text/html")) return page;
+
+ const { intro, persist } = chooseVariant(request, url);
+ const response = new Response(rewrite(page, intro).body, page);
+ response.headers.delete("ETag");
+ response.headers.set("Cache-Control", "private, no-cache");
+ response.headers.append("Vary", "Cookie");
+ if (persist) {
+ response.headers.append(
+ "Set-Cookie",
+ `${COOKIE}=${intro}; Path=/; Max-Age=${COOKIE_MAX_AGE}; SameSite=Lax; Secure`,
+ );
+ }
+ return response;
+ },
+};
diff --git a/landing/index.html b/landing/index.html
index bbc35065..74a2c7df 100644
--- a/landing/index.html
+++ b/landing/index.html
@@ -306,6 +306,12 @@
border-radius: var(--r-lg);
background: var(--surface-secondary);
}
+ /* intro B (landing-worker/) is 4:3: narrower, so it is as tall as the 16:9 intro */
+ @media (min-width: 720px) {
+ html[data-intro="b"] figure { max-width: 75%; margin-inline: auto; }
+ }
+ html[data-intro="b"] .video-frame { aspect-ratio: 4 / 3; }
+ figcaption a { color: inherit; }
figcaption {
display: flex;
align-items: flex-start;
@@ -505,7 +511,8 @@
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:image" content="https://fframes.studio/brand/og-image.png" />
<!-- /fframes brand -->
- <script defer data-domain="fframes.studio" src="https://plausible.io/js/plausible.js"></script>
+ <script defer data-domain="fframes.studio" event-intro="a" src="https://plausible.io/js/script.pageview-props.js"></script>
+ <script>window.plausible = window.plausible || function () { (window.plausible.q = window.plausible.q || []).push(arguments); };</script>
</head>
<body>
<div class="light-leak" aria-hidden="true"></div>
@@ -557,8 +564,8 @@
<figure>
<div class="video-frame">
<video poster="poster.jpg" autoplay muted loop playsinline controls preload="metadata" aria-label="A video made with fframes">
- <source src="fframes-demo.av1.mp4" type='video/mp4; codecs="av01.0.08M.10, mp4a.40.2"'>
- <source src="fframes-demo.mp4" type='video/mp4; codecs="avc1.640029, mp4a.40.2"'>
+ <source data-codec="av1" src="fframes-demo.av1.mp4" type='video/mp4; codecs="av01.0.08M.10, mp4a.40.2"'>
+ <source data-codec="h264" src="fframes-demo.mp4" type='video/mp4; codecs="avc1.640029, mp4a.40.2"'>
</video>
</div>
<figcaption>
@@ -625,12 +632,40 @@
</svg>
</footer>
<script>
+ // Plausible goals, split by the intro variant that landing-worker/ picked
+ const intro = document.documentElement.dataset.intro ?? "a";
+ const track = (name) => window.plausible(name, { props: { intro } });
+
+ for (const link of document.querySelectorAll('a[href="https://github.com/dmtrKovalenko/fframes"]')) {
+ link.addEventListener("click", () => track("GitHub click"));
+ }
+
+ const introVideo = document.querySelector("figure video");
+ let unmuted = false;
+ let finished = false;
+ introVideo.addEventListener("volumechange", () => {
+ if (!unmuted && !introVideo.muted) {
+ unmuted = true;
+ track("Intro unmuted");
+ }
+ });
+ // The video loops, so "ended" never fires: count the first time it wraps around
+ let lastTime = 0;
+ introVideo.addEventListener("timeupdate", () => {
+ if (!finished && lastTime > introVideo.duration - 1 && introVideo.currentTime < 1) {
+ finished = true;
+ track("Intro finished");
+ }
+ lastTime = introVideo.currentTime;
+ });
+
// Copy the command without the prompt characters
for (const button of document.querySelectorAll(".copy")) {
button.addEventListener("click", async () => {
const text = button.previousElementSibling.textContent.replace(/^\$ /gm, "");
try {
await navigator.clipboard.writeText(text);
+ track("Copy command");
button.classList.add("copied");
button.setAttribute("aria-label", "Copied");
setTimeout(() => {
diff --git a/landing/made-of-motion-poster.jpg b/landing/made-of-motion-poster.jpg
new file mode 100644
index 00000000..aeb82baa
Binary files /dev/null and b/landing/made-of-motion-poster.jpg differ
diff --git a/landing/made-of-motion.av1.mp4 b/landing/made-of-motion.av1.mp4
new file mode 100644
index 00000000..e0b7b8f1
Binary files /dev/null and b/landing/made-of-motion.av1.mp4 differ
diff --git a/landing/made-of-motion.mp4 b/landing/made-of-motion.mp4
new file mode 100644
index 00000000..0f22f39b
Binary files /dev/null and b/landing/made-of-motion.mp4 differ
diff --git a/scripts/landing-video.sh b/scripts/landing-video.sh
index 26a14a76..3a708653 100755
--- a/scripts/landing-video.sh
+++ b/scripts/landing-video.sh
@@ -1,19 +1,22 @@
#!/usr/bin/env bash
# Encodes a rendered video into the two files the landing page plays:
#
-# scripts/landing-video.sh out.mp4
+# scripts/landing-video.sh out.mp4 [name]
#
-# landing/fframes-demo.av1.mp4 AV1 10-bit, played by browsers with AV1 support
-# landing/fframes-demo.mp4 H.264 [email protected], the fallback for everything else
+# landing/<name>.av1.mp4 AV1 10-bit, played by browsers with AV1 support
+# landing/<name>.mp4 H.264 [email protected], the fallback for everything else
#
-# Both are 1280x720 with AAC audio and the index at the front (faststart) so playback
-# starts before the download finishes. Cloudflare rejects files over 25 MiB, so the video
-# bitrate is derived from the duration to land each file near TARGET_MIB (default 23).
+# name defaults to fframes-demo, the intro of the page; made-of-motion is the intro that
+# landing-worker/ A/B tests against it. Both files are SIZE (default 1280:720) with AAC
+# audio and the index at the front (faststart) so playback starts before the download
+# finishes. Cloudflare rejects files over 25 MiB, so the video bitrate is derived from the
+# duration to land each file near TARGET_MIB (default 23).
# AV1 keeps film grain with grain synthesis: the encoder denoises, sends grain parameters
# and the decoder adds the grain back, which costs almost no bits.
set -euo pipefail
-input="${1:?usage: scripts/landing-video.sh <rendered-video>}"
+input="${1:?usage: scripts/landing-video.sh <rendered-video> [name]}"
+name="${2:-fframes-demo}"
out="$(cd "$(dirname "$0")/.." && pwd)/landing"
target_mib="${TARGET_MIB:-23}"
audio_kbps=96
@@ -25,15 +28,15 @@ duration=$(ffprobe -v error -show_entries format=duration -of csv=p=0 "$input")
video_kbps=$(echo "$target_mib * 1024 * 1024 * 8 / 1000 * 0.97 / $duration - $audio_kbps" | bc)
echo "duration ${duration}s, video ${video_kbps} kbps"
-scale="scale=1280:720:flags=lanczos"
+scale="scale=${SIZE:-1280:720}:flags=lanczos"
ffmpeg -hide_banner -loglevel error -y -i "$input" -vf "$scale" \
-c:v libsvtav1 -preset 4 -b:v "${video_kbps}k" -g 240 -pix_fmt yuv420p10le \
-svtav1-params "rc=1:tune=0:film-grain=8:film-grain-denoise=1" \
- -c:a aac -b:a "${audio_kbps}k" -movflags +faststart "$tmp/fframes-demo.av1.mp4"
+ -c:a aac -b:a "${audio_kbps}k" -movflags +faststart "$tmp/$name.av1.mp4"
for pass in 1 2; do
- target="$tmp/fframes-demo.mp4"
+ target="$tmp/$name.mp4"
[ "$pass" = 1 ] && target=/dev/null
ffmpeg -hide_banner -loglevel error -y -i "$input" -vf "$scale" \
-c:v libx264 -preset slow -b:v "${video_kbps}k" -pass "$pass" -passlogfile "$tmp/x264" \
@@ -51,4 +54,4 @@ for file in "$tmp"/*.mp4; do
done
mv "$tmp"/*.mp4 "$out/"
-ls -lh "$out"/fframes-demo*.mp4
+ls -lh "$out/$name".*mp4
diff --git a/wrangler.jsonc b/wrangler.jsonc
index aa04357d..52d3f82e 100644
--- a/wrangler.jsonc
+++ b/wrangler.jsonc
@@ -3,11 +3,17 @@
// npm runs the script from the repository root even when Builds starts in landing/.
// The build step publishes skills/ under landing/.well-known/agent-skills for
// `npx skills add https://fframes.studio`.
+// landing-worker/ runs only for the page itself, to A/B test the intro video.
{
"name": "fframes",
"account_id": "86ddf0334885d2955aa80c778509d857",
"compatibility_date": "2026-09-01",
"build": { "command": "npm run build:landing", "watch_dir": "skills" },
- "assets": { "directory": "./landing" },
+ "main": "./landing-worker/index.js",
+ "assets": {
+ "directory": "./landing",
+ "binding": "ASSETS",
+ "run_worker_first": ["/", "/index.html"]
+ },
"previews": {}
}