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
Click to Ad: the cloud end card gets a silent sound track; the server card uses the page's colour
Claude4b871653aced
diff --git a/src/lib/catalog/marketing.ts b/src/lib/catalog/marketing.tsindex faaa5f5f..7f827519 100644--- a/src/lib/catalog/marketing.ts+++ b/src/lib/catalog/marketing.ts@@ -592,7 +592,7 @@ export const CLICK_TO_AD = { aspect: "9:16", maxSeconds: 12, shots: 3 } as constexport const END_CARD = { seconds: 2.5, minSeconds: 2, maxSeconds: 3, zoomTo: 1.05, fps: 24, size: { w: 720, h: 1280 }, cta: "Shop now" } as const;export const END_CARD_MODELS = {- cloud: { art: "overlay-image", clip: "ken-burns", join: "join-clips" },+ cloud: { art: "overlay-image", clip: "ken-burns", sound: "merge-audio-video", join: "join-clips" },open: { append: "oss-append-endcard", clip: "oss-still-to-clip", join: "oss-join-sound1" },} as const;@@ -613,6 +613,11 @@ export interface EndCardInput {/** Cloud, no drawn card: the brand-colour background and the logo overlay-image puts on it. */background?: string;logo?: string;+ /**+ * Cloud: a silent WAV as long as the card (the server writes it). The card clip gets it as its sound track, so+ * both clips merge-videos joins carry sound and the ad's voiceover goes through the join.+ */+ silence?: string;seconds?: number;feature: string;title: string;@@ -627,9 +632,9 @@ export interface EndCardInput {/*** The steps that make the end card and put it after the ad, and the key of the finished video. Cloud:- * [overlay-image: the logo on the brand colour →] ken-burns (zoom 1 → 1.05) → merge-videos after the ad.- * Open: video-append-endcard (the ad's sound kept, padded with silence under the card), or still-to-clip +- * oss-join-sound1 for a clip the open engine didn't make.+ * [overlay-image: the logo on the brand colour →] ken-burns (zoom 1 → 1.05) → a silent sound track under it+ * (merge-audio-video) → merge-videos after the ad. Open: video-append-endcard (the ad's sound kept, padded with+ * silence under the card), or still-to-clip + oss-join-sound1 for a clip the open engine didn't make.*/export function endCardSteps(o: EndCardInput): { steps: EndCardStep[]; final: string } {const seconds = Math.max(END_CARD.minSeconds, Math.min(END_CARD.maxSeconds, o.seconds ?? END_CARD.seconds));@@ -667,6 +672,7 @@ export function endCardSteps(o: EndCardInput): { steps: EndCardStep[]; final: st};}const M = END_CARD_MODELS.cloud;+ if (!o.silence) throw new Error("The end card needs its silent sound track");const steps: EndCardStep[] = [];if (!o.card) {if (!o.background || !o.logo) throw new Error("Add a logo to the brand kit for the end card, or turn End card off");@@ -679,11 +685,20 @@ export function endCardSteps(o: EndCardInput): { steps: EndCardStep[]; final: strequest: { ...helper, modelId: M.clip, op: "still-motion", ...(o.card ? { inputs: { images: [o.card] } } : {}), params: move },...(o.card ? {} : { after: ["endcardArt"], chain: [{ slot: "images" as const }] }),});+ // The card's clip has no sound: give it silence, so merge-videos joins two clips that both have sound (whether it+ // keeps the first clip's voiceover when the second has no audio stream at all is unknown).+ steps.push({+ key: "endcardSound",+ label: "End card sound",+ request: { ...helper, modelId: M.sound, op: "mux", inputs: { audio: o.silence } },+ after: ["endcardClip"],+ chain: [{ slot: "video" }],+ });steps.push({key: "endcard",label: "Adding the end card",request: { ...result, modelId: M.join, op: "concat", prompt: o.title, params: { aspectRatio: o.aspectRatio } },- after: [o.ad, "endcardClip"],+ after: [o.ad, "endcardSound"],chain: [{ slot: "videos" }, { slot: "videos" }],});return { steps, final: "endcard" };diff --git a/src/server/marketing.ts b/src/server/marketing.tsindex 118982f0..ede3e225 100644--- a/src/server/marketing.ts+++ b/src/server/marketing.ts@@ -43,6 +43,7 @@ import {} from "@/lib/catalog/marketing";import { routesFor } from "@/lib/catalog/plan";import { probeMedia } from "@/lib/media-probe";+import { themeFrom } from "@/components/ads/layout";import type { ChainSlot, Engine, GenerationRequest, ModelDef, Operation } from "@/lib/catalog/types";import { getBrand, getProduct } from "./brand";import { createPipeline, type PipelineStep } from "./chains";@@ -656,19 +657,31 @@ export function clickToAdSeconds(engine: Engine, presenter: boolean): { voice: n/** A raster logo overlay-image can place (an SVG can't be read there), else undefined. */const rasterLogo = (brand?: BrandKit | null) => (brand?.logo && !isSvg(brand.logo) ? brand.logo : undefined);+/**+ * Who encoded a step's clip: video-append-endcard copies the ad's packets, so it only takes a clip ComfyUI made+ * (every open Click to Ad final today); anything else gets still-to-clip + the sound-keeping join.+ */+export function adEncoder(step: Pick<Step, "request"> | undefined): "comfyui" | "other" {+ const m = step && getModel(step.request.modelId);+ const op = step?.request.op;+ return m && op && routesFor(m, op).length && routesFor(m, op).every((r) => r.provider === "comfyui") ? "comfyui" : "other";+}+/*** The ad, then its logo end card: the ad's last step becomes a helper and the card's join is the result- * (endCardSteps: cloud ken-burns + merge-videos, open video-append-endcard).+ * (endCardSteps: cloud ken-burns + a silent track + merge-videos, open video-append-endcard).*/-function withEndCard(plan: RunPlan, input: ClickToAdInput, ctx: { brand?: BrandKit | null; background?: string }): RunPlan {+function withEndCard(plan: RunPlan, input: ClickToAdInput, ctx: { brand?: BrandKit | null; background?: string; silence?: string }): RunPlan {if (!input.endCard) return plan;const ad = plan.finals[0];const card = endCardSteps({engine: plan.engine,ad,+ adEncodedBy: plan.engine === "open" ? adEncoder(plan.steps.find((s) => s.key === ad)) : undefined,card: input.endCard.image,background: ctx.background,logo: rasterLogo(ctx.brand),+ silence: ctx.silence,seconds: input.endCard.seconds,feature: plan.feature,title: plan.title,@@ -686,7 +699,7 @@ function withEndCard(plan: RunPlan, input: ClickToAdInput, ctx: { brand?: BrandKreturn { ...plan, steps: [...steps, ...card.steps], finals: [card.final], meta: { ...plan.meta, endCard: input.endCard.image ?? null }, notes };}-export function planClickToAd(input: ClickToAdInput, ctx: { product: Product; brand?: BrandKit | null; script: string; background?: string }): RunPlan {+export function planClickToAd(input: ClickToAdInput, ctx: { product: Product; brand?: BrandKit | null; script: string; background?: string; silence?: string }): RunPlan {try {return withEndCard(planAd(input, ctx), input, ctx);} catch (e) {@@ -1072,12 +1085,15 @@ export function estimateClickToAd(input: ClickToAdInput): RunEstimate {const ctx = context({ productId: input.productId });const presenter = input.presenter ?? styleById(input.styleId)?.presenter ?? false;const script = input.script?.trim() || Array(wordBudget(clickToAdSeconds(input.engine, presenter).voice)).fill("word").join(" ");- return estimatePlan(planClickToAd(input, { product: ctx.product!, brand: ctx.brand, script, background: "/api/files/placeholder.png" }));+ return estimatePlan(planClickToAd(input, { product: ctx.product!, brand: ctx.brand, script, background: "/api/files/placeholder.png", silence: "/api/files/placeholder.wav" }));}-/** The end card's background when the page drew none: the kit's main colour at the card's size (cloud: the logo goes on it). */-function cardBackground(brand?: BrandKit | null): string {- const hex = brand?.colors.find((c) => /^#[0-9a-f]{6}$/i.test(c)) ?? "#141414";+/**+ * The end card's background when the page drew none: the kit's accent at the card's size (cloud: the logo goes on+ * it), the colour the page's card uses (themeFrom), so a white logo isn't put on a white first colour.+ */+export function cardBackground(brand?: BrandKit | null): string {+ const hex = themeFrom(brand).accent;const [r, g, b] = [1, 3, 5].map((i) => parseInt(hex.slice(i, i + 2), 16));const { w, h } = END_CARD.size;const data = Buffer.alloc(w * h * 4);@@ -1085,6 +1101,32 @@ function cardBackground(brand?: BrandKit | null): string {return saveFile({ data: encodePng({ width: w, height: h, data }), contentType: "image/png", source: "generation", name: "end-card-background.png" }).url;}+/** A silent 16-bit stereo WAV `seconds` long (44.1 kHz). */+export function silentWav(seconds: number, rate = 44100): Buffer {+ const frames = Math.round(seconds * rate);+ const bytes = frames * 4;+ const buf = Buffer.alloc(44 + bytes);+ buf.write("RIFF", 0);+ buf.writeUInt32LE(36 + bytes, 4);+ buf.write("WAVEfmt ", 8);+ buf.writeUInt32LE(16, 16);+ buf.writeUInt16LE(1, 20);+ buf.writeUInt16LE(2, 22);+ buf.writeUInt32LE(rate, 24);+ buf.writeUInt32LE(rate * 4, 28);+ buf.writeUInt16LE(4, 32);+ buf.writeUInt16LE(16, 34);+ buf.write("data", 36);+ buf.writeUInt32LE(bytes, 40);+ return buf;+}++/** The cloud end card's sound track: silence as long as the card. */+function cardSilence(seconds?: number): string {+ const s = Math.max(END_CARD.minSeconds, Math.min(END_CARD.maxSeconds, seconds ?? END_CARD.seconds));+ return saveFile({ data: silentWav(s), contentType: "audio/wav", source: "generation", name: "end-card-silence.wav" }).url;+}+export async function clickToAdScript(input: Pick<ClickToAdInput, "engine" | "productId" | "styleId" | "presenter" | "instructions">): Promise<{ script: string; by: "llm" | "rules"; seconds: number }> {const ctx = context({ productId: input.productId });const style = styleById(input.styleId) ?? AD_STYLES[0];@@ -1099,10 +1141,17 @@ export async function runClickToAd(input: ClickToAdInput): Promise<{ run: Marketconst presenter = input.presenter ?? styleById(input.styleId)?.presenter ?? false;const seconds = clickToAdSeconds(input.engine, presenter).voice;const script = input.script?.trim() ? fitScript(input.script, seconds) : (await clickToAdScript(input)).script;- // Checked first with a stand-in, so a plan that can't run writes no file.- const fallback = input.endCard && !input.endCard.image && input.engine === "cloud";- if (fallback) planClickToAd(input, { product: ctx.product, brand: ctx.brand, script, background: "/api/files/placeholder.png" });- const plan = planClickToAd(input, { product: ctx.product, brand: ctx.brand, script, background: fallback ? cardBackground(ctx.brand) : undefined });+ // Checked first with stand-ins, so a plan that can't run writes no file.+ const cloudCard = !!input.endCard && input.engine === "cloud";+ const fallback = cloudCard && !input.endCard?.image;+ if (cloudCard) planClickToAd(input, { product: ctx.product, brand: ctx.brand, script, background: "/api/files/placeholder.png", silence: "/api/files/placeholder.wav" });+ const plan = planClickToAd(input, {+ product: ctx.product,+ brand: ctx.brand,+ script,+ background: fallback ? cardBackground(ctx.brand) : undefined,+ silence: cloudCard ? cardSilence(input.endCard?.seconds) : undefined,+ });estimatePlan(plan);return { run: launch(plan, { ...input, script }), script };}diff --git a/tests/marketing-endcard.test.ts b/tests/marketing-endcard.test.tsindex 95854b23..7fb16cee 100644--- a/tests/marketing-endcard.test.ts+++ b/tests/marketing-endcard.test.ts@@ -21,7 +21,8 @@ process.env.OLLAMA_URL = "http://127.0.0.1:9";const { setSetting } = await import("@/server/settings");const mk = await import("@/server/marketing");const gens = await import("@/server/generations");-const { saveFile } = await import("@/server/storage");+const { readRef, saveFile } = await import("@/server/storage");+const { decodePng } = await import("@/server/png");const { prepareWorkflow } = await import("@/server/providers/comfyui");const { getModel } = await import("@/lib/catalog");const { END_CARD, END_CARD_MODELS, endCardSteps } = await import("@/lib/catalog/marketing");@@ -58,10 +59,10 @@ describe("the end card's steps per engine", () => {expect(getModel("oss-still-to-clip")!.routes).toEqual([expect.objectContaining({ op: "still-motion", provider: "comfyui", endpoint: "still-to-clip" })]);});- it("cloud: the drawn card → a 2.5 s push-in (zoom 1 → 1.05) → merge-videos after the ad", () => {- const { steps, final } = endCardSteps({ ...base, engine: "cloud", card: "/api/files/card.png" });+ it("cloud: the drawn card → a 2.5 s push-in (zoom 1 → 1.05) → a silent track under it → merge-videos after the ad", () => {+ const { steps, final } = endCardSteps({ ...base, engine: "cloud", card: "/api/files/card.png", silence: "/api/files/silence.wav" });expect(final).toBe("endcard");- expect(steps.map((s) => s.key)).toEqual(["endcardClip", "endcard"]);+ expect(steps.map((s) => s.key)).toEqual(["endcardClip", "endcardSound", "endcard"]);expect(steps[0].request).toMatchObject({feature: "click-to-ad-step",modelId: "ken-burns",@@ -69,16 +70,19 @@ describe("the end card's steps per engine", () => {inputs: { images: ["/api/files/card.png"] },params: { duration: 2.5, fps: 24, zoomFrom: 1, zoomTo: 1.05, easing: "ease-in-out" },});- expect(steps[1]).toMatchObject({ after: ["captions", "endcardClip"], chain: [{ slot: "videos" }, { slot: "videos" }], request: { feature: "click-to-ad", title: base.title, modelId: "join-clips", op: "concat" } });+ // Both clips merge-videos joins carry sound, so the ad's voiceover goes through the join.+ expect(steps[1]).toMatchObject({ after: ["endcardClip"], chain: [{ slot: "video" }], request: { feature: "click-to-ad-step", modelId: "merge-audio-video", op: "mux", inputs: { audio: "/api/files/silence.wav" } } });+ expect(steps[2]).toMatchObject({ after: ["captions", "endcardSound"], chain: [{ slot: "videos" }, { slot: "videos" }], request: { feature: "click-to-ad", title: base.title, modelId: "join-clips", op: "concat" } });+ expect(() => endCardSteps({ ...base, engine: "cloud", card: "/api/files/card.png" })).toThrow(/needs its silent sound track/);});it("cloud without a drawn card: overlay-image puts the logo on the brand colour first", () => {- const { steps } = endCardSteps({ ...base, engine: "cloud", background: "/api/files/bg.png", logo: "/api/files/logo.png", seconds: 9 });- expect(steps.map((s) => s.key)).toEqual(["endcardArt", "endcardClip", "endcard"]);+ const { steps } = endCardSteps({ ...base, engine: "cloud", background: "/api/files/bg.png", logo: "/api/files/logo.png", seconds: 9, silence: "/api/files/silence.wav" });+ expect(steps.map((s) => s.key)).toEqual(["endcardArt", "endcardClip", "endcardSound", "endcard"]);expect(steps[0].request).toMatchObject({ modelId: "overlay-image", op: "overlay", inputs: { images: ["/api/files/bg.png", "/api/files/logo.png"] }, params: { position: { x: 0.5, y: 0.45 }, scale: 1 } });expect(steps[1]).toMatchObject({ after: ["endcardArt"], chain: [{ slot: "images" }], request: { params: { duration: 3 } } });expect(steps[1].request.inputs).toBeUndefined();- expect(() => endCardSteps({ ...base, engine: "cloud" })).toThrow(/Add a logo to the brand kit/);+ expect(() => endCardSteps({ ...base, engine: "cloud", silence: "/api/files/silence.wav" })).toThrow(/Add a logo to the brand kit/);});it("open: the card appended to the ad keeping its sound, or still-to-clip + the join that keeps the first clip's sound", () => {@@ -101,17 +105,17 @@ describe("the end card's steps per engine", () => {describe("Click to Ad with an end card", () => {it("ends the product-only ad on the card: captions become a helper, the card's join is the result", () => {const card = upload("card.png");- const plan = mk.planClickToAd({ engine: "cloud", productId: "p1", styleId: "clean-minimal", endCard: { image: card } }, { product: product(), script: SCRIPT });+ const plan = mk.planClickToAd({ engine: "cloud", productId: "p1", styleId: "clean-minimal", endCard: { image: card } }, { product: product(), script: SCRIPT, silence: "/api/files/silence.wav" });expect(plan.finals).toEqual(["endcard"]);const by = Object.fromEntries(plan.steps.map((s) => [s.key, s]));expect(by.captions.request.feature).toBe("click-to-ad-step");expect(by.captions.request.title).toBeUndefined();- expect(by.endcard).toMatchObject({ after: ["captions", "endcardClip"], request: { feature: "click-to-ad", title: plan.title } });+ expect(by.endcard).toMatchObject({ after: ["captions", "endcardSound"], request: { feature: "click-to-ad", title: plan.title } });expect(plan.meta.endCard).toBe(card);expect(plan.notes).toContain("Ends on your logo card for 2.5 s");- // Prices: the card's two fal steps are about free; every step is valid.+ // Prices: the card's three fal steps are about free; every step is valid.const est = mk.estimatePlan(plan);- expect(est.steps.map((s) => s.key).slice(-2)).toEqual(["endcardClip", "endcard"]);+ expect(est.steps.map((s) => s.key).slice(-3)).toEqual(["endcardClip", "endcardSound", "endcard"]);});it("no card asked for: the plan is as before", () => {@@ -122,14 +126,44 @@ describe("Click to Ad with an end card", () => {it("cloud, no drawn card: the kit's raster logo on the background; an SVG logo can't be placed", () => {const logo = upload("logo.png");- const plan = mk.planClickToAd({ engine: "cloud", productId: "p1", styleId: "clean-minimal", endCard: {} }, { product: product(), brand: kit({ logo }), script: SCRIPT, background: "/api/files/bg.png" });+ const plan = mk.planClickToAd({ engine: "cloud", productId: "p1", styleId: "clean-minimal", endCard: {} }, { product: product(), brand: kit({ logo }), script: SCRIPT, background: "/api/files/bg.png", silence: "/api/files/silence.wav" });expect(plan.steps.find((s) => s.key === "endcardArt")!.request.inputs).toEqual({ images: ["/api/files/bg.png", logo] });expect(plan.notes.join(" ")).toMatch(/the kit's logo on its main colour/);- expect(() => mk.planClickToAd({ engine: "cloud", productId: "p1", styleId: "clean-minimal", endCard: {} }, { product: product(), brand: kit({ logo: "/api/files/logo.svg" }), script: SCRIPT, background: "/api/files/bg.png" })).toThrow(+ expect(() => mk.planClickToAd({ engine: "cloud", productId: "p1", styleId: "clean-minimal", endCard: {} }, { product: product(), brand: kit({ logo: "/api/files/logo.svg" }), script: SCRIPT, background: "/api/files/bg.png", silence: "/api/files/silence.wav" })).toThrow(/Add a logo to the brand kit for the end card, or turn End card off/,);});+ it("cloud, no drawn card: the background is the colour the page's card uses, not the kit's first colour", async () => {+ // A white first colour under a white logo would hide it; the page's card takes the kit's accent.+ const url = mk.cardBackground(kit({ colors: ["#ffffff", "#0a7d4f"] }));+ const png = decodePng((await readRef(url)).data);+ expect([png.width, png.height]).toEqual([END_CARD.size.w, END_CARD.size.h]);+ expect([...png.data.subarray(0, 4)]).toEqual([0x0a, 0x7d, 0x4f, 255]);+ expect(themeFrom({ colors: ["#ffffff", "#0a7d4f"] }).accent).toBe("#0a7d4f");+ });++ it("cloud: the card's silent sound track is a WAV as long as the card", () => {+ const wav = mk.silentWav(2.5);+ expect(wav.subarray(0, 4).toString()).toBe("RIFF");+ expect(wav.readUInt16LE(22)).toBe(2);+ expect(wav.readUInt32LE(24)).toBe(44100);+ expect(wav.readUInt32LE(40) / (44100 * 4)).toBeCloseTo(2.5, 5);+ expect(wav.subarray(44).every((b) => b === 0)).toBe(true);+ });++ it("open: reads who encodes the ad from its last step (append for ComfyUI, still-to-clip + join for anything else)", () => {+ const card = upload("card.png");+ const plan = mk.planClickToAd({ engine: "open", productId: "p1", styleId: "clean-minimal", voiceSample: upload("v.png"), endCard: { image: card } }, { product: product(), script: SCRIPT });+ // Every open Click to Ad ends on a ComfyUI step today: the card is appended to it.+ expect(plan.steps.at(-1)!.request.modelId).toBe("oss-append-endcard");+ expect(mk.adEncoder({ request: { feature: "t", modelId: "oss-captions-burnin", op: "captions" } })).toBe("comfyui");+ // A clip made in the browser (or by anything but ComfyUI) can't have its packets copied.+ expect(mk.adEncoder({ request: { feature: "t", modelId: "local-ad-compose", op: "edit" } })).toBe("other");+ expect(mk.adEncoder({ request: { feature: "t", modelId: "no-such-model", op: "concat" } })).toBe("other");+ expect(mk.adEncoder(undefined)).toBe("other");+ });+it("open: appended after the presenter film (ComfyUI-encoded), and needs the drawn card", () => {const card = upload("card.png");const plan = mk.planClickToAd({ engine: "open", productId: "p1", styleId: "simple-ugc", voiceSample: upload("v.png"), endCard: { image: card, seconds: 3 } }, { product: product(), script: SCRIPT });