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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>explainroo frame</title>
<style>html,body{margin:0;background:#000}canvas{display:block}</style>
</head>
<body>
<canvas id="c"></canvas>
<script type="module">
import { boot, renderAudio } from '/engine/runtime.js';
import { upload } from '/engine/upload.js';
const canvas = document.getElementById('c');
const params = new URLSearchParams(location.search);
const scale = Number(params.get('scale') || 1);
const transparent = params.get('transparent') === '1';
function describe(e) {
const where = e.scene ? ` (scene "${e.scene}" at ${Number(e.sceneTime).toFixed(2)}s)` : '';
return (e.message || String(e)) + where;
}
window.explainrooReady = (async () => {
const { engine, audio, state } = await boot({ canvas, scale, transparent });
const fps = state.config.fps;
const sub = document.createElement('canvas');
sub.width = canvas.width;
sub.height = canvas.height;
const subCtx = sub.getContext('2d', { alpha: false });
// Draws the frame `samples` times over half a frame's time around T and
// averages them, like a film camera's shutter. The samples stay inside
// the frame's scene, so a cut never blends with the scene before it. Each
// sample loads its own clip frames, so the result does not depend on what
// was drawn before.
const blurFrame = async (T, samples) => {
const ctx = canvas.getContext('2d');
const sc = state.timeline.scenes[engine.sceneAt(T)];
const lo = sc.start;
const hi = Math.min(sc.start + sc.dur, state.timeline.duration) - 1e-4;
for (let k = 0; k < samples; k++) {
const t = Math.min(hi, Math.max(lo, T + ((k + 0.5) / samples - 0.5) * (0.5 / fps)));
if (engine.clips.size) await engine.prepareFrame(t, subCtx);
engine.renderFrame(t, subCtx);
ctx.save();
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.globalAlpha = k === 0 ? 1 : 1 / (k + 1);
ctx.drawImage(sub, 0, 0);
ctx.restore();
}
};
window.explainroo = {
info() {
return { duration: state.timeline.duration, frames: state.timeline.frames, fps, width: canvas.width, height: canvas.height, scenes: state.timeline.scenes.map((s) => ({ id: s.id, start: s.start, dur: s.dur })) };
},
// The automatic camera's plan for each scene it films.
cameras() {
return Object.fromEntries([...engine.cameraPlans].map(([i, p]) => [state.timeline.scenes[i].id, { start: p.start, moves: p.moves.map(({ start, dur, from, to }) => ({ start, dur, from, to })), elements: p.elements, view: p.view }]));
},
// samples > 1 adds motion blur: a frame in which something moves is
// drawn that many times across half a frame's time and averaged, like a
// film camera's shutter. Still frames are drawn once.
async frames(list, quality, samples = 1) {
const out = [];
for (const f of list) {
try {
if (engine.clips.size) await engine.prepareFrame(f / fps);
engine.moving = false;
engine.renderFrame(f / fps);
if (samples > 1 && engine.moving && !transparent) await blurFrame(f / fps, samples);
} catch (e) {
throw new Error(describe(e));
}
out.push(transparent ? canvas.toDataURL('image/png').slice(22) : canvas.toDataURL('image/jpeg', quality).slice(23));
}
return out;
},
async stills(list, { type = 'image/png', quality, captions = true } = {}) {
for (const { T, path } of list) {
try {
if (engine.clips.size) await engine.prepareFrame(T);
engine.renderFrame(T, undefined, { captions });
} catch (e) {
throw new Error(describe(e));
}
const blob = await new Promise((r) => canvas.toBlob(r, type, quality));
await upload(path, blob);
}
return list.length;
},
async sheet({ times, labels, cols, thumb, path }) {
const tw = thumb;
const th = Math.round((tw * canvas.height) / canvas.width);
const labelH = 30;
const rows = Math.ceil(times.length / cols);
const sheet = document.createElement('canvas');
sheet.width = cols * (tw + 8) + 8;
sheet.height = rows * (th + labelH + 8) + 8;
const g = sheet.getContext('2d');
g.fillStyle = '#16181d';
g.fillRect(0, 0, sheet.width, sheet.height);
g.font = '600 17px Inter, sans-serif';
g.textBaseline = 'middle';
for (let i = 0; i < times.length; i++) {
try {
if (engine.clips.size) await engine.prepareFrame(times[i]);
engine.renderFrame(times[i]);
} catch (e) {
throw new Error(describe(e));
}
const x = 8 + (i % cols) * (tw + 8);
const y = 8 + Math.floor(i / cols) * (th + labelH + 8);
g.drawImage(canvas, x, y, tw, th);
g.fillStyle = '#e6e9ef';
g.fillText(labels[i], x + 4, y + th + labelH / 2);
}
const blob = await new Promise((r) => sheet.toBlob(r, 'image/jpeg', 0.88));
await upload(path, blob);
return { width: sheet.width, height: sheet.height };
},
check(step, viewWidth) {
return engine.check(step, viewWidth);
},
async soundtrack(path) {
const { buffer, cues, errors } = await renderAudio(engine, audio);
if (errors.length) throw new Error(errors.map((e) => `scene "${e.scene}" at ${e.t.toFixed(2)}s: ${e.message}`).join('\n'));
await upload(path, audio.encodeWav(buffer));
return { cues: cues.length, duration: buffer.duration };
},
};
return true;
})();
</script>
</body>
</html>