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
Keyframe animation: the timing of each gap (frames, every Nth frame, the joint frame dropped, 24 or 16 fps) and the film's length
Claude90f7c6bb0ae2
diff --git a/src/lib/inbetween.ts b/src/lib/inbetween.tsnew file mode 100644index 00000000..e0be34f8--- /dev/null+++ b/src/lib/inbetween.ts@@ -0,0 +1,224 @@+/**+ * Keyframe animation (in-betweening): keyframes in, the in-betweens generated, one finished animation. Each gap between+ * two neighbouring keyframes is one Wan 2.2 start/end-frame pass with its own timing:+ *+ * - a length in Wan's frame steps (4n + 1 frames, at least 17: fewer and the motion isn't legible);+ * - a speed: keep every Nth frame (×1–×4), so a fast move skips frames the way hand-drawn animation does;+ * - every pose lands on screen: the kept frames are counted back from the gap's last frame (the next keyframe), and the+ * frame each gap starts on (the keyframe the gap before ended on) is dropped, so no joint shows a frame twice;+ * - the playback rate: 24 fps (the passes are made at 16 fps, so they play faster: part of the animation's timing, as in+ * the workflow this follows) or 16 fps (true time).+ *+ * Fuobe Animation runs this on both engines: one graph on your GPU (src/lib/av/inbetween-graph.ts), every speed and both+ * rates; in the cloud one hosted clip per gap, joined, at ×1 and 16 fps (the hosted endpoint makes every frame and+ * plays it as made). Pure, no imports beyond types: the composer, the planner, the provider and the tools share it.+ */++/** Wan 2.2's frame step: a pass is 4n + 1 frames. */+export const GAP_STEP = 4;+/** The shortest pass: 17 frames ("17 frames minimum for legible motion"). */+export const MIN_GAP_FRAMES = 17;+/** The longest pass: Wan 2.2's own 81 frames (5 s at 16 fps). */+export const MAX_GAP_FRAMES = 81;+/** The rate Wan 2.2 makes frames at. */+export const MAKE_FPS = 16;+/** Speeds: keep every Nth frame. */+export const SPEEDS = [1, 2, 3, 4] as const;+export type Speed = (typeof SPEEDS)[number];+/** Playback rates: 24 fps (snappier, as the workflow plays it) or 16 fps (true time). */+export const PLAYBACK_RATES = [24, 16] as const;+export type PlaybackFps = (typeof PLAYBACK_RATES)[number];+export const DEFAULT_PLAYBACK: PlaybackFps = 24;+/** Keyframes: 2 to 10 (Frames mode's own limit). */+export const MIN_KEYS = 2;+export const MAX_KEYS = 10;+/** The workflow's words for a character whose mouth should stay still between poses. */+export const STILL_MOUTH_PROMPT = "mouth does not move";++/** One gap's timing: frames made, every Nth kept, and its own words (added to the film's prompt). */+export interface GapTiming {+ frames: number;+ speed: Speed;+ prompt?: string;+}++export interface InbetweenTiming {+ gaps: GapTiming[];+ fps: PlaybackFps;+}++/** What each engine does with the timing. */+export interface EngineTiming {+ speeds: readonly Speed[];+ rates: readonly PlaybackFps[];+}+export const ENGINE_TIMING: Record<"open" | "cloud", EngineTiming> = {+ open: { speeds: SPEEDS, rates: PLAYBACK_RATES },+ cloud: { speeds: [1], rates: [16] },+};++/** The nearest pass length Wan makes: 4n + 1, from 17 to 81. */+export function snapGapFrames(n: number): number {+ if (!Number.isFinite(n)) return MIN_GAP_FRAMES;+ const k = Math.round((n - 1) / GAP_STEP);+ return Math.min(MAX_GAP_FRAMES, Math.max(MIN_GAP_FRAMES, k * GAP_STEP + 1));+}++export const isSpeed = (n: unknown): n is Speed => typeof n === "number" && (SPEEDS as readonly number[]).includes(n);+export const isPlayback = (n: unknown): n is PlaybackFps => typeof n === "number" && (PLAYBACK_RATES as readonly number[]).includes(n);++/**+ * Which of a gap's frames are kept, as VideoHelperSuite's every-Nth selection takes them (images[skip::every]): counted+ * back from its last frame (the next keyframe) and never its first (the keyframe before, already on screen). The first+ * gap's first frame (keyframe 1) is taken separately (`first`).+ */+export function gapSelection(g: Pick<GapTiming, "frames" | "speed">): { skip: number; every: Speed; kept: number } {+ const last = g.frames - 1;+ const skip = ((last - 1) % g.speed) + 1;+ return { skip, every: g.speed, kept: Math.floor((last - 1) / g.speed) + 1 };+}++/** The frames kept from each gap (the first gap also shows keyframe 1). */+export function keptFrames(t: Pick<InbetweenTiming, "gaps">): number[] {+ return t.gaps.map((g, i) => gapSelection(g).kept + (i === 0 ? 1 : 0));+}++/** The film's frame count: keyframe 1, then every gap's kept frames. */+export function totalFrames(t: Pick<InbetweenTiming, "gaps">): number {+ return t.gaps.length ? keptFrames(t).reduce((a, b) => a + b, 0) : 0;+}++/** Seconds from one keyframe to the next, at the playback rate. */+export function gapSeconds(g: Pick<GapTiming, "frames" | "speed">, fps: number): number {+ return gapSelection(g).kept / fps;+}++/** The film's length (s). */+export function totalSeconds(t: InbetweenTiming): number {+ return totalFrames(t) / t.fps;+}++/** Each keyframe's time in the film (s): keyframe 1 at 0. */+export function keyTimes(t: InbetweenTiming): number[] {+ const out = [0];+ for (const g of t.gaps) out.push(out[out.length - 1] + gapSeconds(g, t.fps));+ return out.map((s) => Math.round(s * 1000) / 1000);+}++/** Frames Wan makes in all (what the run costs, in time on your GPU or in seconds of hosted video). */+export const madeFrames = (t: Pick<InbetweenTiming, "gaps">) => t.gaps.reduce((a, g) => a + g.frames, 0);++/** The gap that fits `seconds` between two keyframes at this speed and rate (the nearest length Wan makes). */+export function gapFor(seconds: number, speed: Speed, fps: number): GapTiming {+ const kept = Math.max(1, Math.round(seconds * fps));+ return { frames: snapGapFrames(kept * speed + 1), speed };+}++/** The workflow's own timing for its five keyframes (37, 17, 17, 45 frames; every 1st, 3rd, 4th, 1st). */+export const WORKFLOW_TIMING: InbetweenTiming = {+ gaps: [+ { frames: 37, speed: 1 },+ { frames: 17, speed: 3 },+ { frames: 17, speed: 4 },+ { frames: 45, speed: 1 },+ ],+ fps: 24,+};++/** A new gap: 33 frames (about 1.3 s at 24 fps). */+export const DEFAULT_GAP: GapTiming = { frames: 33, speed: 1 };++/** Timing for `keys` keyframes: the given gaps kept (fitted to an engine), new ones added, extra ones dropped. */+export function timingFor(keys: number, given: Partial<InbetweenTiming> | undefined, engine: "open" | "cloud" = "open"): InbetweenTiming {+ const can = ENGINE_TIMING[engine];+ const fps = given?.fps && can.rates.includes(given.fps) ? given.fps : can.rates[0];+ const gaps = Array.from({ length: Math.max(0, keys - 1) }, (_, i) => {+ const g = given?.gaps?.[i] ?? DEFAULT_GAP;+ const speed = can.speeds.includes(g.speed) ? g.speed : 1;+ return { frames: snapGapFrames(g.frames), speed, ...(g.prompt?.trim() && { prompt: g.prompt.trim() }) };+ });+ return { gaps, fps };+}++/**+ * Frames mode's times (s, the first at 0) as in-between timing: each gap as long as the time between its keyframes at+ * the playback rate (the nearest length Wan makes). Anything after the last keyframe is left out: the film ends on it.+ */+export function timingFromTimes(times: number[], opts: { fps?: PlaybackFps; speed?: Speed; engine?: "open" | "cloud" } = {}): InbetweenTiming {+ const engine = opts.engine ?? "open";+ const fps = opts.fps && ENGINE_TIMING[engine].rates.includes(opts.fps) ? opts.fps : ENGINE_TIMING[engine].rates[0];+ const speed = opts.speed && ENGINE_TIMING[engine].speeds.includes(opts.speed) ? opts.speed : 1;+ const gaps: GapTiming[] = [];+ for (let i = 1; i < times.length; i++) gaps.push(gapFor(times[i] - times[i - 1], speed, fps));+ return { gaps, fps };+}++/** Keyframe i dragged to `seconds`: the gaps on either side take the new times (the later keyframes keep theirs). */+export function moveKey(t: InbetweenTiming, i: number, seconds: number): InbetweenTiming {+ if (i <= 0 || i > t.gaps.length) return t;+ const times = keyTimes(t);+ const gaps = t.gaps.map((g) => ({ ...g }));+ const before = gaps[i - 1];+ gaps[i - 1] = { ...before, ...gapFor(seconds - times[i - 1], before.speed, t.fps) };+ if (i < gaps.length) {+ const moved = times[i - 1] + gapSeconds(gaps[i - 1], t.fps);+ const after = gaps[i];+ gaps[i] = { ...after, ...gapFor(times[i + 1] - moved, after.speed, t.fps) };+ }+ return { ...t, gaps };+}++/** One gap's length a step longer (+1) or shorter (−1): 4 frames. */+export const stepGap = (g: GapTiming, by: 1 | -1): GapTiming => ({ ...g, frames: snapGapFrames(g.frames + by * GAP_STEP) });++/** "37 f · ×3", the chip on the ruler. */+export const gapLabel = (g: GapTiming) => `${g.frames} f${g.speed > 1 ? ` · ×${g.speed}` : ""}`;++/** "2.4 s" (one decimal; whole seconds without one). */+export function lengthLabel(s: number): string {+ const r = Math.round(s * 10) / 10;+ return `${r % 1 ? r.toFixed(1) : r.toFixed(0)} s`;+}++/** What's wrong with a timing for this many keyframes, or null. */+export function timingProblem(t: InbetweenTiming, keys: number, engine: "open" | "cloud" = "open"): string | null {+ if (keys < MIN_KEYS) return "Add at least two keyframes: the in-betweens go between them";+ if (keys > MAX_KEYS) return `Up to ${MAX_KEYS} keyframes`;+ if (t.gaps.length !== keys - 1) return `${keys} keyframes have ${keys - 1} gaps between them`;+ const can = ENGINE_TIMING[engine];+ if (!can.rates.includes(t.fps)) return engine === "cloud" ? "In the cloud the animation plays at 16 fps — 24 fps runs on your GPU" : "Play at 24 or 16 fps";+ for (const [i, g] of t.gaps.entries()) {+ const at = `Between keyframes ${i + 1} and ${i + 2}`;+ if (!Number.isInteger(g.frames) || g.frames < MIN_GAP_FRAMES || g.frames > MAX_GAP_FRAMES || (g.frames - 1) % GAP_STEP)+ return `${at}: ${MIN_GAP_FRAMES} to ${MAX_GAP_FRAMES} frames, in steps of ${GAP_STEP}`;+ if (!can.speeds.includes(g.speed)) return engine === "cloud" ? `${at}: in the cloud every frame is kept (×1) — ×${g.speed} runs on your GPU` : `${at}: speed ×1 to ×4`;+ }+ return null;+}++/**+ * The timing a request carries (params.extra.inbetween), checked; when it has none, Frames mode's times make it. Throws+ * with a message for the person when it's wrong.+ */+export function requestTiming(extra: Record<string, unknown> | undefined, keys: number, times: number[] | undefined, engine: "open" | "cloud"): InbetweenTiming {+ const raw = extra?.inbetween as Partial<InbetweenTiming> | undefined;+ let t: InbetweenTiming;+ if (raw && typeof raw === "object" && Array.isArray(raw.gaps)) {+ const gaps = raw.gaps.map((g) => ({+ frames: Number((g as GapTiming)?.frames),+ speed: Number((g as GapTiming)?.speed ?? 1) as Speed,+ ...(typeof (g as GapTiming)?.prompt === "string" && (g as GapTiming).prompt!.trim() && { prompt: (g as GapTiming).prompt!.trim().slice(0, 500) }),+ }));+ t = { gaps, fps: Number(raw.fps ?? ENGINE_TIMING[engine].rates[0]) as PlaybackFps };+ } else if (times && times.length === keys && keys >= MIN_KEYS) {+ t = timingFromTimes(times, { engine });+ } else t = timingFor(keys, undefined, engine);+ const problem = timingProblem(t, keys, engine);+ if (problem) throw new Error(problem);+ return t;+}++/** The words a gap's pass runs on: the film's prompt, the gap's own words, and "mouth does not move" when asked. */+export function gapPrompt(base: string | undefined, gap: Pick<GapTiming, "prompt">, stillMouth: boolean): string {+ return [base?.trim().replace(/[.\s]+$/, ""), gap.prompt?.trim().replace(/[.\s]+$/, ""), stillMouth ? STILL_MOUTH_PROMPT : ""].filter(Boolean).join(". ");+}diff --git a/tests/inbetween.test.ts b/tests/inbetween.test.tsnew file mode 100644index 00000000..16a02c00--- /dev/null+++ b/tests/inbetween.test.ts@@ -0,0 +1,112 @@+import { describe, expect, it } from "vitest";+import {+ gapFor,+ gapPrompt,+ gapSelection,+ keptFrames,+ keyTimes,+ madeFrames,+ moveKey,+ requestTiming,+ snapGapFrames,+ stepGap,+ timingFor,+ timingFromTimes,+ timingProblem,+ totalFrames,+ totalSeconds,+ WORKFLOW_TIMING,+} from "@/lib/inbetween";++/** The frame indexes VHS's every-Nth selection keeps: images[skip::every]. */+const picked = (frames: number, speed: 1 | 2 | 3 | 4) => {+ const { skip, every } = gapSelection({ frames, speed });+ const out: number[] = [];+ for (let i = skip; i < frames; i += every) out.push(i);+ return out;+};++describe("in-between timing", () => {+ it("snaps a gap to Wan's 4n + 1 frames, 17 to 81", () => {+ expect(snapGapFrames(10)).toBe(17);+ expect(snapGapFrames(36)).toBe(37);+ expect(snapGapFrames(38)).toBe(37);+ expect(snapGapFrames(40)).toBe(41);+ expect(snapGapFrames(500)).toBe(81);+ expect(snapGapFrames(Number.NaN)).toBe(17);+ expect(stepGap({ frames: 17, speed: 1 }, -1).frames).toBe(17);+ expect(stepGap({ frames: 17, speed: 2 }, 1)).toEqual({ frames: 21, speed: 2 });+ });++ it("keeps every Nth frame counted back from the next keyframe, never the keyframe before", () => {+ for (const frames of [17, 21, 37, 45, 81])+ for (const speed of [1, 2, 3, 4] as const) {+ const p = picked(frames, speed);+ expect(p.at(-1)).toBe(frames - 1); // the next keyframe is on screen+ expect(p[0]).toBeGreaterThan(0); // the joint frame is dropped+ expect(p.length).toBe(gapSelection({ frames, speed }).kept);+ for (let i = 1; i < p.length; i++) expect(p[i] - p[i - 1]).toBe(speed);+ }+ expect(picked(17, 3)).toEqual([1, 4, 7, 10, 13, 16]);+ expect(picked(17, 4)).toEqual([4, 8, 12, 16]);+ });++ it("times the workflow's own five keyframes", () => {+ // 37 ×1 → 36, 17 ×3 → 6, 17 ×4 → 4, 45 ×1 → 44, plus keyframe 1.+ expect(keptFrames(WORKFLOW_TIMING)).toEqual([37, 6, 4, 44]);+ expect(totalFrames(WORKFLOW_TIMING)).toBe(91);+ expect(totalSeconds(WORKFLOW_TIMING)).toBeCloseTo(91 / 24);+ expect(keyTimes(WORKFLOW_TIMING)).toEqual([0, 1.5, 1.75, 1.917, 3.75]);+ expect(madeFrames(WORKFLOW_TIMING)).toBe(116);+ expect(totalSeconds({ ...WORKFLOW_TIMING, fps: 16 })).toBeCloseTo(91 / 16);+ });++ it("maps Frames mode's times to gaps at the playback rate", () => {+ const t = timingFromTimes([0, 1.5, 3, 4]);+ expect(t.fps).toBe(24);+ expect(t.gaps.map((g) => g.frames)).toEqual([37, 37, 25]);+ expect(keyTimes(t)).toEqual([0, 1.5, 3, 4]);+ // ×2 makes twice the frames for the same time; true time at 16 fps.+ expect(timingFromTimes([0, 1], { speed: 2 }).gaps[0]).toEqual({ frames: 49, speed: 2 });+ expect(timingFromTimes([0, 2], { fps: 16 }).gaps[0].frames).toBe(33);+ // The cloud keeps every frame at 16 fps.+ expect(timingFromTimes([0, 2], { fps: 24, speed: 3, engine: "cloud" })).toEqual({ gaps: [{ frames: 33, speed: 1 }], fps: 16 });+ // Too close: the shortest pass.+ expect(gapFor(0.1, 1, 24).frames).toBe(17);+ });++ it("moves a keyframe by retiming the gaps on either side", () => {+ const t = timingFromTimes([0, 1, 2]);+ const moved = moveKey(t, 1, 1.5);+ expect(keyTimes(moved)[1]).toBeCloseTo(1.5);+ expect(keyTimes(moved)[2]).toBeCloseTo(2.167, 2); // the later keyframe stays close (the shortest pass is 17 frames)+ expect(moveKey(t, 0, 3)).toBe(t);+ });++ it("fits saved timing to the keyframes and the engine", () => {+ const t = timingFor(4, { gaps: [{ frames: 30, speed: 3, prompt: " jumps " }], fps: 16 });+ expect(t.gaps).toEqual([{ frames: 29, speed: 3, prompt: "jumps" }, { frames: 33, speed: 1 }, { frames: 33, speed: 1 }]);+ expect(timingFor(3, { gaps: [{ frames: 21, speed: 4 }], fps: 24 }, "cloud")).toEqual({ gaps: [{ frames: 21, speed: 1 }, { frames: 33, speed: 1 }], fps: 16 });+ });++ it("says what's wrong", () => {+ expect(timingProblem(timingFor(2, undefined), 1)).toMatch(/two keyframes/);+ expect(timingProblem({ gaps: [{ frames: 18, speed: 1 }], fps: 24 }, 2)).toMatch(/17 to 81/);+ expect(timingProblem({ gaps: [{ frames: 17, speed: 3 }], fps: 16 }, 2, "cloud")).toMatch(/your GPU/);+ expect(timingProblem({ gaps: [{ frames: 17, speed: 1 }], fps: 24 }, 2, "cloud")).toMatch(/16 fps/);+ expect(timingProblem({ gaps: [{ frames: 17, speed: 1 }], fps: 24 }, 3)).toMatch(/2 gaps/);+ expect(timingProblem(WORKFLOW_TIMING, 5)).toBeNull();+ });++ it("reads a request's timing, or makes it from the frame times", () => {+ expect(requestTiming({ inbetween: WORKFLOW_TIMING }, 5, undefined, "open")).toEqual(WORKFLOW_TIMING);+ expect(requestTiming({}, 3, [0, 1, 2], "open").gaps.map((g) => g.frames)).toEqual([25, 25]);+ expect(requestTiming(undefined, 2, undefined, "cloud")).toEqual({ gaps: [{ frames: 33, speed: 1 }], fps: 16 });+ expect(() => requestTiming({ inbetween: { gaps: [{ frames: 17, speed: 9 }], fps: 24 } }, 2, undefined, "open")).toThrow(/×1 to ×4/);+ });++ it("writes each gap's words", () => {+ expect(gapPrompt("A girl turns. ", { prompt: "she waves" }, true)).toBe("A girl turns. she waves. mouth does not move");+ expect(gapPrompt("", {}, false)).toBe("");+ });+});