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
Cinema: send reference videos and audio where the model takes them
Claude33be73c4b2f6
diff --git a/src/lib/catalog/cinema.ts b/src/lib/catalog/cinema.tsindex 516b770c..d22a4c9f 100644--- a/src/lib/catalog/cinema.ts+++ b/src/lib/catalog/cinema.ts@@ -1,5 +1,5 @@import { getModel, getPreset } from "./index";-import { composeShots, estimateCost, planGeneration } from "./plan";+import { composeShots, estimateCost, planGeneration, routesFor } from "./plan";import type { Engine, GenerationRequest, GenParams, LoraRef, ModelDef, Operation } from "./types";/*@@ -1001,6 +1001,55 @@ export function withPlaceholders(step: CinemaStep): GenerationRequest {return { ...req, inputs };}+type ClipKey = "videos" | "audios";+const CLIP_WORDS: Record<ClipKey, string> = { videos: "reference videos", audios: "reference audio" };++/**+ * Whether `model` sends reference clips of this kind for `op`: every one of its routes for the+ * operation has a field for inputs.videos / inputs.audios (so whichever provider runs it, they go).+ */+export function takesClips(model: ModelDef, op: Operation, key: ClipKey): boolean {+ const routes = routesFor(model, op);+ return routes.length > 0 && routes.every((r) => !!(r.map as Record<string, unknown> | undefined)?.[key]);+}++/** The most clips of a kind the model takes (the catalog's maxVideos / maxAudios, else the studio's limit). */+function clipMax(model: ModelDef, key: ClipKey): number {+ const o = model.options as { maxVideos?: number; maxAudios?: number };+ return (key === "videos" ? o.maxVideos : o.maxAudios) ?? REFERENCE_LIMITS[key];+}++/**+ * The reference clips a step sends: the ones the model's routes for `op` can take (up to its limit).+ * The rest are left out with a note that says so, naming models in `others` that would take them.+ */+function clipInputs(model: ModelDef, op: Operation, input: CinemaGenerateInput, notes: string[], others: ModelDef[] = []): { videos?: string[]; audios?: string[] } {+ const out: { videos?: string[]; audios?: string[] } = {};+ const dropped: ClipKey[] = [];+ for (const key of ["videos", "audios"] as const) {+ const list = input[key] ?? [];+ if (!list.length) continue;+ if (!takesClips(model, op, key)) {+ dropped.push(key);+ continue;+ }+ const max = clipMax(model, key);+ if (list.length > max) notes.push(`${model.name} takes up to ${max} ${CLIP_WORDS[key]}`);+ out[key] = list.slice(0, max);+ }+ if (dropped.length) {+ const words = dropped.map((k) => CLIP_WORDS[k]).join(" and ");+ const can = others.filter((m) => m.id !== model.id && dropped.every((k) => takesClips(m, op, k))).map((m) => m.name);+ const sent = !!(out.videos || out.audios);+ notes.push(+ can.length+ ? `${model.name} doesn't take ${words} — ${can.join(" or ")} ${can.length > 1 ? "do" : "does"}`+ : `${words.charAt(0).toUpperCase()}${words.slice(1)} aren't sent to ${model.name}${sent ? "" : " — only image references are used"}`,+ );+ }+ return out;+}+function nearest<T extends number>(values: T[], want: number): T {return [...values].sort((a, b) => Math.abs(a - want) - Math.abs(b - want) || a - b)[0];}@@ -1145,12 +1194,16 @@ function planVideo(input: CinemaGenerateInput, ctx: CinemaContext): CinemaPlan {notes.push(max === 1 ? `${model.name} uses the first image as the start frame` : `${model.name} takes up to ${max} reference images`);images = images.slice(0, max);}- if ((input.videos?.length || input.audios?.length) && input.action === "generate")- notes.push(`${model.name} reads image references; video and audio references are used by Extend and Edit`);+ // Reference clips go to reference-to-video when the model's routes take them; otherwise a note says they're left out.+ const others = CINEMA_MODELS.video[input.engine].filter((id) => ctx.higgsfield || !HIGGSFIELD_ONLY.has(id)).flatMap((id) => getModel(id) ?? []);+ const clips = clipInputs(model, "ref2v", input, notes, others);+ const withClips = !!(clips.videos?.length || clips.audios?.length);+ if (withClips && images.length === 1) notes.push("With reference clips, the image is a reference rather than the start frame");const blocker = !input.prompt.trim() && !images.length ? "Describe your scene first" : undefined;+ const inputs = images.length || withClips ? { ...(images.length ? { images } : {}), ...clips } : undefined;const steps: CinemaStep[] = [- { key: "main", request: { feature: "cinema", modelId: model.id, prompt, inputs: images.length ? { images } : undefined, params, loras } },+ { key: "main", request: { feature: "cinema", modelId: model.id, ...(withClips ? { op: "ref2v" as const } : {}), prompt, inputs, params, loras } },];const hex = paletteHex(input.settings.palette);if (open && hex && input.settings.paletteLock !== false) {@@ -1175,6 +1228,8 @@ function planVideo(input: CinemaGenerateInput, ctx: CinemaContext): CinemaPlan {return finish(model, steps, notes, blocker);}+const STILL_CLIPS_NOTE = "Stills use image references — reference videos and audio are left out";+function editModel(engine: Engine): ModelDef {return getModel(engine === "open" ? "oss-qwen-edit" : "nano-banana-pro")!;}@@ -1188,6 +1243,7 @@ function planStill(input: CinemaGenerateInput, ctx: CinemaContext): CinemaPlan {notes.push(`Stills with references run on ${instead.name}`);model = instead;}+ if (input.videos?.length || input.audios?.length) notes.push(STILL_CLIPS_NOTE);const compiled = compileDirector({ prompt: input.prompt, settings: input.settings, brief: input.brief, mode: "image", target: "prompt" });const params = fitParams(model, { ...input, resolution: input.resolution ?? "1K" }, notes, "image");const blocker = stillCameraMissing(input.settings) ?? (!input.prompt.trim() ? "Describe your scene first" : undefined);@@ -1230,6 +1286,7 @@ function planGrid(input: CinemaGenerateInput, ctx: CinemaContext): CinemaPlan {const { rows, cols } = gridDims(size);Object.assign(extra, { rows, columns: cols });} else model = input.engine === "cloud" ? getModel("nano-banana-pro")! : pickModel("image", input, ctx, notes);+ if (input.videos?.length || input.audios?.length) notes.push(STILL_CLIPS_NOTE);const compiled = compileDirector({ prompt: input.prompt, settings: input.settings, brief: input.brief, mode: "image", target: "prompt" });const params = fitParams(model, { ...input, batch: 1, resolution: input.resolution ?? "2K" }, notes, "image");if (Object.keys(extra).length) params.extra = extra;@@ -1288,9 +1345,10 @@ function planExtend(input: CinemaGenerateInput, ctx: CinemaContext): CinemaPlanconst compiled = compileDirector({ prompt: scene, settings, moves, brief: input.brief, mode: "video", target: "prompt" });const params = fitParams(model, input, notes, "video");params.numOutputs = 1;+ const clips = clipInputs(model, "extend", input, notes);return finish(model,- [{ key: "main", request: { feature: "cinema", modelId: model.id, op: "extend", prompt: compiled.prompt, inputs: src ? { video: src.url } : undefined, params, step: { label: "Extended · sequel", index: 0, of: 1 } } }],+ [{ key: "main", request: { feature: "cinema", modelId: model.id, op: "extend", prompt: compiled.prompt, inputs: src ? { video: src.url, ...clips } : undefined, params, step: { label: "Extended · sequel", index: 0, of: 1 } } }],notes,src ? undefined : "Select a video take to extend",);@@ -1309,6 +1367,7 @@ function planExtend(input: CinemaGenerateInput, ctx: CinemaContext): CinemaPlanrequest: { feature: "cinema", modelId: "extract-frame", op: "frame", inputs: { video: src.url }, params: { extra: { frame_type: "first" } }, step: { label: "First frame", index: 0, of: 3 } },});}+ if (input.videos?.length || input.audios?.length) notes.push("A prequel is made from the take's first frame — reference videos and audio aren't used");const scene = tidy(input.prompt) || "the moments right before this shot";const n = frame ? 2 : 3;steps.push({@@ -1354,9 +1413,11 @@ function planEdit(input: CinemaGenerateInput, ctx: CinemaContext): CinemaPlan {if (input.engine === "cloud") {const model = getModel(ctx.higgsfield ? "seedance-2-5" : "kling-o3")!;const images = (input.images ?? []).slice(0, 4);+ // Extra reference clips ride along when the edit model's routes take them.+ const clips = clipInputs(model, "v2v", input, notes);return finish(model,- [{ key: "main", request: { feature: "cinema", modelId: model.id, op: "v2v", prompt, inputs: src ? { video: src.url, ...(images.length ? { images } : {}) } : undefined, params: { numOutputs: 1 }, step: { label: "Edited take", index: 0, of: 1 } } }],+ [{ key: "main", request: { feature: "cinema", modelId: model.id, op: "v2v", prompt, inputs: src ? { video: src.url, ...(images.length ? { images } : {}), ...clips } : undefined, params: { numOutputs: 1 }, step: { label: "Edited take", index: 0, of: 1 } } }],notes,blocker,);@@ -1365,11 +1426,12 @@ function planEdit(input: CinemaGenerateInput, ctx: CinemaContext): CinemaPlan {if (!restyle) return finish(editModel("open"), [], notes, blocker ?? "Open-source video edit needs the Wan 2.2 Fun VACE Restyle workflow — switch to Cloud for this one");if (src && !src.firstFrame) blocker ??= "Couldn't read this take's first frame — open it in a browser that can play it, then try again";const edit = editModel("open");+ const clips = clipInputs(restyle, "v2v", input, notes);return finish(restyle,[{ key: "frame-edit", request: { feature: "cinema", modelId: edit.id, op: "edit", prompt, inputs: src?.firstFrame ? { images: [src.firstFrame] } : undefined, params: { numOutputs: 1 }, step: { label: "Edited first frame", index: 0, of: 2 } } },- { key: "main", after: ["frame-edit"], request: { feature: "cinema", modelId: restyle.id, op: "v2v", prompt, inputs: src ? { video: src.url } : undefined, chain: [{ slot: "images", position: "first" }], step: { label: "Edited take", index: 1, of: 2 } } },+ { key: "main", after: ["frame-edit"], request: { feature: "cinema", modelId: restyle.id, op: "v2v", prompt, inputs: src ? { video: src.url, ...clips } : undefined, chain: [{ slot: "images", position: "first" }], step: { label: "Edited take", index: 1, of: 2 } } },],notes,blocker,diff --git a/tests/cinema-refs.test.ts b/tests/cinema-refs.test.tsnew file mode 100644index 00000000..75faf0ec--- /dev/null+++ b/tests/cinema-refs.test.ts@@ -0,0 +1,98 @@+import { afterEach, describe, expect, it } from "vitest";+import { draftToInput, initialDraft } from "@/components/cinema/draft";+import { getModel } from "@/lib/catalog";+import { planCinema, takesClips, type CinemaGenerateInput } from "@/lib/catalog/cinema";+import type { ModelRoute } from "@/lib/catalog/types";++// Reference clips (video, audio) reach the cloud models whose field maps take them, and every other+// route says plainly that they're left out. The maps come from the cloud catalog: the tests give the+// Cinema Studio 4.0 / Seedance routes the fields the catalog adds (video_urls, audio_urls) and put them back.++const VIDEO: CinemaGenerateInput = { engine: "cloud", mode: "video", action: "generate", prompt: "A lighthouse keeper climbs the stairs", settings: {}, duration: 5, batch: 1 };+const CLIPS = { videos: ["/api/files/ref.mp4"], audios: ["/api/files/ref.mp3"] };++const saved: [ModelRoute, ModelRoute["map"]][] = [];+function giveClipFields(modelId: string, op: ModelRoute["op"]) {+ for (const r of getModel(modelId)!.routes.filter((x) => x.op === op)) {+ saved.push([r, r.map]);+ r.map = { ...r.map, videos: "video_urls", audios: "audio_urls" } as ModelRoute["map"];+ }+}+afterEach(() => {+ for (const [r, map] of saved.splice(0)) r.map = map;+});++describe("reference clips", () => {+ it("are sent to reference-to-video when the model's routes take them", () => {+ giveClipFields("cinema-studio-4", "ref2v");+ expect(takesClips(getModel("cinema-studio-4")!, "ref2v", "videos")).toBe(true);+ const plan = planCinema({ ...VIDEO, ...CLIPS, images: ["/api/files/a.png"] }, { higgsfield: true });+ expect(plan.model.id).toBe("cinema-studio-4");+ expect(plan.blocker).toBeUndefined();+ const req = plan.steps[0].request;+ expect(req.op).toBe("ref2v");+ expect(req.inputs).toMatchObject({ images: ["/api/files/a.png"], ...CLIPS });+ expect(plan.notes.join(" ")).toMatch(/image is a reference rather than the start frame/);+ // Clips are references, not a scene: the prompt is still needed.+ expect(planCinema({ ...VIDEO, ...CLIPS, prompt: "" }, { higgsfield: true }).blocker).toBe("Describe your scene first");+ });++ it("are left out with a note naming the models that would take them", () => {+ giveClipFields("cinema-studio-4", "ref2v");+ const plan = planCinema({ ...VIDEO, ...CLIPS, modelId: "kling-3" }, { higgsfield: true });+ expect(plan.model.id).toBe("kling-3");+ expect(plan.steps[0].request.inputs).toBeUndefined();+ // (Seedance 2.5 is named too once the cloud catalog maps its clip fields.)+ expect(plan.notes.join(" ")).toMatch(/Kling 3\.0 doesn't take reference videos and reference audio — Cinema Studio 4\.0( or Seedance 2\.5 do| does)/);+ // Without a Higgsfield key Cinema Studio isn't offered: Seedance is named when it takes them, else the note says plainly they're left out.+ const none = planCinema({ ...VIDEO, videos: CLIPS.videos, modelId: "kling-3" }, { higgsfield: false });+ expect(none.notes.join(" ")).toMatch(/Kling 3\.0 doesn't take reference videos — Seedance 2\.5 does|Reference videos aren't sent to Kling 3\.0 — only image references are used/);+ expect(none.notes.join(" ")).not.toMatch(/Cinema Studio/);+ // Open models and stills never take them.+ expect(planCinema({ ...VIDEO, ...CLIPS, engine: "open" }, { higgsfield: false }).notes.join(" ")).toMatch(/aren't sent to Wan 2\.2/);+ expect(planCinema({ ...VIDEO, ...CLIPS, mode: "image", settings: { camera: "modern", lens: "clean-sharp", focal: "50mm" } }, { higgsfield: false }).notes).toContain(+ "Stills use image references — reference videos and audio are left out",+ );+ });++ it("ride along on Edit and Extend when those routes take them", () => {+ giveClipFields("seedance-2-5", "v2v");+ giveClipFields("seedance-2-5", "extend");+ const source = { url: "/api/files/take.mp4", kind: "video" as const };+ const edit = planCinema({ ...VIDEO, ...CLIPS, action: "edit", prompt: "make it snow", source }, { higgsfield: true });+ expect(edit.model.id).toBe("seedance-2-5");+ expect(edit.steps[0].request.inputs).toMatchObject({ video: source.url, ...CLIPS });+ const ext = planCinema({ ...VIDEO, ...CLIPS, action: "extend-forward", source }, { higgsfield: false });+ expect(ext.steps[0].request.inputs).toMatchObject({ video: source.url, ...CLIPS });+ const kling = planCinema({ ...VIDEO, ...CLIPS, action: "edit", prompt: "make it snow", source }, { higgsfield: false });+ expect(kling.model.id).toBe("kling-o3");+ expect(kling.steps[0].request.inputs).toEqual({ video: source.url });+ expect(kling.notes.join(" ")).toMatch(/aren't sent to Kling O3/);+ });+});++describe("draftToInput", () => {+ it("maps the prompt bar's state to a generate request", () => {+ const d = initialDraft();+ const refs = [+ { id: "1", ref: "/api/files/a.png", preview: "", kind: "image" as const },+ { id: "2", ref: "", preview: "blob:x", kind: "image" as const, uploading: true },+ { id: "3", ref: "/api/files/b.mp4", preview: "", kind: "video" as const },+ { id: "4", ref: "/api/files/c.mp3", preview: "", kind: "audio" as const },+ ];+ const v = draftToInput({ ...d, prompt: "rain", refs, models: { "video:cloud": "kling-3" } }, "cloud", { genre: "noir" });+ expect(v).toMatchObject({ engine: "cloud", mode: "video", action: "generate", modelId: "kling-3", prompt: "rain", images: ["/api/files/a.png"], videos: ["/api/files/b.mp4"], audios: ["/api/files/c.mp3"], settings: { genre: "noir" } });+ expect(v.source).toBeUndefined();+ expect(v.grid).toBeUndefined();++ // A selected take: its action and source go along (with the first frame read in the browser).+ const source = { generationId: "g1", url: "/api/files/take.mp4", kind: "video" as const, label: "Take" };+ const edit = draftToInput({ ...d, source, action: "edit" }, "open", {}, "/api/files/frame.png");+ expect(edit).toMatchObject({ action: "edit", source: { url: source.url, kind: "video", firstFrame: "/api/files/frame.png" } });+ // Image mode ignores the take and its action; Grid sends the grid.+ const grid = draftToInput({ ...d, mode: "image", imageTool: "grid", source, action: "edit", grid: { size: "3x3", type: "zooms" } }, "cloud", {});+ expect(grid).toMatchObject({ mode: "image", action: "grid", grid: { size: "3x3", type: "zooms" }, moves: undefined, multiShot: undefined });+ expect(grid.source).toBeUndefined();+ expect(draftToInput({ ...d, mode: "image", source, action: "edit" }, "cloud", {}).action).toBe("generate");+ });+});