The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.
TypeScript ai-videocomfyuicreative-toolsgenerative-aiideogramimage-generationimage-to-3dimage-to-videoltx-videominimax-h3nextjsopen-sourceopen-weightsqwen-imagetext-to-imagevideo-generationwan
N7-5: the scene engine flies a path and records it frame-exact (WebCodecs VP8 to WebM)
Claude73b103049c56
diff --git a/src/components/cinema/scene-engine.ts b/src/components/cinema/scene-engine.tsindex 6acbbf26..798008f7 100644--- a/src/components/cinema/scene-engine.ts+++ b/src/components/cinema/scene-engine.ts@@ -3,6 +3,8 @@import * as THREE from "three";import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";import { FullScreenQuad } from "three/examples/jsm/postprocessing/Pass.js";+import { flyPoseOf, type FramePoses } from "@/lib/camera-path";+import { muxWebM, type EncodedFrame } from "@/lib/webm";import {LENS,blurScale,@@ -28,8 +30,9 @@ import type { SceneSource } from "./scene-actions";/*** The 3D scene viewer's renderer (three.js, loaded only when a scene opens): a MoGe mesh (GLB) or a point cloud- * built from the still and its depth map, a fly camera, the frame-fitted lens, a depth-of-field pass, and the- * capture of exactly what the frame shows. The maths is in src/lib/scene3d.ts; this file only draws.+ * built from the still and its depth map, a fly camera, the frame-fitted lens, a depth-of-field pass, the capture+ * of exactly what the frame shows, and (Fly path, N7-5) a preview flight and a recording of the scene along a camera+ * path. The maths is in src/lib/scene3d.ts and src/lib/camera-path.ts; this file only draws.*/export type { SceneSource };@@ -40,8 +43,11 @@ export interface SceneInfo {layout: "scene" | "object";/** The lens (mm) that shows the photo's own view from the start. */photoLens: number;- /** The subject distance: the orbit pivot for the re-shot angles. */+ /** The subject distance: the orbit pivot for the re-shot angles and camera paths. */pivotDepth: number;+ /** The photo's own field of view (degrees) and shape: what a path render starts from. */+ photoFov: { h: number; v: number };+ photoAspect: number;/** Points, or triangles, drawn. */size: number;}@@ -99,6 +105,7 @@ uniform float blur;uniform float framePx;uniform float maxRadius;uniform float holeRadius;+uniform float holeAlpha;varying vec2 vUv;const float EMPTY = 0.99999;float rawAt(vec2 uv) { return texture2D(tDepth, uv).x; }@@ -149,7 +156,8 @@ void main() {}base = acc / wsum;}- gl_FragColor = vec4(toSRGB(base), 1.0);+ // What nothing covers: the background colour on screen, transparent black in a path render (Uni3C's holes).+ gl_FragColor = raw >= EMPTY ? vec4(toSRGB(base) * holeAlpha, holeAlpha) : vec4(toSRGB(base), 1.0);}`;const VOID = 0x0b0b0c;@@ -195,6 +203,100 @@ function sampleVertices(root: THREE.Object3D, max = 80_000): Float32Array {return Float32Array.from(out);}+/** The type MediaRecorder records a path in when WebCodecs can't: MP4 (H.264) where it writes it, else WebM. Null: none. */+export function pathVideoType(): string | null {+ if (typeof MediaRecorder === "undefined") return null;+ return ["video/mp4;codecs=avc1.42E01E", "video/mp4", "video/webm;codecs=vp9", "video/webm;codecs=vp8", "video/webm"].find((t) => MediaRecorder.isTypeSupported(t)) ?? null;+}++const VP8 = (width: number, height: number, fps: number): VideoEncoderConfig => ({ codec: "vp8", width, height, bitrate: 8_000_000, framerate: fps });++/** WebCodecs can encode VP8 at this size here. */+async function vp8Supported(width: number, height: number, fps: number): Promise<boolean> {+ if (typeof VideoEncoder === "undefined" || typeof VideoFrame === "undefined") return false;+ try {+ return !!(await VideoEncoder.isConfigSupported(VP8(width, height, fps))).supported;+ } catch {+ return false;+ }+}++/**+ * `count` frames of two streams (`draw(i)` gives frame i of each) encoded with WebCodecs and written as two WebM files:+ * every frame kept, frame i at i / fps, a keyframe each second.+ */+async function encodeVp8(count: number, width: number, height: number, fps: number, draw: (i: number) => [ImageData, ImageData]): Promise<[Uint8Array, Uint8Array]> {+ const chunks: EncodedFrame[][] = [[], []];+ let failure: unknown;+ const encoders = [0, 1].map((k) => {+ const e = new VideoEncoder({+ output: (chunk) => {+ const data = new Uint8Array(chunk.byteLength);+ chunk.copyTo(data);+ chunks[k].push({ data, key: chunk.type === "key" });+ },+ error: (err) => (failure = err),+ });+ e.configure(VP8(width, height, fps));+ return e;+ });+ try {+ const step = 1e6 / fps;+ for (let i = 0; i < count; i++) {+ const images = draw(i);+ images.forEach((img, k) => {+ const f = new VideoFrame(img.data, { format: "RGBA", codedWidth: width, codedHeight: height, timestamp: Math.round(i * step), duration: Math.round(step) });+ encoders[k].encode(f, { keyFrame: i % Math.max(1, Math.round(fps)) === 0 });+ f.close();+ });+ if (failure) throw failure;+ // Let the encoders and the page breathe (the progress label repaints).+ while (encoders.some((e) => e.encodeQueueSize > 4)) await new Promise((r) => setTimeout(r, 5));+ if (i % 4 === 3) await new Promise((r) => setTimeout(r, 0));+ }+ await Promise.all(encoders.map((e) => e.flush()));+ if (failure) throw failure;+ } finally {+ encoders.forEach((e) => e.state !== "closed" && e.close());+ }+ if (chunks.some((c) => c.length !== count)) throw new Error(`The path render lost frames (${chunks[0].length} of ${count})`);+ const track = { codec: "V_VP8" as const, width, height, fps };+ return [muxWebM(track, chunks[0]), muxWebM(track, chunks[1])];+}++/** The fallback: two canvases recorded with MediaRecorder, each frame handed over 1/fps apart. */+async function recordWithMediaRecorder(type: string, count: number, width: number, height: number, fps: number, draw: (i: number) => [ImageData, ImageData]): Promise<[Blob, Blob]> {+ const canvases = [0, 1].map(() => Object.assign(document.createElement("canvas"), { width, height }));+ const ctxs = canvases.map((c) => c.getContext("2d")!);+ const streams = canvases.map((c) => c.captureStream(0));+ const tracks = streams.map((s) => s.getVideoTracks()[0] as MediaStreamTrack & { requestFrame?: () => void });+ const recorders = streams.map((s) => new MediaRecorder(s, { mimeType: type, videoBitsPerSecond: 8_000_000 }));+ const chunks: Blob[][] = [[], []];+ const stopped = recorders.map((r, i) => {+ r.ondataavailable = (e) => e.data.size && chunks[i].push(e.data);+ return new Promise<void>((resolve) => (r.onstop = () => resolve()));+ });+ const wait = (ms: number) => new Promise((r) => setTimeout(r, ms));+ const step = 1000 / Math.max(1, fps);+ try {+ recorders.forEach((r) => r.start());+ await wait(step);+ const t0 = performance.now();+ for (let i = 0; i < count; i++) {+ draw(i).forEach((img, k) => ctxs[k].putImageData(img, 0, 0));+ tracks.forEach((t) => t.requestFrame?.());+ await wait(Math.max(0, t0 + (i + 1) * step - performance.now()));+ }+ await wait(step);+ } finally {+ recorders.forEach((r) => r.state !== "inactive" && r.stop());+ await Promise.all(stopped);+ streams.forEach((s) => s.getTracks().forEach((t) => t.stop()));+ }+ const base = type.split(";")[0];+ return [new Blob(chunks[0], { type: base }), new Blob(chunks[1], { type: base })];+}+export class SceneEngine {info: SceneInfo | null = null;/** Called when the autofocus distance changes (scene units ≈ m). */@@ -222,6 +324,8 @@ export class SceneEngine {private focusTimer: ReturnType<typeof setTimeout> | undefined;private size = { w: 1, h: 1, dpr: 1 };private disposed = false;+ /** A path preview or recording owns the camera. */+ private playing = false;constructor(canvas: HTMLCanvasElement) {this.renderer = new THREE.WebGLRenderer({ canvas, antialias: false, alpha: false, powerPreference: "high-performance" });@@ -245,6 +349,7 @@ export class SceneEngine {framePx: { value: 1 },maxRadius: { value: 0 },holeRadius: { value: 0 },+ holeAlpha: { value: 1 },},});this.post = new FullScreenQuad(this.postMat);@@ -297,7 +402,7 @@ export class SceneEngine {const fov = sceneFov(sample);if (fov) {const aspect = Math.tan((fov.h * Math.PI) / 360) / Math.tan((fov.v * Math.PI) / 360);- info = { kind: "mesh", layout: "scene", photoLens: focalForFov(fov.h, aspect), pivotDepth: medianDepth(sample), size: Math.round(triangles) };+ info = { kind: "mesh", layout: "scene", photoLens: focalForFov(fov.h, aspect), pivotDepth: medianDepth(sample), size: Math.round(triangles), photoFov: fov, photoAspect: aspect };} else {// An object around its centre: move it so the camera starts at the origin, looking at it from the front.const sphere = new THREE.Box3().setFromObject(root).getBoundingSphere(new THREE.Sphere());@@ -306,7 +411,15 @@ export class SceneEngine {root.position.sub(new THREE.Vector3(...start.position));root.updateMatrixWorld(true);sample = sampleVertices(root);- info = { kind: "mesh", layout: "object", photoLens: lens, pivotDepth: start.position[2] - sphere.center.z, size: Math.round(triangles) };+ info = {+ kind: "mesh",+ layout: "object",+ photoLens: lens,+ pivotDepth: start.position[2] - sphere.center.z,+ size: Math.round(triangles),+ photoFov: lensFov(lens, 16 / 9),+ photoAspect: 16 / 9,+ };}this.sample = sample;this.model = root;@@ -326,7 +439,7 @@ export class SceneEngine {points.frustumCulled = false;this.scene.add(points);this.sample = cloud.positions;- info = { kind: "points", layout: "scene", photoLens: LENS.default, pivotDepth: cloud.depth, size: cloud.count };+ info = { kind: "points", layout: "scene", photoLens: LENS.default, pivotDepth: cloud.depth, size: cloud.count, photoFov: lensFov(LENS.default, aspect), photoAspect: aspect };}this.info = info;this.camera.near = Math.max(0.005, info.pivotDepth * 0.01);@@ -399,6 +512,38 @@ export class SceneEngine {return this.pose;}+ /** Put the camera at a pose (a path's keyframe). */+ setPose(pose: FlyPose) {+ this.move(pose);+ }++ /**+ * Fly the camera along a path in real time (`poseAt(0…1)` for `seconds`), then put it back where it was. Flying keys+ * do nothing meanwhile; the signal stops it early.+ */+ async playPath(poseAt: (t: number) => FlyPose, seconds: number, signal?: AbortSignal): Promise<void> {+ const before = this.pose;+ this.playing = true;+ this.keys.clear();+ try {+ await new Promise<void>((resolve) => {+ const t0 = performance.now();+ const tick = () => {+ if (this.disposed || signal?.aborted) return resolve();+ const t = Math.min(1, (performance.now() - t0) / 1000 / Math.max(0.1, seconds));+ this.pose = poseAt(t);+ this.dirty = true;+ if (t >= 1) return resolve();+ requestAnimationFrame(tick);+ };+ requestAnimationFrame(tick);+ });+ } finally {+ this.playing = false;+ if (!this.disposed) this.move(before);+ }+ }+private move(pose: FlyPose) {this.pose = pose;this.dirty = true;@@ -433,7 +578,7 @@ export class SceneEngine {// ---- Drawingprivate step(dt: number) {- if (this.info && this.view && this.keys.size) {+ if (this.info && this.view && this.keys.size && !this.playing) {const input = keysToInput(this.keys);if (input.forward || input.right || input.up) {this.pose = flyStep(this.pose, input, dt, flySpeed(this.info.pivotDepth) * this.view.speed, this.boost);@@ -447,15 +592,18 @@ export class SceneEngine {}}- /** Render into `out` (null = the screen) at w×h px; the frame is frameW×frameH px of it, centred. */- private draw(out: THREE.WebGLRenderTarget | null, w: number, h: number, frameW: number, frameH: number, color = this.target) {+ /**+ * Render into `out` (null = the screen) at w×h px; the frame is frameW×frameH px of it, centred. `shot` renders from+ * another camera instead (a path frame: its pose and vertical field of view, no depth of field, holes transparent).+ */+ private draw(out: THREE.WebGLRenderTarget | null, w: number, h: number, frameW: number, frameH: number, color = this.target, shot?: { pose: FlyPose; vFov: number }) {const v = this.view!;const cam = this.camera;- const frameFov = lensFov(v.lens, v.aspect).v;- cam.fov = canvasFov(frameFov, frameH, h);+ const pose = shot?.pose ?? this.pose;+ cam.fov = shot ? shot.vFov : canvasFov(lensFov(v.lens, v.aspect).v, frameH, h);cam.aspect = w / h;- cam.position.set(...this.pose.position);- cam.rotation.set(this.pose.pitch, this.pose.yaw, 0, "YXZ");+ cam.position.set(...pose.position);+ cam.rotation.set(pose.pitch, pose.yaw, 0, "YXZ");cam.updateProjectionMatrix();cam.updateMatrixWorld();if (this.pointsMat) this.pointsMat.uniforms.scale.value = h / 2 / Math.tan((cam.fov * Math.PI) / 360);@@ -469,7 +617,8 @@ export class SceneEngine {u.cameraNear.value = cam.near;u.cameraFar.value = cam.far;u.focus.value = this.focus;- u.blur.value = v.dof ? blurScale(v.lens, v.aperture, this.focus, v.aspect) : 0;+ u.blur.value = v.dof && !shot ? blurScale(v.lens, v.aperture, this.focus, v.aspect) : 0;+ u.holeAlpha.value = shot ? 0 : 1;u.framePx.value = frameW;u.maxRadius.value = Math.min(0.025 * frameW, 48);// Cracks up to about 1.4% of the frame's width close; wider unseen areas stay empty.@@ -506,6 +655,77 @@ export class SceneEngine {}}+ /**+ * Record the scene along a path (build plan N7-5): every frame of `path` (OpenCV camera-to-world poses and a pixel+ * intrinsic, src/lib/camera-path.ts framePoses) rendered at its size with nothing behind the scene, as two videos at+ * `fps`: `render` (black where nothing was seen: Uni3C's render_video) and `mask` (white = a hole; core's Uni3C node has+ * no mask input, so it is kept for WorldCast). With WebCodecs every frame is encoded (VP8) and written to WebM at its+ * exact time; without it MediaRecorder records the frames handed over 1/fps apart, which may drop one.+ */+ async recordPath(path: FramePoses, o: { fps: number; onProgress?: (p: number) => void; signal?: AbortSignal }): Promise<{ render: Blob; mask: Blob; type: string; frames: number }> {+ if (!this.view || !this.info) throw new Error("The scene isn't ready yet");+ const { width, height, poses, intrinsic } = path;+ const exact = await vp8Supported(width, height, o.fps);+ const type = exact ? "video/webm" : pathVideoType();+ if (!type) throw new Error("This browser can't record video — use the cloud engine, or another browser");+ const vFov = (2 * Math.atan(height / 2 / intrinsic[1][1]) * 180) / Math.PI;+ const color = new THREE.WebGLRenderTarget(width, height, { type: this.target.texture.type, depthTexture: new THREE.DepthTexture(width, height) });+ const out = new THREE.WebGLRenderTarget(width, height);+ const buf = new Uint8Array(width * height * 4);+ const render = new ImageData(width, height);+ const mask = new ImageData(width, height);+ /** Frame i into `render` and `mask`: WebGL reads bottom-up, and a pixel nothing covered is transparent black. */+ const frame = (i: number) => {+ this.draw(out, width, height, width, height, color, { pose: flyPoseOf(poses[i]), vFov });+ this.renderer.readRenderTargetPixels(out, 0, 0, width, height, buf);+ for (let y = 0; y < height; y++) {+ const src = (height - 1 - y) * width * 4;+ const dst = y * width * 4;+ for (let x = 0; x < width * 4; x += 4) {+ const hole = buf[src + x + 3] < 128;+ render.data[dst + x] = hole ? 0 : buf[src + x];+ render.data[dst + x + 1] = hole ? 0 : buf[src + x + 1];+ render.data[dst + x + 2] = hole ? 0 : buf[src + x + 2];+ render.data[dst + x + 3] = 255;+ const m = hole ? 255 : 0;+ mask.data[dst + x] = m;+ mask.data[dst + x + 1] = m;+ mask.data[dst + x + 2] = m;+ mask.data[dst + x + 3] = 255;+ }+ }+ };+ const stop = () => {+ if (o.signal?.aborted || this.disposed) throw new DOMException("Stopped", "AbortError");+ };+ this.playing = true;+ try {+ if (exact) {+ const [a, b] = await encodeVp8(poses.length, width, height, o.fps, (i) => {+ stop();+ frame(i);+ o.onProgress?.((i + 1) / poses.length);+ return [render, mask];+ });+ return { render: new Blob([a as BlobPart], { type }), mask: new Blob([b as BlobPart], { type }), type, frames: poses.length };+ }+ const [r, m] = await recordWithMediaRecorder(type, poses.length, width, height, o.fps, (i) => {+ stop();+ frame(i);+ o.onProgress?.((i + 1) / poses.length);+ return [render, mask];+ });+ return { render: r, mask: m, type: type.split(";")[0], frames: poses.length };+ } finally {+ color.depthTexture?.dispose();+ color.dispose();+ out.dispose();+ this.renderer.setRenderTarget(null);+ this.playing = false;+ this.dirty = true;+ }+ }+dispose() {this.disposed = true;cancelAnimationFrame(this.raf);diff --git a/src/lib/webm.ts b/src/lib/webm.tsnew file mode 100644index 00000000..e31041f6--- /dev/null+++ b/src/lib/webm.ts@@ -0,0 +1,113 @@+/**+ * A minimal WebM writer for one video track of already-encoded frames (WebCodecs VP8 / VP9 chunks): the browser's path+ * render (build plan N7-5) needs every frame it drew, in order, at an exact frame rate, which MediaRecorder doesn't+ * promise (it drops frames it can't keep up with). Matroska / WebM layout as the format specification gives it: an EBML+ * header, then one Segment with Info, Tracks and Clusters of SimpleBlocks (millisecond timecodes). Pure: no DOM.+ */++export interface EncodedFrame {+ data: Uint8Array;+ /** A keyframe (the decoder can start here). */+ key: boolean;+}++export interface WebMTrack {+ /** Matroska codec id: "V_VP8" or "V_VP9". */+ codec: "V_VP8" | "V_VP9";+ width: number;+ height: number;+ fps: number;+}++const enc = new TextEncoder();++/** An EBML variable-length size: the shortest form that holds n (never the all-ones "unknown" value). */+function vsize(n: number): Uint8Array {+ for (let len = 1; len <= 8; len++) {+ if (n < 2 ** (7 * len) - 1) {+ const out = new Uint8Array(len);+ let v = n;+ for (let i = len - 1; i >= 0; i--) {+ out[i] = v & 0xff;+ v = Math.floor(v / 256);+ }+ out[0] |= 0x80 >> (len - 1);+ return out;+ }+ }+ throw new Error("Element too large for EBML");+}++/** An unsigned integer in as few bytes as it needs (at least one). */+function uint(n: number): Uint8Array {+ const bytes: number[] = [];+ let v = Math.max(0, Math.round(n));+ do {+ bytes.unshift(v & 0xff);+ v = Math.floor(v / 256);+ } while (v > 0);+ return Uint8Array.from(bytes);+}++function float64(n: number): Uint8Array {+ const b = new Uint8Array(8);+ new DataView(b.buffer).setFloat64(0, n);+ return b;+}++function concat(parts: Uint8Array[]): Uint8Array {+ const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));+ let at = 0;+ for (const p of parts) {+ out.set(p, at);+ at += p.length;+ }+ return out;+}++/** An element: its id (as written, class bits included), its size and its body. */+function el(id: number, ...body: Uint8Array[]): Uint8Array {+ const data = concat(body);+ return concat([uint(id), vsize(data.length), data]);+}++const u = (id: number, n: number) => el(id, uint(n));+const s = (id: number, text: string) => el(id, enc.encode(text));++/** Frames that start a new cluster: every keyframe, and at least every 30 s (SimpleBlock times are 16-bit offsets). */+const CLUSTER_MS = 30_000;++/**+ * One WebM file of `frames` in order at `track.fps`: frame i is shown at i / fps seconds (rounded to the millisecond)+ * and lasts 1 / fps (the track's DefaultDuration, in nanoseconds), so a reader counts exactly frames.length frames.+ */+export function muxWebM(track: WebMTrack, frames: readonly EncodedFrame[]): Uint8Array {+ const fps = Math.max(1, track.fps);+ const msOf = (i: number) => Math.round((i * 1000) / fps);+ const header = el(0x1a45dfa3, u(0x4286, 1), u(0x42f7, 1), u(0x42f2, 4), u(0x42f3, 8), s(0x4282, "webm"), u(0x4287, 4), u(0x4285, 2));+ const info = el(0x1549a966, u(0x2ad7b1, 1_000_000), s(0x4d80, "Fuobe"), s(0x5741, "Fuobe"), el(0x4489, float64((frames.length * 1000) / fps)));+ const tracks = el(+ 0x1654ae6b,+ el(0xae, u(0xd7, 1), u(0x73c5, 1), u(0x83, 1), u(0x9c, 0), s(0x86, track.codec), u(0x23e383, Math.round(1e9 / fps)), el(0xe0, u(0xb0, track.width), u(0xba, track.height))),+ );+ const clusters: Uint8Array[] = [];+ let blocks: Uint8Array[] = [];+ let start = 0;+ const close = () => {+ if (blocks.length) clusters.push(el(0x1f43b675, u(0xe7, start), ...blocks));+ blocks = [];+ };+ frames.forEach((f, i) => {+ const t = msOf(i);+ if (!blocks.length || (f.key && i > 0) || t - start >= CLUSTER_MS) {+ close();+ start = t;+ }+ const rel = t - start;+ const head = new Uint8Array([0x81, (rel >> 8) & 0xff, rel & 0xff, f.key ? 0x80 : 0x00]);+ blocks.push(el(0xa3, head, f.data));+ });+ close();+ const segment = el(0x18538067, info, tracks, ...clusters);+ return concat([header, segment]);+}diff --git a/tests/webm.test.ts b/tests/webm.test.tsnew file mode 100644index 00000000..94afa95c--- /dev/null+++ b/tests/webm.test.ts@@ -0,0 +1,65 @@+import { execFileSync } from "node:child_process";+import fs from "node:fs";+import os from "node:os";+import path from "node:path";+import { describe, expect, it } from "vitest";+import { muxWebM, type EncodedFrame } from "@/lib/webm";++/** The WebM writer behind the browser's path render (N7-5): real VP8 frames in, a file ffmpeg reads frame for frame. */++const has = (bin: string) => {+ try {+ execFileSync(bin, ["-version"], { stdio: "ignore" });+ return true;+ } catch {+ return false;+ }+};++/** VP8 frames out of an IVF file (32-byte header, then 12-byte frame headers). */+function ivfFrames(buf: Buffer): EncodedFrame[] {+ const frames: EncodedFrame[] = [];+ let at = buf.readUInt16LE(6);+ while (at + 12 <= buf.length) {+ const size = buf.readUInt32LE(at);+ const data = new Uint8Array(buf.subarray(at + 12, at + 12 + size));+ frames.push({ data, key: (data[0] & 1) === 0 });+ at += 12 + size;+ }+ return frames;+}++describe("muxWebM", () => {+ it("writes an EBML header, a webm doc type and one SimpleBlock per frame", () => {+ const frames = Array.from({ length: 5 }, (_, i) => ({ data: new Uint8Array([i, i, i]), key: i === 0 }));+ const out = muxWebM({ codec: "V_VP8", width: 64, height: 32, fps: 16 }, frames);+ expect([...out.subarray(0, 4)]).toEqual([0x1a, 0x45, 0xdf, 0xa3]);+ expect(Buffer.from(out).includes(Buffer.from("webm"))).toBe(true);+ expect(Buffer.from(out).includes(Buffer.from("V_VP8"))).toBe(true);+ // SimpleBlock id 0xA3, size 7 (4-byte block header + 3 bytes), track 1.+ let blocks = 0;+ for (let i = 0; i + 2 < out.length; i++) if (out[i] === 0xa3 && out[i + 1] === 0x87 && out[i + 2] === 0x81) blocks++;+ expect(blocks).toBe(5);+ });++ it.skipIf(!has("ffmpeg") || !has("ffprobe"))("muxes real VP8 frames into a file ffmpeg decodes frame for frame at the given rate", () => {+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "fuobe-webm-"));+ try {+ const ivf = path.join(dir, "in.ivf");+ execFileSync("ffmpeg", ["-v", "error", "-f", "lavfi", "-i", "testsrc=size=96x64:rate=16", "-frames:v", "81", "-c:v", "libvpx", "-g", "30", "-f", "ivf", ivf]);+ const frames = ivfFrames(fs.readFileSync(ivf));+ expect(frames).toHaveLength(81);+ const file = path.join(dir, "out.webm");+ fs.writeFileSync(file, muxWebM({ codec: "V_VP8", width: 96, height: 64, fps: 16 }, frames));+ const probe = JSON.parse(+ execFileSync("ffprobe", ["-v", "error", "-count_frames", "-select_streams", "v:0", "-show_entries", "stream=codec_name,width,height,nb_read_frames,avg_frame_rate:format=duration", "-of", "json", file]).toString(),+ );+ expect(probe.streams[0]).toMatchObject({ codec_name: "vp8", width: 96, height: 64, nb_read_frames: "81" });+ expect(Number(probe.format.duration)).toBeCloseTo(81 / 16, 1);+ const [n, d] = String(probe.streams[0].avg_frame_rate).split("/").map(Number);+ expect(n / d).toBeCloseTo(16, 0);+ } finally {+ fs.rmSync(dir, { recursive: true, force: true });+ }+ });+});