storytellurx/scripts/product-demo/frame-schedule.ts

18 lines
855 B
TypeScript

/** Sample the browser clock at a fixed cadence instead of accumulating rounded JPEG durations. */
export function recordingFrameManifest(
frames: readonly { file: string; timestamp: number }[],
finished: number,
fps: number,
) {
const first = frames[0];
if (!first || finished <= first.timestamp) throw new Error("No recording interval captured");
const frameCount = Math.ceil(((finished - first.timestamp) / 1000) * fps);
const entries: string[] = [];
let source = 0;
for (let frame = 0; frame < frameCount; frame++) {
const timestamp = first.timestamp + (frame * 1000) / fps;
while (source + 1 < frames.length && frames[source + 1]!.timestamp <= timestamp) source++;
entries.push(`file '${frames[source]!.file}'\noption framerate ${fps}\nduration ${1 / fps}\n`);
}
return { manifest: entries.join(""), frameCount };
}