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: a clip without sound runs on the H3 swap, scene and picture graphs
Adarsh7724ab721f6a
diff --git a/src/lib/comfy-compose.ts b/src/lib/comfy-compose.tsindex 162d53f3..a4ba1f41 100644--- a/src/lib/comfy-compose.ts+++ b/src/lib/comfy-compose.ts@@ -1323,7 +1323,7 @@ export async function composeWorkflow(endpointTemplate: string, input: GenInput,wf = seconds ? silenceVhsAudio(wf, Math.max(1, Math.round(seconds * fps)) / fps) : dropVhsAudio(wf);}if (clip && !clip.hasAudio && clip.hasVideo) {- wf = dropVhsAudio(wf);+ wf = dropVhsAudio(wf, clipLength);// Core LoadAudio reading the clip's own sound (the end card's ad): no sound, no sound track.wf = dropLoadedSound(wf, (b.video ?? []).filter((t) => t.input === "audio").map((t) => t.node), outputIds);}diff --git a/src/lib/comfy.ts b/src/lib/comfy.tsindex 01c1ddb5..8639a357 100644--- a/src/lib/comfy.ts+++ b/src/lib/comfy.ts@@ -684,17 +684,39 @@ export function tagMetaBatch(wf: ComfyWorkflow, job: string): ComfyWorkflow {return out;}+/** Inputs that may go without sound: a VHS soundtrack link into them is dropped for a silent clip. */+const OPTIONAL_SOUND: Record<string, string[]> = { VHS_VideoCombine: ["audio"], CreateVideo: ["audio"], TextGenerate: ["audio"] };+/** Nodes that pad the sound they get to the loaded frames themselves: an empty track is enough (and never too long). */+const PADS_TO_FRAMES = new Set(["H3SoundtrackOrSilence"]);/**- * A clip with no sound: VHS_LoadVideo fails ("failed to extract audio") once its audio output is read, so- * the links from that output (VHS_VideoCombine.audio) are dropped. Only for clips whose header says so.+ * A clip with no sound: VHS_LoadVideo fails ("failed to extract audio") once its audio output is read, so no+ * link from that output is left. Where sound is optional (VHS_VideoCombine.audio) the link is dropped, and the+ * result has no sound track. Where a node needs sound (the Openjutsu graphs' AudioConcat, H3 Face Chunks) it+ * gets stereo silence `seconds` long (the clip's length; 1 s when unknown), and H3 Soundtrack or Silence an empty+ * track it pads to the loaded frames, so a silent clip runs and its result is silent. Only for clips whose header+ * says they have no sound.*/-export function dropVhsAudio(wf: ComfyWorkflow): ComfyWorkflow {+export function dropVhsAudio(wf: ComfyWorkflow, seconds?: number): ComfyWorkflow {const loaders = new Set(Object.entries(wf).filter(([, n]) => n.class_type === "VHS_LoadVideo").map(([id]) => id));if (!loaders.size) return wf;const out = cloneWorkflow(wf);+ const clipLength = seconds && Number.isFinite(seconds) && seconds > 0 ? Math.ceil(seconds * 1000) / 1000 : 1;+ const silences = new Map<number, string>();+ const silence = (length: number) => {+ let id = silences.get(length);+ if (id) return id;+ id = "silence";+ for (let k = 2; out[id]; k++) id = `silence${k}`;+ out[id] = { class_type: "EmptyAudio", inputs: { duration: length, sample_rate: 44100, channels: 2 }, _meta: { title: length ? "Silence as long as the clip" : "No sound (padded to the frames)" } };+ silences.set(length, id);+ return id;+ };for (const n of Object.values(out)) {- const a = n.inputs.audio;- if (isLinkValue(a) && loaders.has(a[0]) && a[1] === 2) delete n.inputs.audio;+ for (const [input, v] of Object.entries(n.inputs)) {+ if (!isLinkValue(v) || !loaders.has(v[0]) || v[1] !== 2) continue;+ if (OPTIONAL_SOUND[n.class_type]?.includes(input)) delete n.inputs[input];+ else n.inputs[input] = [silence(PADS_TO_FRAMES.has(n.class_type) ? 0 : clipLength), 0];+ }}return out;}diff --git a/tests/silent-clip-sound.test.ts b/tests/silent-clip-sound.test.tsnew file mode 100644index 00000000..7d7bdc37--- /dev/null+++ b/tests/silent-clip-sound.test.ts@@ -0,0 +1,151 @@+import { describe, expect, it, vi } from "vitest";+import type { GenInput, Operation } from "@/lib/catalog/types";+import type { ComfyWorkflow } from "@/lib/comfy";+import { dropVhsAudio } from "@/lib/comfy";+import { composeWorkflow, type ComposeCatalog } from "@/lib/comfy-compose";+import type { MediaInfo } from "@/lib/media-probe";+import { OPENJUTSU_WORKFLOWS } from "@/lib/openjutsu-graphs";++/**+ * A clip without sound on the Openjutsu graphs (Group swap, one by one too, Scene edit, From picture, Motion transfer,+ * Fix faces, Fix colours): VHS_LoadVideo stops with "failed to extract audio" once its sound output is read, so the+ * composed graph never reads it. Nodes that need sound get silence and the result is silent; a clip with sound keeps it.+ */++vi.setConfig({ testTimeout: 60_000 });++const { loadManifest, getWorkflow } = await import("@/server/providers/comfyui");+const catalog: ComposeCatalog = { has: (id) => loadManifest().workflows.some((w) => w.id === id), get: getWorkflow };++const clipFacts = (hasAudio: boolean): MediaInfo => ({+ container: "mp4",+ duration: 7.5,+ videoDuration: 7.5,+ width: 1280,+ height: 720,+ fps: 24,+ frames: 180,+ hasVideo: true,+ hasAudio,+ ...(hasAudio && { channels: 2 }),+});+const PICTURE: MediaInfo = { container: "image", width: 1024, height: 1024, hasVideo: false, hasAudio: false };++const isLink = (v: unknown): v is [string, number] => Array.isArray(v) && v.length === 2 && typeof v[0] === "string" && typeof v[1] === "number";+const vhsLoaders = (wf: ComfyWorkflow) => Object.keys(wf).filter((id) => wf[id].class_type === "VHS_LoadVideo");+/** Every input that reads a VHS loader's sound (output 2). */+const soundReads = (wf: ComfyWorkflow) => {+ const loaders = new Set(vhsLoaders(wf));+ return Object.entries(wf).flatMap(([id, n]) =>+ Object.entries(n.inputs)+ .filter(([, v]) => isLink(v) && loaders.has(v[0]) && v[1] === 2)+ .map(([input]) => `${n.class_type}.${input} (${id})`),+ );+};+const dangling = (wf: ComfyWorkflow) => Object.entries(wf).flatMap(([id, n]) => Object.entries(n.inputs).filter(([, v]) => isLink(v) && !wf[v[0]]).map(([k]) => `${id}.${k}`));++/** The run Genjutsu sends for each graph that loads the clip with VHS. */+const RUNS: { id: string; op: Operation; pictures: number; openjutsu: Record<string, unknown> }[] = [+ { id: OPENJUTSU_WORKFLOWS.group, op: "group-swap", pictures: 1, openjutsu: { pictures: 1 } },+ { id: OPENJUTSU_WORKFLOWS.groupOneByOne, op: "group-swap", pictures: 2, openjutsu: { pictures: 2, onePerPerson: true } },+ { id: OPENJUTSU_WORKFLOWS.scene, op: "scene-edit", pictures: 1, openjutsu: { action: "change", target: "jacket", becomes: "a cream silk blazer", picture: true } },+ { id: OPENJUTSU_WORKFLOWS.h3, op: "picture-video", pictures: 1, openjutsu: { renderer: "h3", seconds: 6 } },+ { id: OPENJUTSU_WORKFLOWS.motionH3, op: "motion-transfer", pictures: 1, openjutsu: { seconds: 6 } },+ { id: OPENJUTSU_WORKFLOWS.faces, op: "face-fix-video", pictures: 1, openjutsu: { faceStyle: "mouth" } },+ { id: OPENJUTSU_WORKFLOWS.colours, op: "tone-video", pictures: 0, openjutsu: {} },+];++async function compose(id: string, op: Operation, pictures: number, openjutsu: Record<string, unknown>, hasAudio: boolean) {+ const facts = new Map<string, MediaInfo>([["fuobe_clip.mp4", clipFacts(hasAudio)]]);+ const images = Array.from({ length: pictures }, (_, i) => {+ facts.set(`fuobe_pic${i}.png`, PICTURE);+ return `fuobe_pic${i}.png`;+ });+ const input: GenInput = { prompt: "Swap each person for the model in the pictures", seed: 7, numOutputs: 1, video: "fuobe_clip.mp4", ...(images.length && { images }), extra: { openjutsu } };+ const out = await composeWorkflow(id, input, {+ generationId: `silent-${id}`,+ op,+ catalog,+ sent: [],+ made: async (_d, _t, name) => name,+ media: (name) => facts.get(name),+ });+ return out.workflow;+}++describe("a clip without sound on the Openjutsu graphs", () => {+ it("every graph that loads the clip with VHS reads its sound somewhere (so a silent clip would stop it)", async () => {+ for (const r of RUNS.filter((r) => r.id !== OPENJUTSU_WORKFLOWS.colours)) {+ const wf = await compose(r.id, r.op, r.pictures, r.openjutsu, true);+ expect(vhsLoaders(wf).length, r.id).toBeGreaterThan(0);+ expect(soundReads(wf).length, r.id).toBeGreaterThan(0);+ }+ });++ it("never reads a silent clip's sound: needed sound becomes silence, optional sound goes, no link is left hanging", async () => {+ for (const r of RUNS) {+ const wf = await compose(r.id, r.op, r.pictures, r.openjutsu, false);+ expect(soundReads(wf), r.id).toEqual([]);+ expect(dangling(wf), r.id).toEqual([]);+ // No VHS_VideoCombine is left with the clip's sound, and the graph keeps its output.+ for (const n of Object.values(wf)) if (n.class_type === "EmptyAudio") expect(n.inputs.channels, r.id).toBe(2);+ }+ });++ it("Group swap one by one: the soundtrack AudioConcat gets silence as long as the clip", async () => {+ const wf = await compose(OPENJUTSU_WORKFLOWS.groupOneByOne, "group-swap", 2, { pictures: 2, onePerPerson: true }, false);+ const concat = Object.values(wf).find((n) => n.class_type === "AudioConcat" && isLink(n.inputs.audio2) && wf[(n.inputs.audio2 as [string, number])[0]]?.class_type === "EmptyAudio")!;+ expect(concat).toBeDefined();+ const first = wf[(concat.inputs.audio1 as [string, number])[0]];+ expect(first.class_type).toBe("EmptyAudio");+ expect(first.inputs.duration).toBe(7.5);+ });++ it("From picture and Motion transfer on H3: Soundtrack or Silence gets an empty track it pads to the loaded frames", async () => {+ for (const id of [OPENJUTSU_WORKFLOWS.h3, OPENJUTSU_WORKFLOWS.motionH3]) {+ const wf = await compose(id, id === OPENJUTSU_WORKFLOWS.h3 ? "picture-video" : "motion-transfer", 1, { seconds: 6 }, false);+ const node = Object.values(wf).find((n) => n.class_type === "H3SoundtrackOrSilence")!;+ const src = wf[(node.inputs.audio as [string, number])[0]];+ expect(src.class_type, id).toBe("EmptyAudio");+ expect(src.inputs.duration, id).toBe(0);+ }+ });++ it("a clip with sound keeps it: every graph reads the clip's own sound as before", async () => {+ for (const r of RUNS.filter((r) => r.id !== OPENJUTSU_WORKFLOWS.colours)) {+ const wf = await compose(r.id, r.op, r.pictures, r.openjutsu, true);+ expect(Object.values(wf).some((n) => n.class_type === "EmptyAudio" && n._meta?.title === "Silence as long as the clip"), r.id).toBe(false);+ }+ });+});++describe("dropVhsAudio", () => {+ const wf: ComfyWorkflow = {+ "1": { class_type: "VHS_LoadVideo", inputs: { video: "a.mp4" } },+ "2": { class_type: "VHS_VideoCombine", inputs: { images: ["1", 0], audio: ["1", 2] } },+ "3": { class_type: "AudioConcat", inputs: { audio1: ["1", 2], audio2: ["9", 0] } },+ "4": { class_type: "H3SoundtrackOrSilence", inputs: { audio: ["1", 2], video_info: ["1", 3] } },+ "5": { class_type: "H3FaceChunks", inputs: { audio: ["1", 2] } },+ "9": { class_type: "EmptyAudio", inputs: { duration: 1, sample_rate: 8000, channels: 2 } },+ };++ it("drops optional sound, gives needed sound one silence of the clip's length and the padding node an empty track", () => {+ const out = dropVhsAudio(wf, 4.2004);+ expect(out["2"].inputs.audio).toBeUndefined();+ expect(out["3"].inputs.audio1).toEqual(out["5"].inputs.audio);+ const long = out[(out["3"].inputs.audio1 as [string, number])[0]];+ expect(long).toMatchObject({ class_type: "EmptyAudio", inputs: { duration: 4.201, sample_rate: 44100, channels: 2 } });+ const empty = out[(out["4"].inputs.audio as [string, number])[0]];+ expect(empty).toMatchObject({ class_type: "EmptyAudio", inputs: { duration: 0 } });+ expect(out["4"].inputs.video_info).toEqual(["1", 3]);+ // The source graph is left as it was.+ expect(wf["2"].inputs.audio).toEqual(["1", 2]);+ });++ it("uses 1 s when the clip's length is unknown, and leaves a graph without VHS loaders alone", () => {+ const out = dropVhsAudio(wf);+ expect(out[(out["3"].inputs.audio1 as [string, number])[0]].inputs.duration).toBe(1);+ const none: ComfyWorkflow = { "1": { class_type: "LoadVideo", inputs: { file: "a.mp4" } } };+ expect(dropVhsAudio(none)).toBe(none);+ });+});