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
Video references: a person in a picture performing a clip's movement gets each input's role
Adarsh2f437873438e
diff --git a/src/lib/h3-references.ts b/src/lib/h3-references.tsindex d91cf591..b20795b2 100644--- a/src/lib/h3-references.ts+++ b/src/lib/h3-references.ts@@ -147,3 +147,111 @@ export function expandH3References(wf: ComfyWorkflow, files: H3ReferenceFiles):});return out;}++/** Where a motion copy takes place: the clip's setting and camera (with the new person in it), or the picture's own setting. */+export type H3MotionPlace = "picture" | "clip";++/** The place a motion copy uses when the words don't say. */+export const H3_MOTION_DEFAULT_PLACE: H3MotionPlace = "clip";++export interface H3MotionPlan {+ /** The picture whose person performs the motion (1-based). */+ picture: number;+ /** How the words name that person ("woman"), or "person". */+ who: string;+ place: H3MotionPlace;+}++const MOTION_WORD = /\b(?:danc\w*|move|moves|moving|movements?|motion|choreograph\w*|steps|poses?|posing|gestures?|perform\w*|mimic\w*|copy|copies|copying|imitat\w*)\b/i;+const PERSON_WORD = /^(?:woman|man|girl|boy|lady|guy|person|model|dancer|actor|actress|character|singer|athlete)$/i;+const PLACE_NOUN = "(?:setting|place|room|background|location|studio|stage|scene|set)";+const placeOf = (tag: "Video" | "Picture") =>+ new RegExp(`\\b${PLACE_NOUN}\\s+(?:of|from|in)\\s+<${tag} \\d+>|<${tag} \\d+>'s\\s+${PLACE_NOUN}\\b|\\b(?:same|that)\\s+${PLACE_NOUN}\\s+as\\s+<${tag} \\d+>`, "i");+const CLIP_PLACE = placeOf("Video");+const PICTURE_PLACE = placeOf("Picture");+const IN_LAYOUT = /\b(?:integrated_multimodal_description|subject_definitions)\s*:/;++/**+ * Whether the words ask for the person in a reference picture to perform the movement of the reference clip ("the woman in+ * image 1 dancing like the woman in clip 1"), and which picture and place. Null for anything else, for words already in+ * H3's layout, and for more than one clip or any voice or music clip.+ */+export function h3MotionPlan(words: string, refs: { images: number; videos: number; audios: number }): H3MotionPlan | null {+ if (refs.images < 1 || refs.videos !== 1 || refs.audios > 0) return null;+ const text = h3ReferenceTags(words.trim());+ if (!text || IN_LAYOUT.test(text)) return null;+ const sentences = text.split(/(?<=[.!?])\s+|\n+/);+ if (!sentences.some((s) => (/<Video 1>/.test(s) || /\bthe (?:clip|video)\b/i.test(s)) && MOTION_WORD.test(s))) return null;+ const named = /\b(?:the|a|an)\s+(\w+)\s+(?:in|from|of)\s+<Picture (\d+)>/i.exec(text);+ const picture = named ? Number(named[2]) : Number(/<Picture (\d+)>/.exec(text)?.[1] ?? 1);+ if (picture < 1 || picture > refs.images) return null;+ const who = named && PERSON_WORD.test(named[1]) ? named[1].toLowerCase() : "person";+ const place: H3MotionPlace = CLIP_PLACE.test(text) ? "clip" : PICTURE_PLACE.test(text) ? "picture" : H3_MOTION_DEFAULT_PLACE;+ return { picture, who, place };+}++/** The person's words as the shot's action: the request verb goes, the pictured person becomes <Subject 1>, the clip's performer is named by role. */+function motionAction(words: string, picture: number): string {+ let s = h3ReferenceTags(words.trim())+ .replace(/^(?:please\s+)?(?:create|make|generate|render|produce|show)\s+(?:me\s+)?(?:(?:a|an|the)\s+)?(?:(?:short\s+)?(?:video|clip|film|animation)\s+)?(?:(?:of|where|in which|showing)\s+)?/i, "")+ .replace(new RegExp(`\\b(?:the|a|an)\\s+\\w+\\s+(?:in|from|of)\\s+<Picture ${picture}>`, "gi"), "<Subject 1>")+ .replace(/\b(?:the|a|an)\s+\w+\s+(?:in|from|of)\s+<Video 1>/gi, "the performer of <Video 1>")+ .replace(/\s+/g, " ")+ .trim();+ if (!s) return "";+ s = s[0].toUpperCase() + s.slice(1);+ return /[.!?]$/.test(s) ? s : `${s}.`;+}++const PRONOUN: Record<string, [string, string]> = { woman: ["her", "her"], girl: ["her", "her"], lady: ["her", "her"], actress: ["her", "her"], man: ["him", "his"], boy: ["him", "his"], guy: ["him", "his"], actor: ["him", "his"] };++/**+ * H3's full-reference layout for a motion copy: the person of the picture, with the complete outfit, performs the movement,+ * framing and timing of the clip in every frame through the last frame. Place "clip": the clip's setting and camera stay+ * and its performer becomes the pictured person. Place "picture": the clip supplies only motion and every surface comes from+ * the picture. The clip's own sound is reused unless a sound or music line is given. Other pictures stay further references.+ * Null when the words are not a motion copy (h3MotionPlan).+ */+export function h3MotionPrompt(+ words: string,+ refs: { images: number; videos: number; audios: number },+ opts: { place?: H3MotionPlace; sound?: string; music?: string } = {},+): string | null {+ const found = h3MotionPlan(words, refs);+ if (!found) return null;+ const place = opts.place ?? found.place;+ const P = `<Picture ${found.picture}>`;+ const V = "<Video 1>";+ const [them, their] = PRONOUN[found.who] ?? ["them", "their"];+ const inClip = place === "clip";+ const extra: string[] = [];+ for (let n = 1, k = 3; n <= refs.images; n++) if (n !== found.picture) extra.push(`<Subject ${k++}> is what <Picture ${n}> shows, used as the shot describes.`);+ const defs = [+ `<Subject 1> is the ${found.who} in ${P}, with ${their} complete outfit.`,+ `<Subject 2> is the setting of ${inClip ? V : P}.`,+ inClip+ ? `${V} is the source of the setting, camera, body movement, framing and timing. Its performer becomes <Subject 1>.`+ : `${V} is a motion guide at 24 fps. It supplies only the body poses, arm and leg movements, head direction, framing and timing; every visible surface comes from ${P}.`,+ ...extra,+ ];+ const summary = `[reference generation + audio reuse] The target video shows <Subject 1> in <Subject 2>, performing the movement of ${V} with its ${inClip ? "camera, framing" : "framing"} and timing, from the first frame to the last.`;+ const retention = [+ `<Subject 1> (appears in [Shot 1]): fully_preserved - the identity and complete outfit of ${P}.`,+ inClip ? `<Subject 2> (setting): fully_preserved - the place, light and camera of ${V}.` : `<Subject 2> (setting): fully_preserved - the place of ${P} fills the frame around <Subject 1> in every frame.`,+ inClip+ ? `${V} (setting, motion and timing): partially_preserved - its place, camera, movement and timing stay; its performer is <Subject 1>.`+ : `${V} (motion and timing): attribute_transfer - its movement is performed by <Subject 1>.`,+ ];+ const shot = inClip+ ? `[Shot 1] The performer of ${V} is <Subject 1> in every frame through the last frame, with the face, hair and complete outfit of ${P}. <Subject 1> performs every movement of ${V} at the same moment, full body in frame, with its camera and framing, in real time. One continuous take.`+ : `[Shot 1] <Subject 1> performs every movement of ${V} at the same moment, moving into each new pose with its timing, full body in frame, through the last frame. The setting of ${P} stays fully visible around ${them} in every frame. One continuous take.`;+ const action = motionAction(words, found.picture);+ return [+ `subject_definitions:\n${defs.join("\n")}`,+ `summary:\n${summary}`,+ `retention_analysis:\n${retention.join("\n")}`,+ `detailed_description:\nLive-action footage in the setting and light of ${inClip ? V : P}.\n${shot}${action ? ` ${action}` : ""}`,+ `overall_soundscape:\n${opts.sound?.trim() || `The sound of ${V}, reused unchanged.`}`,+ `non_diegetic_music:\n${opts.music?.trim() || `The music of ${V}, reused unchanged.`}`,+ ].join("\n\n");+}diff --git a/src/server/generations.ts b/src/server/generations.tsindex c0d10bdf..09e4e902 100644--- a/src/server/generations.ts+++ b/src/server/generations.ts@@ -40,7 +40,7 @@ import { computeDimensions } from "@/lib/comfy";import { captionFromWords, captionWords, parseCaption } from "@/lib/ideogram-caption";import { AUTO_IMAGE, AUTO_LARGE_MODELS, autoCandidates, autoModelsFor, autoNext, autoPatterns, autoResolution, nextDeclinedFallback } from "@/lib/auto-image";import { h3Frames, h3Prompt, inH3Layout } from "@/lib/h3-prompt";-import { h3ReferenceTags, h3TalkPrompt } from "@/lib/h3-references";+import { h3MotionPrompt, h3ReferenceTags, h3TalkPrompt } from "@/lib/h3-references";import {CHAIN_LIST_SLACK,LAST_FRAME_CHAIN,@@ -3753,7 +3753,11 @@ async function handOver(id: string, route: ModelRoute, providerId: ProviderId, pprompt = wrapped;update(id, { prompt });} else if (plan.model.options.promptFormat === "minimax-h3" && plan.op === "ref2v" && prompt) {- const wrapped = h3Prompt(h3ReferenceTags(prompt), "t2v", h3Frames(plan.params.duration ?? plan.model.defaults?.duration ?? 5), plan.sound?.soundscape, plan.sound?.music);+ // A person in a picture performing a clip's movement gets H3's full-reference layout, so each input has its role.+ const refs = { images: req.inputs?.images?.length ?? 0, videos: req.inputs?.videos?.length ?? 0, audios: req.inputs?.audios?.length ?? 0 };+ const wrapped =+ h3MotionPrompt(prompt, refs, { sound: plan.sound?.soundscape, music: plan.sound?.music }) ??+ h3Prompt(h3ReferenceTags(prompt), "t2v", h3Frames(plan.params.duration ?? plan.model.defaults?.duration ?? 5), plan.sound?.soundscape, plan.sound?.music);if (wrapped !== prompt) {prompt = wrapped;update(id, { prompt });diff --git a/tests/h3-motion-prompt.test.ts b/tests/h3-motion-prompt.test.tsnew file mode 100644index 00000000..b7a9974e--- /dev/null+++ b/tests/h3-motion-prompt.test.ts@@ -0,0 +1,86 @@+import { describe, expect, it } from "vitest";+import { h3MotionPlan, h3MotionPrompt, H3_MOTION_DEFAULT_PLACE } from "@/lib/h3-references";++const ONE = { images: 1, videos: 1, audios: 0 };+const ASK = "create a video of the woman in image 1 dancing like the woman in clip 1";+const FIELDS = ["subject_definitions:", "summary:", "retention_analysis:", "detailed_description:", "overall_soundscape:", "non_diegetic_music:"];++/** Every tag used is defined in subject_definitions, and every input has a tag there. */+function tagsDefined(p: string, images: number) {+ const defs = p.slice(0, p.indexOf("summary:"));+ const used = new Set(p.match(/<(?:Subject|Picture|Video|Audio) \d+>/g));+ for (const t of used) expect(defs, `${t} defined`).toContain(t);+ for (let n = 1; n <= images; n++) expect(defs).toContain(`<Picture ${n}>`);+ expect(defs).toContain("<Video 1>");+}++describe("h3MotionPlan", () => {+ it("finds a motion copy in the studio's words", () => {+ expect(h3MotionPlan(ASK, ONE)).toEqual({ picture: 1, who: "woman", place: H3_MOTION_DEFAULT_PLACE });+ expect(h3MotionPlan("Make the man in image 2 do the same moves as clip 1", { images: 2, videos: 1, audios: 0 })).toMatchObject({ picture: 2, who: "man" });+ expect(h3MotionPlan("image 1 copies the choreography of the clip", ONE)).toMatchObject({ picture: 1, who: "person" });+ });++ it("reads the place from the words", () => {+ expect(h3MotionPlan("the woman in image 1 dances like clip 1, in the room of image 1", ONE)?.place).toBe("picture");+ expect(h3MotionPlan("the woman in image 1 dances like clip 1 in the same studio as clip 1", ONE)?.place).toBe("clip");+ });++ it("leaves other reference runs alone", () => {+ expect(h3MotionPlan("the woman in image 1 walks along the beach at sunset", ONE)).toBeNull();+ expect(h3MotionPlan(ASK, { images: 0, videos: 1, audios: 0 })).toBeNull();+ expect(h3MotionPlan(ASK, { images: 1, videos: 2, audios: 0 })).toBeNull();+ expect(h3MotionPlan(ASK, { images: 1, videos: 1, audios: 1 })).toBeNull();+ expect(h3MotionPlan(`integrated_multimodal_description: [Shot 1] ${ASK}`, ONE)).toBeNull();+ expect(h3MotionPlan(`subject_definitions:\n<Subject 1> is the woman in <Picture 1>.\n\nsummary: ${ASK}`, ONE)).toBeNull();+ expect(h3MotionPlan("the woman in image 3 dances like clip 1", ONE)).toBeNull();+ });+});++describe("h3MotionPrompt", () => {+ for (const place of ["clip", "picture"] as const) {+ it(`writes the full-reference layout (${place} setting)`, () => {+ const p = h3MotionPrompt(ASK, ONE, { place })!;+ const at = FIELDS.map((f) => p.indexOf(f));+ expect(at.every((i) => i >= 0)).toBe(true);+ expect([...at].sort((a, b) => a - b)).toEqual(at);+ expect(p).toContain("<Subject 1> is the woman in <Picture 1>, with her complete outfit.");+ expect(p).toContain("[reference generation + audio reuse]");+ expect(p).toContain("in every frame");+ expect(p).toContain("[Shot 1]");+ tagsDefined(p, 1);+ // What H3 reads names only what is wanted, at real-time pace, with the picture carrying the look.+ expect(p).not.toMatch(/\b(?:no|not|never|without|don't|avoid)\b/i);+ expect(p).not.toMatch(/\b(?:slowly|slow motion|gently|languid|leisurely)\b/i);+ expect(p).not.toMatch(/\b(?:skin|make-up|makeup|glow\w*|shimmer\w*)\b/i);+ expect(p).not.toMatch(/\bcreate a video\b/i);+ // The clip's performer is named by role, never as a second woman to keep.+ expect(p).not.toMatch(/the woman in <Video 1>/);+ });+ }++ it("keeps the clip's setting and camera, with its performer replaced", () => {+ const p = h3MotionPrompt(ASK, ONE, { place: "clip" })!;+ expect(p).toContain("<Subject 2> is the setting of <Video 1>.");+ expect(p).toContain("The performer of <Video 1> is <Subject 1> in every frame through the last frame");+ });++ it("uses the clip for motion only when the picture's setting is asked for", () => {+ const p = h3MotionPrompt(ASK, ONE, { place: "picture" })!;+ expect(p).toContain("<Subject 2> is the setting of <Picture 1>.");+ expect(p).toContain("attribute_transfer");+ expect(p).toContain("every visible surface comes from <Picture 1>");+ });++ it("defines further pictures and takes a preset's sound lines", () => {+ const p = h3MotionPrompt("the woman in image 1 dances like clip 1, wearing the gown of image 2", { images: 2, videos: 1, audios: 0 }, { sound: "Heels on a wooden floor.", music: "N/A" })!;+ expect(p).toContain("<Subject 3> is what <Picture 2> shows");+ tagsDefined(p, 2);+ expect(p).toContain("overall_soundscape:\nHeels on a wooden floor.");+ expect(p).toContain("non_diegetic_music:\nN/A");+ });++ it("returns null when the words are not a motion copy", () => {+ expect(h3MotionPrompt("the woman in image 1 walks into the scene of clip 1", ONE)).toBeNull();+ });+});