Explainer videos and product demos made by your AI agent. Free and open source: a local voice (Kokoro), word timing (Whisper) and a canvas renderer turn a script into a narrated MP4.
JavaScript ai-agentsanimationcanvasclaude-codecodexexplainer-videoffmpegjavascriptkokoroproduct-demotext-to-speechvideo-generationwhisper
Export a web player that draws the video live, sharp at any size (#20)
Vincent Schmalbach060e367166aa
diff --git a/AGENTS.md b/AGENTS.mdindex 16bf0a0..70c8429 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -761,6 +761,7 @@ when `ink` or `muted` has too little contrast against `bg`.| `check [project]` | finds layout, timing and pronunciation problems (`--size`) || `render [project]` | the video plus subtitle files, chapters and a cover (`--format mp4`, `gif`, `webm` or `mov`, `--transparent`, `--size`, `--draft`, `--from`, `--to`, `--workers`, `--out`) || `verify [project]` | checks the finished MP4, WebM or MOV: loudness, black frames, silence, clear voice (`--file` picks another file) |+| `export [project]` | writes a web player folder (`--out dir`, `--json`), by default `out/player/` || `capture [project] <url>` | saves screenshots, wording, colors, fonts and logos from a live site (`--mobile`, `--json`) || `image [project] <name> "<prompt>"` | makes an illustration with OpenRouter || `images [project]` | lists the images and what they cost |@@ -794,6 +795,46 @@ transitions become `fade` in a transparent video. Check frames withtransparent video. Without `--transparent`, WebM and MOV are ordinary opaquevideos.+## Web player++Use `export` for a video on a docs site or in an iframe. The canvas draws+at the screen's resolution, so text stays sharp when the player grows.+The folder has its own engine, fonts, assets, soundtrack, cover and subtitles.+It needs no explainroo server. The voice is made first if needed.++```bash+node bin/explainroo.js export videos/my-video+```++Serve `out/player/` with any static web server. Opening `index.html` as a+local file does not work. Copy the folder to your site, keeping its files+together. It works in a subfolder too.++For a 1920 x 1080 video, embed it like this. Change the aspect ratio to+match your video's width and height.++```html+<div style="aspect-ratio: 1920 / 1080; width: 100%">+ <iframe src="/videos/my-video/index.html" title="My video"+ style="width: 100%; height: 100%; border: 0"+ allow="autoplay; fullscreen" allowfullscreen></iframe>+</div>+```++`index.html?autoplay=1&muted=1` starts muted playback, subject to the+browser's autoplay rules. `index.html?t=12.5` starts at 12.5 seconds.+Click the picture to play or pause. Space or K does the same. The arrow+keys seek five seconds. F opens fullscreen, and M mutes the sound.+CC shows text captions above the controls. It starts off and remembers+your choice in the browser. Captions drawn in the canvas stay as they are.++Clips use the browser's video decoder. Use a format the browser can play.+Their timing can differ a little from the MP4. Math formulas are included+as typeset paths. Project modules must use relative local imports. Dotfiles,+JSON and text files from the project are kept out of the export.++The MP4 remains the file to upload to video platforms.+## Scene APIEach scene function draws one frame. explainroo calls it for every frame withdiff --git a/engine/clips.js b/engine/clips.jsindex eec8db6..1b2d4ec 100644--- a/engine/clips.js+++ b/engine/clips.js@@ -11,9 +11,32 @@ export function clipURL(kind, src, frame) {return `/__clip/${kind}?path=${encodeURIComponent(src)}${frame === undefined ? '' : '&frame=' + frame}`;}-export async function loadClips(assets) {+export async function loadClips(assets, state = null) {const clips = new Map();await Promise.all(assets.filter((a) => VIDEO_FILE.test(a)).map(async (src) => {+ if (state) {+ const video = document.createElement('video');+ video.hidden = true;+ video.muted = true;+ video.playsInline = true;+ video.preload = 'auto';+ video.src = './project/' + src.split('/').map(encodeURIComponent).join('/');+ document.body.append(video);+ const loaded = () => new Promise((resolve, reject) => {+ video.onloadeddata = resolve;+ video.onerror = () => reject(new Error(`clip "${src}" could not be loaded. Use a browser-supported video format.`));+ });+ await loaded();+ // Servers without byte ranges cannot seek a media URL in Chrome.+ if (!video.seekable.length || video.seekable.end(video.seekable.length - 1) < video.duration - 0.1) {+ const res = await fetch(video.src);+ if (!res.ok) throw new Error(`clip "${src}" could not be loaded`);+ video.src = URL.createObjectURL(await res.blob());+ await loaded();+ }+ clips.set(src, { src, video, duration: video.duration, width: video.videoWidth, height: video.videoHeight, fps: state.config.fps, frames: Math.max(1, Math.ceil(video.duration * state.config.fps)) });+ return;+ }const res = await fetch(clipURL('info', src));if (!res.ok) throw new Error(`clip "${src}" could not be loaded. Put a video in the project's assets/ folder.`);clips.set(src, { src, ...await res.json() });diff --git a/engine/player.html b/engine/player.htmlnew file mode 100644index 0000000..93ac19e--- /dev/null+++ b/engine/player.html@@ -0,0 +1,67 @@+<!doctype html>+<html lang="en">+<head>+<meta charset="utf-8">+<meta name="viewport" content="width=device-width, initial-scale=1">+<meta name="generator" content="explainroo web player">+<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; font-src 'self'; connect-src 'self'; media-src 'self' blob:; object-src 'none'; base-uri 'none'">+<title>explainroo player</title>+<style>+*{box-sizing:border-box}html,body{margin:0;background:#111;color:#fff;font:14px system-ui,sans-serif}+#player{--accent:#c2e7ff;position:relative;width:100%;aspect-ratio:16/9;overflow:hidden;background:#111;container-type:inline-size;outline-offset:-3px}+#player:fullscreen{height:100%;display:grid;place-items:center}#player.quiet:fullscreen{cursor:none}+canvas,#poster{display:block;width:100%;height:100%;object-fit:contain}canvas{position:absolute;inset:0}+#poster{position:absolute;inset:0}#poster[hidden],#start[hidden],#controls[hidden]{display:none}+button{display:grid;place-items:center;position:relative;flex:none;width:44px;height:44px;padding:0;color:inherit;background:transparent;border:0;border-radius:8px;cursor:pointer;transition:background .15s}+button:hover{background:rgba(255,255,255,.12)}button:disabled{cursor:wait}+button svg{width:22px;height:22px;fill:none;stroke:currentColor;stroke-width:1.8;stroke-linecap:round;stroke-linejoin:round;pointer-events:none}+button[data-icon="play"] svg,button[data-icon="pause"] svg{fill:currentColor;stroke-width:1.2}+button:focus-visible,input:focus-visible,#player:focus-visible{outline:2px solid var(--accent);outline-offset:3px}+#player:focus-visible{outline-offset:-3px}+#start{position:absolute;left:50%;top:50%;transform:translate(-50%,-50%);width:clamp(60px,16cqw,88px);height:clamp(60px,16cqw,88px);border-radius:50%;color:#101724;background:#f7f9fc;box-shadow:0 6px 32px rgba(0,0,0,.25);transition:background .15s,scale .15s}+#start:focus-visible{box-shadow:0 0 0 6px rgba(0,0,0,.7),0 6px 32px rgba(0,0,0,.25)}+#start:hover{background:#fff;scale:1.05}#start svg{width:42%;height:42%;fill:currentColor;stroke-width:1.2;translate:2px 0}+#controls{position:absolute;bottom:0;left:0;z-index:2;isolation:isolate;width:100%;padding:0 12px 6px;opacity:1;transition:opacity .2s}+#controls::before{content:"";position:absolute;inset:-40px 0 0;z-index:-1;pointer-events:none;background:linear-gradient(to bottom,transparent,rgba(5,7,12,.88) 45%,rgba(5,7,12,.98))}+#player.quiet #controls{opacity:0;pointer-events:none}#player.quiet #controls:has(:focus-visible){opacity:1;pointer-events:auto}+#buttons{display:flex;align-items:center;gap:4px}#cc{margin-left:auto}+#cc[aria-pressed="true"]{color:var(--accent);background:rgba(194,231,255,.12)}#cc[aria-pressed="true"]::after{content:"";position:absolute;bottom:5px;left:calc(50% - 7px);width:14px;height:2px;border-radius:2px;background:var(--accent)}+#seek{--played:0%;display:block;width:100%;height:40px;margin:0;padding:0;appearance:none;-webkit-appearance:none;background:transparent;cursor:pointer;touch-action:pan-y}+#seek::-webkit-slider-runnable-track{height:3px;border-radius:3px;background:linear-gradient(to right,var(--accent) var(--played),rgba(255,255,255,.3) var(--played))}+#seek::-moz-range-track{height:3px;border-radius:3px;background:rgba(255,255,255,.3)}#seek::-moz-range-progress{height:3px;border-radius:3px;background:var(--accent)}+#seek::-webkit-slider-thumb{appearance:none;-webkit-appearance:none;width:10px;height:10px;margin-top:-3.5px;border:0;border-radius:50%;background:var(--accent);transition:transform .15s}+#seek::-moz-range-thumb{width:10px;height:10px;border:0;border-radius:50%;background:var(--accent);transition:transform .15s}+#seek:hover::-webkit-slider-thumb,#seek:focus-visible::-webkit-slider-thumb{transform:scale(1.6)}#seek:hover::-moz-range-thumb,#seek:focus-visible::-moz-range-thumb{transform:scale(1.6)}+#time{margin-left:6px;color:rgba(255,255,255,.9);font-size:12px;font-variant-numeric:tabular-nums;white-space:nowrap;letter-spacing:.015em}+#captions{position:absolute;bottom:100px;left:7%;right:7%;z-index:3;text-align:center;white-space:pre-line;font-size:clamp(16px,2.2cqw,32px);line-height:1.4;pointer-events:none}+#player.quiet #captions{bottom:20px}#captions span{background:rgba(0,0,0,.8);padding:3px 8px;border-radius:4px;box-decoration-break:clone;-webkit-box-decoration-break:clone}#captions:empty{display:none}+#status{position:absolute;top:12px;left:12px;right:12px;text-align:center;background:rgba(0,0,0,.8)}#status:empty{display:none}+@container(min-width:700px){#controls{padding:8px 20px 10px}#seek{height:20px}button{width:36px;height:36px}button svg{width:20px;height:20px}#buttons{gap:8px}#captions{bottom:84px}}+@container(max-width:360px){button{width:40px;height:40px}button svg{width:20px;height:20px}#buttons{gap:2px}#controls{padding-inline:10px}#time{margin-left:4px}}+@media(prefers-reduced-motion:reduce){#controls,button,#start{transition:none}#seek::-webkit-slider-thumb{transition:none}#seek::-moz-range-thumb{transition:none}}+</style>+</head>+<body>+<main id="player" tabindex="0" aria-label="Video player">+<canvas id="c" aria-label="Video"></canvas>+<img id="poster" src="./poster.jpg" alt="Video cover">+<button id="start" aria-label="Play" title="Play" disabled>+<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M5 5a2 2 0 0 1 3.008-1.728l11.997 6.998a2 2 0 0 1 .003 3.458l-12 7A2 2 0 0 1 5 19z"/></svg>+</button>+<div id="captions" role="region" aria-label="Captions"></div>+<div id="status" role="status"></div>+<div id="controls" role="group" aria-label="Playback controls" hidden>+<input id="seek" type="range" min="0" max="0" step="0.01" value="0" aria-label="Seek" disabled>+<div id="buttons">+<button id="play" aria-label="Play" title="Play" disabled></button>+<button id="mute" aria-label="Mute" title="Mute" aria-pressed="false"></button>+<span id="time">0:00 / 0:00</span>+<button id="cc" aria-label="Show captions" title="Show captions" aria-pressed="false"></button>+<button id="fullscreen" aria-label="Enter fullscreen" title="Enter fullscreen"></button>+</div>+</div>+</main>+<audio id="audio" src="./audio.m4a" preload="none"><track id="track" kind="subtitles" src="./subtitles.vtt" srclang="en" label="English"></audio>+<script type="module" src="./engine/player.js"></script>+</body>+</html>diff --git a/engine/player.js b/engine/player.jsnew file mode 100644index 0000000..56e50c9--- /dev/null+++ b/engine/player.js@@ -0,0 +1,219 @@+import { boot } from './runtime.js';++const get = id => document.getElementById(id);+const player = get('player');+const canvas = get('c');+const audio = get('audio');+const seekBar = get('seek');+const playButton = get('play');+const startButton = get('start');+const poster = get('poster');+const captions = get('captions');+const params = new URLSearchParams(location.search);+let engine;+let duration = 0;+let T = 0;+let playing = false;+let started = false;+let audioClock = false;+let stamp = performance.now();+let audioFailed = false;+let request;+let hideTimer;+let cc = false;+try { cc = localStorage.getItem('explainroo-cc') === '1'; } catch {}+audio.muted = params.get('muted') === '1';+const track = get('track').track;+track.mode = 'hidden';++const formatTime = time => `${Math.floor(time / 60)}:${String(Math.floor(time % 60)).padStart(2, '0')}`;+function iconButton(button, name, label) {+ if (button.getAttribute('aria-label') !== label) button.setAttribute('aria-label', label);+ if (button.title !== label) button.title = label;+ if (!engine || button.dataset.icon === name) return;+ const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');+ svg.setAttribute('viewBox', '0 0 24 24');+ svg.setAttribute('aria-hidden', 'true');+ svg.setAttribute('focusable', 'false');+ for (const d of engine.icons[name]) {+ const path = document.createElementNS(svg.namespaceURI, 'path');+ path.setAttribute('d', d);+ svg.append(path);+ }+ button.replaceChildren(svg);+ button.dataset.icon = name;+}+function controls() {+ iconButton(playButton, playing ? 'pause' : 'play', playing ? 'Pause' : 'Play');+ iconButton(get('mute'), audio.muted ? 'volume-x' : 'volume-2', audio.muted ? 'Unmute' : 'Mute');+ iconButton(get('cc'), 'closed-caption', cc ? 'Hide captions' : 'Show captions');+ iconButton(get('fullscreen'), document.fullscreenElement ? 'minimize' : 'maximize', document.fullscreenElement ? 'Exit fullscreen' : 'Enter fullscreen');+ get('mute').setAttribute('aria-pressed', String(audio.muted));+ get('cc').setAttribute('aria-pressed', String(cc));+ seekBar.value = T;+ seekBar.style.setProperty('--played', `${duration ? T / duration * 100 : 0}%`);+ seekBar.setAttribute('aria-valuetext', `${formatTime(T)} of ${formatTime(duration)}`);+ get('time').textContent = `${formatTime(T)} / ${formatTime(duration)}`;+}+function showControls() {+ player.classList.remove('quiet');+ clearTimeout(hideTimer);+ if (playing) hideTimer = setTimeout(() => {+ if (!get('controls').querySelector(':focus-visible')) player.classList.add('quiet');+ }, 2000);+}+function draw() {+ if (engine && started) engine.renderFrame(Math.min(T, Math.max(0, duration - 1 / engine.fps)));+ const cues = [...(track.cues || [])].filter(c => c.startTime <= T && T < c.endTime);+ captions.replaceChildren();+ if (cc && started && cues.length) {+ const span = document.createElement('span');+ span.textContent = cues.map(c => c.getCueAsHTML().textContent).join('\n');+ captions.append(span);+ }+ controls();+}+function resize() {+ if (!engine) return;+ const bounds = player.getBoundingClientRect();+ const scale = Math.min(bounds.width / engine.W, bounds.height / engine.H);+ const width = Math.max(1, Math.round(engine.W * scale * devicePixelRatio));+ const height = Math.max(1, Math.round(engine.H * scale * devicePixelRatio));+ if (canvas.width !== width || canvas.height !== height) {+ canvas.width = width;+ canvas.height = height;+ for (const buffer of engine.buffers) { buffer.width = width; buffer.height = height; }+ engine.scale = width / engine.W;+ canvas.style.width = `${engine.W * scale}px`;+ canvas.style.height = `${engine.H * scale}px`;+ canvas.style.left = `${(bounds.width - engine.W * scale) / 2}px`;+ canvas.style.top = `${(bounds.height - engine.H * scale) / 2}px`;+ }+ draw();+}+function tick(now) {+ if (!playing) return;+ // The sound leads. While it loads or buffers the picture waits, so no words+ // are cut; only a soundtrack that cannot play at all falls back to the clock.+ if (audioClock) T = Math.min(duration, audio.currentTime);+ else if (audioFailed) T = Math.min(duration, T + (now - stamp) / 1000);+ stamp = now;+ draw();+ if (T >= duration) pause();+ else request = requestAnimationFrame(tick);+}+function pause() {+ playing = false;+ audio.pause();+ audioClock = false;+ cancelAnimationFrame(request);+ showControls();+ controls();+}+async function play() {+ if (!engine || playing) return;+ if (T >= duration) seek(0);+ started = true;+ poster.hidden = true;+ startButton.hidden = true;+ get('controls').hidden = false;+ playing = true;+ stamp = performance.now();+ audio.currentTime = T;+ draw();+ showControls();+ request = requestAnimationFrame(tick);+ try { await audio.play(); }+ catch (e) {+ if (e.name === 'NotAllowedError') { pause(); get('status').textContent = 'Press Play to start playback.'; }+ }+}+function seek(time) {+ T = Math.max(0, Math.min(duration, time));+ stamp = performance.now();+ audio.currentTime = T;+ draw();+}+function toggle() { if (playing) pause(); else play(); }+function mute() { audio.muted = !audio.muted; controls(); }+async function fullscreen() {+ try {+ if (document.fullscreenElement) await document.exitFullscreen();+ else await player.requestFullscreen();+ } catch { get('status').textContent = 'Fullscreen is not available here.'; }+}+get('play').onclick = toggle;+startButton.onclick = toggle;+for (const el of [canvas, poster]) el.onclick = toggle;+seekBar.oninput = () => seek(Number(seekBar.value));+get('mute').onclick = mute;+get('fullscreen').onclick = fullscreen;+get('cc').onclick = () => {+ cc = !cc;+ try { localStorage.setItem('explainroo-cc', cc ? '1' : '0'); } catch {}+ draw();+};+player.onpointermove = showControls;+player.onpointerdown = showControls;+player.onfocusin = showControls;+player.onfocusout = showControls;+document.onkeydown = event => {+ if (event.altKey || event.ctrlKey || event.metaKey) return;+ const key = event.key.toLowerCase();+ // Leave a focused button's native Space click and the slider's arrows intact.+ if ((key === ' ' && event.target.tagName === 'BUTTON') || (['arrowleft', 'arrowright'].includes(key) && event.target === seekBar)) return;+ const actions = { ' ': toggle, k: toggle, arrowleft: () => seek(T - 5), arrowright: () => seek(T + 5), f: fullscreen, m: mute };+ if (!actions[key]) return;+ event.preventDefault();+ actions[key]();+ showControls();+};+audio.onplaying = () => {+ if (!playing) return;+ if (Math.abs(audio.currentTime - T) > 0.1) audio.currentTime = T;+ audioClock = true;+ get('status').textContent = '';+};+audio.onwaiting = audio.onseeking = () => { audioClock = false; stamp = performance.now(); };+audio.onseeked = () => { audioClock = playing && !audio.paused && audio.readyState >= 3; };+audio.onloadedmetadata = async () => {+ if (audio.src.startsWith('blob:') || (audio.seekable.length && audio.seekable.end(audio.seekable.length - 1) >= duration - 0.1)) return;+ audioClock = false;+ try {+ const res = await fetch('./audio.m4a');+ if (!res.ok) return;+ const blob = await res.blob();+ audio.pause();+ audio.src = URL.createObjectURL(blob);+ audio.currentTime = T;+ if (playing) await audio.play();+ } catch {}+};+audio.onended = () => { T = duration; draw(); pause(); };+audio.onerror = () => { audioFailed = true; audioClock = false; stamp = performance.now(); };+get('track').onload = draw;+document.onfullscreenchange = () => {+ controls();+ resize();+};+new ResizeObserver(resize).observe(player);+window.addEventListener('resize', resize);+controls();+window.explainrooReady = (async () => {+ const result = await boot({ canvas, player: true });+ engine = result.engine;+ duration = result.state.timeline.duration;+ document.title = result.state.title;+ canvas.setAttribute('aria-label', result.state.title);+ poster.alt = `${result.state.title} cover`;+ player.style.aspectRatio = `${engine.W} / ${engine.H}`;+ seekBar.max = duration;+ seekBar.disabled = playButton.disabled = startButton.disabled = false;+ const start = Number(params.get('t'));+ seek(Number.isFinite(start) ? start : 0);+ resize();+ for (const clip of engine.clips.values()) clip.video.onseeked = () => { if (!playing) draw(); };+ if (params.get('autoplay') === '1') await play();+ return engine;+})();+window.explainrooReady.catch(e => { get('status').textContent = `Could not load the player: ${e.message}`; });diff --git a/engine/runtime.js b/engine/runtime.jsindex d7042db..0a636b2 100644--- a/engine/runtime.js+++ b/engine/runtime.js@@ -1,6 +1,6 @@// Boots the engine in a page: loads the project state, fonts, icons, images// and scenes, then exposes frame rendering, checks and the soundtrack.-import rough from '/vendor/rough.js';+import rough from '../vendor/rough.js';import { Stage, SceneError } from './stage.js';import { getTheme, makeBackground, fontString, colorOf } from './themes.js';import { Pen } from './pen.js';@@ -20,8 +20,8 @@ export async function fetchState() {return res.json();}-async function loadFonts(theme, config) {- const manifest = await (await fetch('/fonts/fonts.json')).json();+async function loadFonts(theme, config, root = '/') {+ const manifest = await (await fetch(root + 'fonts/fonts.json')).json();const wanted = new Set(Object.values(theme.fonts).map((f) => f.family));// The UI kit (s.ui) draws app screens in Inter and headlines in Instrument Serif.wanted.add('Inter');@@ -35,22 +35,22 @@ async function loadFonts(theme, config) {throw new Error(`font "${family}" from ${url} could not be loaded`);}));};- for (const f of manifest) if (wanted.has(f.family)) add(f.family, `/fonts/${f.file}`, f);+ for (const f of manifest) if (root === './' || wanted.has(f.family)) add(f.family, `${root}fonts/${f.file}`, f);// A video's own fonts, from "fonts" in video.json.- for (const f of config.fonts || []) add(f.family, '/project/' + f.src.split('/').map(encodeURIComponent).join('/'), f);+ for (const f of config.fonts || []) add(f.family, root + 'project/' + f.src.split('/').map(encodeURIComponent).join('/'), f);await Promise.all(faces);// Make sure every weight used by the theme is ready before the first frame.await Promise.all(Object.values(theme.fonts).map((f) => document.fonts.load(`${f.weight} 40px "${f.family}"`)));}-async function loadImages(assets) {+async function loadImages(assets, root = '/') {const images = new Map();await Promise.all(assets.filter((a) => /\.(png|jpe?g|webp|gif|svg)$/i.test(a)).map(async (a) => {const img = new Image();- img.src = '/project/' + a.split('/').map(encodeURIComponent).join('/');+ img.src = root + 'project/' + a.split('/').map(encodeURIComponent).join('/');await img.decode().catch(() => {throw new Error(`image ${a} could not be decoded`);});@@ -132,6 +132,12 @@ export class Engine {clipImage(clip, frame) {if (this.recorder) return null;+ if (clip.video) {+ const video = clip.video;+ const time = Math.min(frame / clip.fps, Math.max(0, clip.duration - 0.04));+ if (!video.seeking && Math.abs(video.currentTime - time) > 0.04) video.currentTime = time;+ return video.readyState >= 2 ? video : null;+ }const key = this.clipFrames.key(clip, frame);if (this.clipProbe) {this.clipProbe.set(key, { clip, frame });@@ -162,6 +168,7 @@ export class Engine {mathData(tex, display, scene, t) {const key = JSON.stringify([tex, display]);+ if (this.staticMath) return this.staticMath[key] || { error: 'formula missing from the player. Export the project again.' };if (this.collectingMath) {if (!this.mathCache.has(key)) this.mathCache.set(key, null);return null;@@ -523,27 +530,29 @@ export class Engine {}// Boots the engine for render, check or studio pages.-export async function boot({ canvas, scale = 1, transparent = false, version = '' }) {- const state = await fetchState();+export async function boot({ canvas, scale = 1, transparent = false, version = '', player = false }) {+ const root = player ? './' : '/';+ const state = player ? await (await fetch('./state.json')).json() : await fetchState();const theme = getTheme(state.config.theme, state.config.look);const [icons, images, scenesMod, audio, iconTags, clips] = await Promise.all([- fetch('/engine/icons/lucide.json').then((r) => r.json()).then((j) => {+ fetch(root + 'engine/icons/lucide.json').then((r) => r.json()).then((j) => {// Old Lucide names keep working.for (const [alias, target] of Object.entries(j.aliases || {})) if (!j.icons[alias]) j.icons[alias] = j.icons[target];return j.icons;}),- loadImages(state.assets),- import(`/project/scenes.js?v=${encodeURIComponent(version || state.version)}`),- import('/engine/audio/soundtrack.js').catch((e) => ({ SFX: {}, loadError: e })),- fetch('/engine/icons/lucide-tags.json').then((r) => r.json()),- loadClips(state.assets),- loadFonts(theme, state.config),+ loadImages(state.assets, root),+ import(player ? '../project/scenes.js' : `/project/scenes.js?v=${encodeURIComponent(version || state.version)}`),+ player ? { SFX: {} } : import('./audio/soundtrack.js').catch((e) => ({ SFX: {}, loadError: e })),+ player ? {} : fetch('/engine/icons/lucide-tags.json').then((r) => r.json()),+ loadClips(state.assets, player ? state : null),+ loadFonts(theme, state.config, root),]);const scenes = scenesMod.default;if (!scenes || typeof scenes !== 'object') throw new Error('scenes.js must "export default { sceneId(s) { ... } }"');const engine = new Engine({ canvas, state, scenes, icons, images, clips, scale, transparent, sfxNames: Object.keys(audio.SFX) });engine.iconTags = iconTags;- await engine.loadMath();+ if (player) engine.staticMath = await (await fetch('./math.json')).json();+ else await engine.loadMath();return { engine, audio, state };}diff --git a/src/cli.js b/src/cli.jsindex 23996f9..fe82f9a 100644--- a/src/cli.js+++ b/src/cli.js@@ -6,6 +6,7 @@ import { layoutAreas } from '../engine/layout.js';import { ScriptError } from './script.js';import { prepare, makeState } from './pipeline.js';import { render } from './render.js';+import { exportPlayer } from './export.js';import { check, stills, sheet, verify } from './qa.js';import { startServer, ROOT } from './server.js';import { VOICES, LANGUAGES, voiceLanguage, loadTTS, loadASR, cacheRoot, ttsDtype, TTS_SAMPLE_RATE, MULTILINGUAL_ASR_MODEL } from './models.js';@@ -33,6 +34,7 @@ Make a videorender [project] write the video with subtitles, chapters and a cover (--format mp4|gif|webm|mov,--transparent, --size, --draft, --from s, --to s, --workers n, --out file)verify [project] check the video: loudness, black frames, narration (--file)+ export [project] write a web player folder (--out dir, default: out/player/)Product demoscapture [project] <url> save screenshots, wording, colors, fonts and logo of a live site (--mobile)@@ -159,6 +161,12 @@ export async function main(argv) {print([r.out, r.subtitles?.srt, r.subtitles?.vtt, r.chapters, r.cover].filter(Boolean).map((f) => path.relative(process.cwd(), f) || f).join('\n'), r);return 0;}+ case 'export': {+ const project = loadProject(pos[0]);+ const r = await exportPlayer(project, { log, out: textFlag(flags, 'out') });+ print(`${r.out} (${r.duration.toFixed(1)}s)`, r);+ return 0;+ }case 'verify': {const project = loadProject(pos[0]);const r = await verify(project, { file: flags.file, log });diff --git a/src/export.js b/src/export.jsnew file mode 100644index 0000000..09edea1--- /dev/null+++ b/src/export.js@@ -0,0 +1,167 @@+// A static player uses the same frames and mix as a render, without a server.+import fs from 'node:fs';+import path from 'node:path';+import { fileURLToPath } from 'node:url';+import { prepare, makeState } from './pipeline.js';+import { startServer, projectFile, ROOT } from './server.js';+import { launch, openEngine } from './browser.js';+import { ffmpeg, normalizeLoudness } from './ffmpeg.js';+import { resolveTimes } from './qa.js';+import { subtitleCues, toVtt } from './subtitles.js';++export function localModules(dir, entry = 'scenes.js') {+ const files = new Map();+ const visit = rel => {+ if (files.has(rel)) return;+ const file = projectFile(dir, rel);+ if (!file || !/\.m?js$/i.test(file)) throw new Error(`module "${rel}" cannot be exported. Keep modules inside the project, away from dotfiles.`);+ const source = fs.readFileSync(file, 'utf8');+ files.set(rel, file);+ const imports = /\/\/[^\n]*|\/\*[\s\S]*?\*\/|'(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*"|`(?:\\.|[^`\\])*`|\b(?:import|export)\s+(?:[\w\s,*{}$]+\s+from\s*)?['"]([^'"\n]+)['"]|\bimport\s*\(\s*['"]([^'"\n]+)['"]\s*\)/g;+ for (const m of source.matchAll(imports)) {+ const spec = m[1] || m[2];+ if (!spec) continue;+ if (!spec.startsWith('./') && !spec.startsWith('../')) throw new Error(`module "${spec}" cannot be exported. Use a relative local import.`);+ visit(path.posix.normalize(path.posix.join(path.posix.dirname(rel), spec.split(/[?#]/)[0])));+ }+ };+ visit(entry);+ return files;+}++export function copyPlayer(project, timeline, used, out) {+ const copy = (src, rel) => {+ const dest = path.join(out, rel);+ fs.mkdirSync(path.dirname(dest), { recursive: true });+ fs.copyFileSync(src, dest);+ };+ const projectModules = localModules(project.dir);+ for (const rel of used.modules || []) for (const [name, file] of localModules(project.dir, rel)) projectModules.set(name, file);+ for (const [rel, file] of projectModules) copy(file, `project/${rel}`);+ for (const rel of used.assets) {+ const file = projectFile(project.dir, rel);+ if (!file || !rel.startsWith('assets/')) throw new Error(`asset "${rel}" cannot be exported. Keep assets inside assets/, away from dotfiles.`);+ if (/\.svg$/i.test(rel) && /(?:href\s*=\s*['"]|url\(\s*['"]?)(?:\/|[a-z]+:)/i.test(fs.readFileSync(file, 'utf8'))) throw new Error(`image "${rel}" uses an outside file. Use a self-contained image.`);+ copy(file, `project/${rel}`);+ }+ const fonts = JSON.parse(fs.readFileSync(path.join(ROOT, 'fonts/fonts.json'), 'utf8')).filter(f => used.fonts.includes(f.family));+ for (const f of fonts) copy(path.join(ROOT, 'fonts', f.file), `fonts/${f.file}`);+ for (const family of new Set(fonts.map(f => f.family))) {+ const name = family.replaceAll(' ', '') + '-OFL.txt';+ copy(path.join(ROOT, 'fonts/licenses', name), `fonts/licenses/${name}`);+ }+ fs.mkdirSync(path.join(out, 'fonts'), { recursive: true });+ fs.writeFileSync(path.join(out, 'fonts/fonts.json'), JSON.stringify(fonts));+ // Audio synthesis and upload code run during export, never during playback.+ const modules = ['runtime', 'stage', 'themes', 'pen', 'transitions', 'captions', 'util', 'layout', 'clips', 'ui', 'readability', 'pathdata', 'text', 'code'];+ for (const name of modules) copy(path.join(ROOT, 'engine', `${name}.js`), `engine/${name}.js`);+ copy(path.join(ROOT, 'engine/icons/lucide.json'), 'engine/icons/lucide.json');+ copy(path.join(ROOT, 'engine/icons/LICENSE-lucide.txt'), 'engine/icons/LICENSE-lucide.txt');+ const rough = fileURLToPath(import.meta.resolve('roughjs/bundled/rough.esm.js'));+ copy(rough, 'vendor/rough.js');+ copy(path.join(path.dirname(rough), '../LICENSE'), 'vendor/LICENSE-roughjs.txt');+ copy(path.join(ROOT, 'LICENSE'), 'LICENSE');+ copy(path.join(ROOT, 'engine/player.html'), 'index.html');+ copy(path.join(ROOT, 'engine/player.js'), 'engine/player.js');+ const state = makeState(project, timeline);+ state.config = Object.fromEntries(Object.entries(state.config).filter(([key]) => !key.startsWith('_') && key !== 'images'));+ state.config.fonts = state.config.fonts.filter(f => used.assets.includes(f.src));+ if (state.config.brand?.logo && !used.assets.includes(state.config.brand.logo)) {+ state.config.brand = { ...state.config.brand };+ delete state.config.brand.logo;+ }+ // A custom look is already resolved in config.look; its source is private.+ if (state.config.look) state.config.theme = state.config.look.extends;+ if (typeof state.config.voice === 'object' && state.config.voice.provider === 'recording') state.config.voice = { provider: 'recording', src: 'audio.m4a' };+ state.timeline = { ...timeline, scenes: timeline.scenes.map(s => ({ ...s, voice: s.voice ? { ...s.voice, url: 'audio.m4a' } : null })) };+ state.assets = used.assets.filter(a => !/\.(woff2?|ttf|otf)$/i.test(a));+ fs.writeFileSync(path.join(out, 'state.json'), JSON.stringify(state));+ fs.writeFileSync(path.join(out, 'math.json'), JSON.stringify(used.math));+ fs.writeFileSync(path.join(out, 'subtitles.vtt'), toVtt(subtitleCues(timeline)));+}++export async function exportPlayer(project, { out, log = () => {} } = {}) {+ out = path.resolve(out || path.join(project.paths.out, 'player'));+ if (fs.existsSync(out) && fs.readdirSync(out).length && (!fs.existsSync(path.join(out, 'index.html')) || !fs.readFileSync(path.join(out, 'index.html'), 'utf8').includes('<meta name="generator" content="explainroo web player">'))) throw new Error('output folder is not an exported player. Choose an empty folder with --out.');+ localModules(project.dir);+ const { timeline } = await prepare(project, { log });+ const state = makeState(project, timeline);+ state.assets = state.assets.filter(a => projectFile(project.dir, a));+ let browser, server;+ fs.mkdirSync(path.dirname(out), { recursive: true });+ const temp = fs.mkdtempSync(path.join(path.dirname(out), '.player-'));+ try {+ server = await startServer({ projectDir: project.dir, getState: () => state });+ browser = await launch();+ const context = await browser.newContext();+ let outside;+ const projectModules = new Set();+ await context.route('**/*', route => {+ const url = new URL(route.request().url());+ if (url.origin !== server.url) {+ outside = route.request().url();+ return route.abort();+ }+ if (url.pathname.startsWith('/project/') && /\.m?js$/i.test(url.pathname)) projectModules.add(decodeURIComponent(url.pathname.slice(9)));+ return route.continue();+ });+ // openEngine uses browser.newPage, so keep its requests in this context.+ const page = await openEngine(context, server.url);+ const cover = resolveTimes(timeline, [project.config.cover === 'auto' ? `${timeline.scenes[0].id}@end` : project.config.cover])[0].T;+ const used = await page.evaluate(async cover => {+ const { boot } = await import('/engine/runtime.js');+ const { engine } = await boot({ canvas: document.createElement('canvas') });+ const assets = new Set();+ const fonts = new Set();+ for (const method of ['image', 'clip']) {+ const original = engine[method].bind(engine);+ engine[method] = src => {+ const result = original(src);+ const key = src.replace(/^\.?\//, '');+ assets.add(engine[method === 'image' ? 'images' : 'clips'].has(key) ? key : 'assets/' + key);+ return result;+ };+ }+ const proto = CanvasRenderingContext2D.prototype;+ const font = Object.getOwnPropertyDescriptor(proto, 'font');+ Object.defineProperty(proto, 'font', { ...font, set(value) {+ for (const m of value.matchAll(/["']([^"']+)["']/g)) fonts.add(m[1]);+ font.set.call(this, value);+ } });+ try {+ const { renderAudio } = await import('/engine/runtime.js');+ const audio = await import('/engine/audio/soundtrack.js');+ const { upload } = await import('/engine/upload.js');+ const sound = await renderAudio(engine, audio);+ if (sound.errors.length) throw new Error(sound.errors[0].message);+ // Music-free videos still need their scenes sampled for asset usage.+ const collected = engine.collectEvents();+ if (collected.errors.length) throw new Error(collected.errors[0].message);+ for (const sc of engine.timeline.scenes) engine.renderFrame(sc.start + sc.dur - 1 / engine.fps);+ await engine.prepareFrame(cover);+ engine.renderFrame(cover, undefined, { captions: false });+ await upload('build/player-mix.wav', audio.encodeWav(sound.buffer));+ await upload('build/player-poster.jpg', await new Promise(resolve => engine.canvas.toBlob(resolve, 'image/jpeg', 0.9)));+ if ([...engine.mathCache.values()].some(m => m?.error)) throw new Error('a formula could not be typeset. Run check and fix it before exporting.');+ return { assets: [...assets], fonts: [...fonts], math: Object.fromEntries(engine.mathCache) };+ } finally { Object.defineProperty(proto, 'font', font); }+ }, cover);+ if (outside) throw new Error('the project uses an outside URL. Use local files before exporting.');+ used.modules = [...projectModules];+ for (const f of project.config.fonts) if (used.fonts.includes(f.family)) used.assets.push(f.src);+ copyPlayer(project, timeline, used, temp);+ const mix = path.join(project.paths.build, 'player-mix.wav');+ const norm = path.join(project.paths.build, 'player-mix-norm.wav');+ await normalizeLoudness(mix, norm, project.config.loudness);+ await ffmpeg(['-loglevel', 'error', '-i', norm, '-c:a', 'aac', '-b:a', '192k', '-movflags', '+faststart', path.join(temp, 'audio.m4a')]);+ fs.copyFileSync(path.join(project.paths.build, 'player-poster.jpg'), path.join(temp, 'poster.jpg'));+ if (fs.existsSync(out)) fs.rmSync(out, { recursive: true });+ fs.renameSync(temp, out);+ log(`wrote ${out} (${timeline.duration.toFixed(1)}s)`);+ return { out, duration: timeline.duration, width: project.config.width, height: project.config.height };+ } finally {+ if (browser) await browser.close();+ if (server) await server.close();+ fs.rmSync(temp, { recursive: true, force: true });+ }+}diff --git a/src/server.js b/src/server.jsindex dd7ab1f..413d554 100644--- a/src/server.js+++ b/src/server.js@@ -62,7 +62,7 @@ function realInside(base, target) {// The rules apply to where a file really is: a symlink such as// assets/logo.png -> ../../.env is followed and then refused.-function projectFile(projectDir, rel) {+export function projectFile(projectDir, rel) {if (!allowedName(rel)) return null;const file = safeJoin(projectDir, rel);if (!file || !fs.existsSync(file)) return null;diff --git a/test/export-browser.test.js b/test/export-browser.test.jsnew file mode 100644index 0000000..2dcf9ff--- /dev/null+++ b/test/export-browser.test.js@@ -0,0 +1,222 @@+import test from 'node:test';+import assert from 'node:assert/strict';+import fs from 'node:fs';+import os from 'node:os';+import path from 'node:path';+import http from 'node:http';+import { findChrome, launch } from '../src/browser.js';+import { loadProject } from '../src/project.js';+import { exportPlayer } from '../src/export.js';+import { withEngine } from '../src/pipeline.js';+import { ffmpeg, probe } from '../src/ffmpeg.js';+import { ROOT } from '../src/server.js';++let chrome;+try { chrome = findChrome(); } catch {}++async function servePlayer(dir, ranges = true) {+ const types = { '.html': 'text/html', '.js': 'text/javascript', '.mjs': 'text/javascript', '.json': 'application/json', '.vtt': 'text/vtt', '.m4a': 'audio/mp4', '.mp4': 'video/mp4', '.svg': 'image/svg+xml', '.woff2': 'font/woff2' };+ const server = http.createServer((req, res) => {+ const url = new URL(req.url, 'http://localhost');+ const rel = decodeURIComponent(url.pathname).replace(/^\/docs\/video\//, '');+ const file = path.resolve(dir, rel);+ if (!url.pathname.startsWith('/docs/video/') || !file.startsWith(dir + path.sep) || !fs.existsSync(file) || !fs.statSync(file).isFile()) { res.writeHead(404); res.end(); return; }+ const size = fs.statSync(file).size;+ const range = ranges && /^bytes=(\d+)-(\d*)$/.exec(req.headers.range || '');+ const start = range ? Number(range[1]) : 0;+ const end = range?.[2] ? Math.min(size - 1, Number(range[2])) : size - 1;+ res.writeHead(range ? 206 : 200, { 'Content-Type': types[path.extname(file)] || 'application/octet-stream', 'Content-Length': end - start + 1, ...(ranges ? { 'Accept-Ranges': 'bytes' } : {}), ...(range ? { 'Content-Range': `bytes ${start}-${end}/${size}` } : {}) });+ fs.createReadStream(file, { start, end }).pipe(res);+ });+ await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));+ return { url: `http://127.0.0.1:${server.address().port}/docs/video/`, close: () => new Promise(resolve => { server.closeAllConnections(); server.close(resolve); }) };+}++test('exported player runs at a sub-path with matching frames, clips and accessible controls', { skip: !chrome }, async t => {+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'explainroo-player-browser-'));+ t.after(() => fs.rmSync(dir, { recursive: true, force: true }));+ fs.mkdirSync(path.join(dir, 'assets'));+ fs.writeFileSync(path.join(dir, 'assets/used.svg'), '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><circle cx="50" cy="50" r="40" fill="#245cff"/></svg>');+ fs.writeFileSync(path.join(dir, 'assets/unused.svg'), '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"/>');+ fs.writeFileSync(path.join(dir, 'assets/secret.json'), '{"key":"SECRET"}');+ fs.writeFileSync(path.join(dir, '.env'), 'SECRET');+ fs.copyFileSync(path.join(ROOT, 'fonts/Inter-400-latin.woff2'), path.join(dir, 'assets/unused.woff2'));+ await ffmpeg(['-loglevel', 'error', '-f', 'lavfi', '-i', 'color=red:s=160x90:r=30:d=1', '-f', 'lavfi', '-i', 'color=lime:s=160x90:r=30:d=1', '-filter_complex', '[0:v][1:v]concat=n=2:v=1:a=0[v]', '-map', '[v]', '-c:v', 'libx264', '-threads', '1', path.join(dir, 'assets/clip.mp4')]);+ fs.writeFileSync(path.join(dir, 'video.json'), JSON.stringify({ theme: 'clean', music: false, sfx: false, end: 0, fonts: [{ family: 'Unused', src: 'assets/unused.woff2' }] }));+ fs.writeFileSync(path.join(dir, 'script.md'), '# Player fixture\n## intro {min=12}\n');+ fs.writeFileSync(path.join(dir, 'helper.mjs'), "export const label = 'Player fixture';");+ fs.writeFileSync(path.join(dir, 'scenes.js'), `const modulePath = './helper.mjs';+ const { label } = await import(modulePath);+ export default { intro(s) {+ s.title(label, { y: 220, enter: 'none' });+ s.math('E=mc^2', { y: 440, enter: 'none' });+ s.image('assets/used.svg', { x: 1600, y: 440, w: 160, enter: 'none' });+ s.clip('assets/clip.mp4', { y: 760, w: 480, at: 2, out: 4, enter: 'none', shadow: false, radius: 0 });+ } };`);+ const project = loadProject(dir);+ const output = await exportPlayer(project);+ assert.equal(output.out, path.join(dir, 'out/player'));+ for (const name of ['audio.m4a', 'poster.jpg', 'subtitles.vtt']) assert.ok(fs.existsSync(path.join(output.out, name)));+ assert.ok(!fs.existsSync(path.join(output.out, 'project/build')));+ assert.ok(fs.existsSync(path.join(output.out, 'project/helper.mjs')));+ assert.deepEqual(fs.readdirSync(path.join(output.out, 'project/assets')).sort(), ['clip.mp4', 'used.svg']);+ const exportedState = JSON.parse(fs.readFileSync(path.join(output.out, 'state.json'), 'utf8'));+ assert.deepEqual(exportedState.config.fonts, []);+ const audioInfo = await probe(path.join(output.out, 'audio.m4a'));+ assert.equal(audioInfo.streams[0].codec_name, 'aac');+ assert.ok(Math.abs(Number(audioInfo.format.duration) - 12) < 0.1);+ // A cue exercises the browser's native TextTrack parsing and the HTML overlay.+ fs.writeFileSync(path.join(output.out, 'subtitles.vtt'), 'WEBVTT\n\n00:00:00.000 --> 00:00:10.000\nReadable & accessible\n');+ const timeline = JSON.parse(fs.readFileSync(path.join(project.paths.build, 'timeline.json'), 'utf8'));+ let reference;+ await withEngine(project, timeline, {}, async page => {+ await page.evaluate(() => window.explainroo.stills([{ T: 0, path: 'out/reference.png' }]));+ reference = fs.readFileSync(path.join(project.paths.out, 'reference.png')).toString('base64');+ });+ const server = await servePlayer(output.out);+ t.after(() => server.close());+ const browser = await launch();+ t.after(() => browser.close());+ const context = await browser.newContext({ viewport: { width: 1920, height: 1080 } });+ const page = await context.newPage();+ const errors = [];+ const requests = [];+ page.on('pageerror', e => errors.push(e.message));+ page.on('request', req => requests.push(req.url()));+ await page.goto(server.url + 'index.html?muted=1');+ await page.evaluate(() => window.explainrooReady);+ assert.equal(await page.locator('#poster').isVisible(), true);+ assert.equal(await page.locator('#audio').evaluate(a => a.paused), true);+ assert.ok(!requests.some(url => url.endsWith('/audio.m4a')));+ assert.equal(await page.locator('#cc').getAttribute('aria-pressed'), 'false');+ assert.equal(await page.getByRole('button', { name: 'Play', exact: true }).count(), 1);+ assert.equal(await page.locator('#controls').isVisible(), false);+ assert.equal(await page.locator('#start').isVisible(), true);+ for (const [id, label] of [['mute', 'Unmute'], ['cc', 'Show captions'], ['fullscreen', 'Enter fullscreen']]) assert.equal(await page.locator(`#${id}`).getAttribute('aria-label'), label);+ assert.equal(await page.locator('#seek').getAttribute('aria-label'), 'Seek');+ const icons = JSON.parse(fs.readFileSync(path.join(ROOT, 'engine/icons/lucide.json'), 'utf8')).icons;+ for (const [id, name] of [['play', 'play'], ['mute', 'volume-x'], ['cc', 'closed-caption'], ['fullscreen', 'maximize']]) {+ assert.deepEqual(await page.locator(`#${id} path`).evaluateAll(paths => paths.map(path => path.getAttribute('d'))), icons[name]);+ assert.equal(await page.locator(`#${id}`).getAttribute('title'), await page.locator(`#${id}`).getAttribute('aria-label'));+ }+ await page.locator('#poster').click({ position: { x: 40, y: 40 } });+ await page.waitForFunction(() => Number(document.querySelector('#seek').value) > 0.15);+ assert.ok(await page.locator('#audio').evaluate(a => a.currentTime > 0.1 && !a.paused));+ await page.keyboard.press('k');+ assert.equal(await page.getByRole('button', { name: 'Play', exact: true }).count(), 1);+ assert.equal(await page.locator('#poster').isVisible(), false);+ assert.equal(await page.locator('#start').isVisible(), false);+ assert.equal(await page.locator('#controls').isVisible(), true);+ for (const label of ['Play', 'Unmute', 'Show captions', 'Enter fullscreen']) assert.equal(await page.getByRole('button', { name: label, exact: true }).count(), 1);+ assert.equal(await page.getByRole('slider', { name: 'Seek' }).count(), 1);+ await page.locator('#seek').evaluate(input => { input.value = 0; input.dispatchEvent(new Event('input')); });+ const difference = await page.evaluate(async base64 => {+ const image = new Image(); image.src = 'data:image/png;base64,' + base64; await image.decode();+ const canvas = document.querySelector('#c');+ const other = document.createElement('canvas'); other.width = canvas.width; other.height = canvas.height;+ const ctx = other.getContext('2d'); ctx.drawImage(image, 0, 0);+ const a = canvas.getContext('2d').getImageData(0, 0, canvas.width, canvas.height).data;+ const b = ctx.getImageData(0, 0, canvas.width, canvas.height).data;+ let error = 0; for (let i = 0; i < a.length; i++) error += Math.abs(a[i] - b[i]);+ return error / a.length;+ }, reference);+ assert.ok(difference < 0.2, `mean pixel error ${difference}`);+ await page.locator('#player').focus();+ await page.keyboard.press('ArrowRight');+ assert.equal(Number(await page.locator('#seek').inputValue()), 5);+ await page.keyboard.press('ArrowLeft');+ assert.equal(Number(await page.locator('#seek').inputValue()), 0);+ await page.keyboard.press('m');+ assert.equal(await page.locator('#audio').evaluate(a => a.muted), false);+ assert.equal(await page.locator('#mute').getAttribute('title'), 'Mute');+ assert.equal(await page.locator('#mute').getAttribute('data-icon'), 'volume-2');+ await page.keyboard.press('m');+ await page.keyboard.press('Space');+ await page.waitForFunction(() => document.querySelector('#play').getAttribute('aria-label') === 'Pause');+ assert.equal(await page.locator('#play').getAttribute('title'), 'Pause');+ assert.equal(await page.locator('#play').getAttribute('data-icon'), 'pause');+ await page.waitForFunction(() => getComputedStyle(document.querySelector('#controls')).opacity === '0');+ await page.mouse.move(20, 20);+ await page.waitForFunction(() => getComputedStyle(document.querySelector('#controls')).opacity === '1');+ await page.locator('#play').focus();+ await page.waitForTimeout(2200);+ assert.equal(await page.locator('#controls').evaluate(el => getComputedStyle(el).opacity), '1');+ assert.equal(await page.locator('#play').evaluate(el => getComputedStyle(el).outlineStyle), 'solid');+ await page.locator('#player').focus();+ await page.keyboard.press('Space');+ await page.locator('#cc').click();+ await page.waitForFunction(() => document.querySelector('#captions').textContent === 'Readable & accessible');+ assert.equal(await page.locator('#cc').getAttribute('aria-pressed'), 'true');+ assert.equal(await page.locator('#cc').getAttribute('title'), 'Hide captions');+ assert.notEqual(await page.locator('#cc').evaluate(el => getComputedStyle(el, '::after').backgroundColor), 'rgba(0, 0, 0, 0)');+ await page.locator('#seek').evaluate(input => { input.value = 3.5; input.dispatchEvent(new Event('input')); });+ await page.waitForFunction(() => {+ const c = document.querySelector('#c'); const p = c.getContext('2d').getImageData(c.width / 2, c.height * 760 / 1080, 1, 1).data;+ return p[1] > 240 && p[0] < 15;+ });+ await page.locator('#seek').evaluate(input => { input.value = 2.5; input.dispatchEvent(new Event('input')); });+ await page.waitForFunction(() => {+ const c = document.querySelector('#c'); const p = c.getContext('2d').getImageData(c.width / 2, c.height * 760 / 1080, 1, 1).data;+ return p[0] > 240 && p[1] < 15;+ });+ await page.locator('#player').focus();+ await page.keyboard.press('f');+ await page.waitForFunction(() => !!document.fullscreenElement);+ assert.equal(await page.getByRole('button', { name: 'Exit fullscreen' }).count(), 1);+ assert.equal(await page.locator('#fullscreen').getAttribute('title'), 'Exit fullscreen');+ assert.equal(await page.locator('#fullscreen').getAttribute('data-icon'), 'minimize');+ await page.keyboard.press('k');+ await page.waitForFunction(() => getComputedStyle(document.querySelector('#controls')).opacity === '0');+ assert.equal(await page.locator('#player').evaluate(el => getComputedStyle(el).cursor), 'none');+ await page.locator('#player').dispatchEvent('pointerdown', { pointerType: 'touch' });+ await page.waitForFunction(() => getComputedStyle(document.querySelector('#controls')).opacity === '1');+ assert.notEqual(await page.locator('#player').evaluate(el => getComputedStyle(el).cursor), 'none');+ await page.keyboard.press('k');+ await page.keyboard.press('f');+ await page.waitForFunction(() => !document.fullscreenElement);+ await page.setViewportSize({ width: 480, height: 320 });+ await page.waitForFunction(() => document.querySelector('#c').width === 480);+ assert.equal(await page.locator('#player').evaluate(el => Math.round(el.getBoundingClientRect().height)), 270);+ await page.setViewportSize({ width: 320, height: 240 });+ const layout = await page.locator('#controls').evaluate(el => {+ const bounds = el.getBoundingClientRect();+ return [...el.querySelectorAll('button,input,#time')].map(child => {+ const box = child.getBoundingClientRect();+ return { inside: box.left >= bounds.left && box.right <= bounds.right, target: child.tagName === 'SPAN' || (box.width >= 40 && box.height >= 40) };+ });+ });+ assert.ok(layout.every(el => el.inside && el.target));+ await page.emulateMedia({ reducedMotion: 'reduce' });+ assert.equal(await page.locator('#controls').evaluate(el => getComputedStyle(el).transitionDuration), '0s');+ await page.reload();+ await page.evaluate(() => window.explainrooReady);+ assert.equal(await page.locator('#cc').getAttribute('aria-pressed'), 'true');+ assert.equal(await page.locator('#poster').isVisible(), true);+ await page.keyboard.press('k');+ await page.waitForFunction(() => document.querySelector('#play').getAttribute('aria-label') === 'Pause');+ await page.keyboard.press('k');+ await page.goto(server.url + 'index.html?autoplay=1&muted=1&t=6.5');+ await page.evaluate(() => window.explainrooReady);+ await page.waitForFunction(() => Number(document.querySelector('#seek').value) > 6.6);+ assert.equal(await page.locator('#audio').evaluate(a => a.muted), true);+ assert.deepEqual(errors, []);+ assert.ok(requests.every(url => url.startsWith(server.url)), requests.join('\n'));+ assert.ok(!requests.some(url => /__math|__clip|__state|__upload|build\/clips/.test(url)));+ await page.route('**/audio.m4a', route => route.abort());+ await page.goto(server.url + 'index.html?autoplay=1&muted=1&t=6.5');+ await page.evaluate(() => window.explainrooReady);+ await page.waitForFunction(() => Number(document.querySelector('#seek').value) > 6.7);+ assert.ok(await page.locator('#audio').evaluate(a => !!a.error));+ await page.unroute('**/audio.m4a');+ const basic = await servePlayer(output.out, false);+ t.after(() => basic.close());+ await page.goto(basic.url + 'index.html?muted=1&t=3.5');+ await page.evaluate(() => window.explainrooReady);+ assert.ok(await page.locator('video').evaluate(v => v.src.startsWith('blob:')));+ await page.locator('#start').click();+ await page.waitForFunction(() => document.querySelector('#audio').src.startsWith('blob:') && document.querySelector('#audio').currentTime > 3.5);+ await page.keyboard.press('k');+ await page.locator('#seek').evaluate(input => { input.value = 2.5; input.dispatchEvent(new Event('input')); });+ await page.waitForFunction(() => Math.abs(document.querySelector('video').currentTime - 0.5) < 0.05);+ await page.waitForFunction(() => Math.abs(document.querySelector('#audio').currentTime - 2.5) < 0.05);+});diff --git a/test/export.test.js b/test/export.test.jsnew file mode 100644index 0000000..2211d7a--- /dev/null+++ b/test/export.test.js@@ -0,0 +1,85 @@+import test from 'node:test';+import assert from 'node:assert/strict';+import fs from 'node:fs';+import os from 'node:os';+import path from 'node:path';+import { copyPlayer, localModules, exportPlayer } from '../src/export.js';+import { loadProject } from '../src/project.js';+import { buildTimeline } from '../src/timeline.js';++function fixture(t) {+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'explainroo-export-'));+ t.after(() => fs.rmSync(dir, { recursive: true, force: true }));+ fs.mkdirSync(path.join(dir, 'assets'));+ fs.mkdirSync(path.join(dir, 'lib'));+ fs.writeFileSync(path.join(dir, 'video.json'), JSON.stringify({ theme: 'clean', _private: 'SECRET', images: { style: 'PRIVATE NOTES' }, fonts: [{ family: 'Own', src: 'assets/own.woff2' }, { family: 'Unused', src: 'assets/unused.woff2' }] }));+ fs.writeFileSync(path.join(dir, 'script.md'), '## intro {min=3}\n');+ fs.writeFileSync(path.join(dir, 'scenes.js'), "import { draw } from './lib/draw.mjs'; export default { intro: draw };");+ fs.writeFileSync(path.join(dir, 'lib/draw.mjs'), "export { draw } from '../helper.js';");+ fs.writeFileSync(path.join(dir, 'helper.js'), 'export function draw(s) { s.title("Hello"); }');+ for (const name of ['used.svg', 'unused.svg', 'own.woff2', 'unused.woff2', '.env', 'secret.json', 'notes.txt', 'notes.md']) fs.writeFileSync(path.join(dir, 'assets', name), name.includes('svg') ? '<svg xmlns="http://www.w3.org/2000/svg"/>' : 'SECRET');+ fs.writeFileSync(path.join(dir, '.env'), 'SECRET');+ fs.mkdirSync(path.join(dir, 'build/clips'), { recursive: true });+ fs.writeFileSync(path.join(dir, 'build/clips/frame.jpg'), 'extracted');+ const project = loadProject(dir);+ return { dir, project, timeline: buildTimeline(project, {}) };+}++function files(dir) { return fs.readdirSync(dir, { recursive: true }).filter(f => fs.statSync(path.join(dir, f)).isFile()).sort(); }++test('player copies only used assets, font families and local modules, with public state', t => {+ const { dir, project, timeline } = fixture(t);+ const out = path.join(dir, 'player');+ const math = { '["x",true]': { paths: ['M0 0'], viewBox: [0, 0, 1, 1] } };+ copyPlayer(project, timeline, { assets: ['assets/used.svg', 'assets/own.woff2'], fonts: ['Inter', 'Own'], math }, out);+ const names = files(out);+ for (const name of ['index.html', 'engine/player.js', 'engine/runtime.js', 'vendor/rough.js', 'engine/icons/lucide.json', 'project/scenes.js', 'project/lib/draw.mjs', 'project/helper.js', 'project/assets/used.svg', 'project/assets/own.woff2', 'state.json', 'math.json', 'subtitles.vtt']) assert.ok(names.includes(name), name);+ assert.deepEqual(names.filter(n => n.startsWith('project/assets/')), ['project/assets/own.woff2', 'project/assets/used.svg']);+ assert.ok(!names.some(n => /build|\.env|secret|notes|unused|soundtrack|upload/.test(n)));+ const manifest = JSON.parse(fs.readFileSync(path.join(out, 'fonts/fonts.json'), 'utf8'));+ assert.deepEqual([...new Set(manifest.map(f => f.family))], ['Inter']);+ assert.deepEqual(names.filter(n => n.startsWith('fonts/') && n.endsWith('.woff2')).sort(), [...new Set(manifest.map(f => 'fonts/' + f.file))].sort());+ const state = JSON.parse(fs.readFileSync(path.join(out, 'state.json'), 'utf8'));+ assert.deepEqual(Object.keys(state).sort(), ['assets', 'config', 'timeline', 'title', 'version']);+ assert.deepEqual(state.assets, ['assets/used.svg']);+ assert.deepEqual(state.config.fonts.map(f => f.family), ['Own']);+ assert.deepEqual(state.timeline, timeline);+ assert.match(state.version, /^[a-f\d]{12}$/);+ assert.ok(!JSON.stringify(state).includes('SECRET'));+ assert.ok(!JSON.stringify(state).includes('PRIVATE NOTES'));+ assert.deepEqual(JSON.parse(fs.readFileSync(path.join(out, 'math.json'), 'utf8')), math);+ const html = fs.readFileSync(path.join(out, 'index.html'), 'utf8');+ for (const [, url] of html.matchAll(/(?:src|href)="([^"]+)"/g)) assert.ok(url.startsWith('./'), url);+ assert.ok(!html.includes('http:') && !html.includes('https:'));+});++test('export follows local imports, refusing private files, outside symlinks and remote modules', t => {+ const { dir, project, timeline } = fixture(t);+ assert.deepEqual([...localModules(dir).keys()], ['scenes.js', 'lib/draw.mjs', 'helper.js']);+ fs.writeFileSync(path.join(dir, 'scenes.js'), `// import 'https://example.com/no.js';+ const label = "import './missing.js'";+ /* export { draw } from './private.js'; */+ import('./lib/draw.mjs?v=1'); export default {};`);+ assert.deepEqual([...localModules(dir).keys()], ['scenes.js', 'lib/draw.mjs', 'helper.js']);+ const out = path.join(dir, 'player');+ for (const rel of ['assets/.env', 'assets/secret.json', 'assets/notes.txt', '../outside.png']) assert.throws(() => copyPlayer(project, timeline, { assets: [rel], fonts: [], math: {} }, out), /cannot be exported/);+ fs.symlinkSync(path.join(dir, '.env'), path.join(dir, 'assets/fake.png'));+ assert.throws(() => copyPlayer(project, timeline, { assets: ['assets/fake.png'], fonts: [], math: {} }, out), /cannot be exported/);+ fs.symlinkSync(path.join(os.tmpdir(), 'outside.js'), path.join(dir, 'outside.js'));+ fs.writeFileSync(path.join(dir, 'scenes.js'), "import 'https://example.com/scenes.js';");+ assert.throws(() => localModules(dir), /relative local import/);+ fs.writeFileSync(path.join(dir, 'scenes.js'), "import '/absolute/scenes.js';");+ assert.throws(() => localModules(dir), /relative local import/);+ fs.writeFileSync(path.join(dir, 'scenes.js'), "import('./.private.js');");+ assert.throws(() => localModules(dir), /cannot be exported/);+});++test('export leaves a non-player output folder alone', async t => {+ const { dir, project } = fixture(t);+ const out = path.join(dir, 'keep');+ fs.mkdirSync(out);+ fs.writeFileSync(path.join(out, 'notes.txt'), 'Keep me');+ await assert.rejects(exportPlayer(project, { out }), /Choose an empty folder/);+ assert.equal(fs.readFileSync(path.join(out, 'notes.txt'), 'utf8'), 'Keep me');+ assert.equal(fs.existsSync(path.join(dir, 'build/voice')), false);+});