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
Edit speech: a video's sound stays in step with the picture
Claudeab5eb9974d9f
diff --git a/src/components/audio-tools/speech-edit-tool.tsx b/src/components/audio-tools/speech-edit-tool.tsxindex 636c7f46..41c47c9b 100644--- a/src/components/audio-tools/speech-edit-tool.tsx+++ b/src/components/audio-tools/speech-edit-tool.tsx@@ -32,6 +32,7 @@ import {PACES,PITCH_STEPS,PROMPT_HELPER_NOTE,+ QUALITY_HINTS,SPEECH_EDIT_FEATURE,VOLUME_STEPS,type AukQuality,@@ -277,6 +278,8 @@ export function SpeechEditTool({ tabs }: { tabs?: React.ReactNode }) {const take = useCallback((s: Source, given?: Cue[], line?: number) => {setSource(s);+ // A video keeps its length (Pace would put the sound out of step with the picture).+ if (s.kind === "video") setEffect((e) => (e.kind === "pace" ? defaultEffect("pitch") : e));setResultId(null);setVideoId(null);setTranscriptId(null);@@ -419,7 +422,7 @@ export function SpeechEditTool({ tabs }: { tabs?: React.ReactNode }) {</p>{view === "sound" && (<div className="mb-3 space-y-2 rounded-xl bg-muted/50 p-2.5">- <Segmented label="Effect" value={effect.kind} onChange={(k) => setEffect(defaultEffect(k))} options={EFFECTS.map((k) => ({ value: k, label: EFFECT_LABELS[k] }))} />+ <Segmented label="Effect" value={effect.kind} onChange={(k) => setEffect(defaultEffect(k))} options={EFFECTS.filter((k) => k !== "pace" || source.kind !== "video").map((k) => ({ value: k, label: EFFECT_LABELS[k] }))} /><EffectOptions effect={effect} onChange={setEffect} /></div>)}@@ -446,8 +449,8 @@ export function SpeechEditTool({ tabs }: { tabs?: React.ReactNode }) {</ol><div className="mt-3 flex flex-wrap items-center gap-2"><span className="w-16 shrink-0 text-[13px]">Quality</span>- <Segmented label="Quality" value={quality} onChange={setQuality} options={(["fast", "best"] as const).map((q) => ({ value: q, label: AUK_SETTINGS[q].label.replace(/ \(.*\)$/, "") }))} />- <span className="text-[12px] text-muted-foreground">{quality === "fast" ? "AuK-Flash, 4 steps" : "Full AuK, 32 steps: slower, a little closer to the voice"}</span>+ <Segmented label="Quality" value={quality} onChange={setQuality} options={(["fast", "best"] as const).map((q) => ({ value: q, label: AUK_SETTINGS[q].label }))} />+ <span className="text-[12px] text-muted-foreground">{QUALITY_HINTS[quality]}</span></div></Panel>)}diff --git a/src/lib/speech-edit.ts b/src/lib/speech-edit.tsindex 281a1c59..e6f31333 100644--- a/src/lib/speech-edit.ts+++ b/src/lib/speech-edit.ts@@ -107,6 +107,8 @@ export interface TextChange {from: number;to: number;words: string[];+ /** Where `words` start in the new text (set by diffWords; lets merged changes take the words between them as edited). */+ at?: number;}/**@@ -143,11 +145,11 @@ export function diffWords(old: string[], next: string[]): TextChange[] {i++;j++;} else if (j < m && (i >= n || dp[i][j + 1] >= dp[i + 1][j])) {- open ??= { from: head + i, to: head + i, words: [] };+ open ??= { from: head + i, to: head + i, words: [], at: head + j };open.words.push(next[head + j]);j++;} else {- open ??= { from: head + i, to: head + i, words: [] };+ open ??= { from: head + i, to: head + i, words: [], at: head + j };open.to = head + i + 1;i++;}@@ -244,13 +246,15 @@ export const EFFECT_LABELS: Record<EffectKind, string> = {isolate: "Keep only this speaker",};-/** AuK's instruction for an effect on a window (Cookbook §3–§5); `said` is what the window's speaker says (Keep only this speaker). */+/**+ * AuK's instruction for an effect on a window, worded exactly as its Cookbook's templates (§3–§5: "by {1/2/3} semitones",+ * "to {0.5/0.75/1.25/1.5/2.0}x"); `said` is what the window's speaker says (Keep only this speaker). */export function effectInstruction(e: SpeechEffect, said = ""): string {switch (e.kind) {case "pitch":- return `${e.semitones > 0 ? "Raise" : "Lower"} the pitch by ${Math.abs(e.semitones)} semitone${Math.abs(e.semitones) === 1 ? "" : "s"}.`;+ return `${e.semitones > 0 ? "Raise" : "Lower"} the pitch by ${Math.abs(e.semitones)} semitones.`;case "pace":- return `Adjust the speech speed to ${e.speed}x.`;+ return `Adjust the speech speed to ${Number.isInteger(e.speed) ? e.speed.toFixed(1) : e.speed}x.`;case "volume":return `${e.db > 0 ? "Increase" : "Decrease"} the volume by ${Math.abs(e.db)} dB.`;case "emotion":@@ -337,6 +341,13 @@ export interface PlanInput {words: Word[];/** Seconds the recording runs. */duration: number;+ /** The edited text's words (lets two merged changes keep the edited words between them, with their punctuation). */+ next?: string[];+ /**+ * Every window keeps its old length (a video's sound, which must stay in step with the picture): AuK fits the new words+ * into the same time, and Pace, which only changes the length, is refused.+ */+ keepLength?: boolean;}/**@@ -368,19 +379,22 @@ function spanOf(words: Word[], c: TextChange, duration: number): [number, number}/** Merge two changes into one that spans both (the words between them unchanged on both sides). */-function mergeChanges(words: Word[], a: TextChange, b: TextChange): TextChange {+function mergeChanges(words: Word[], a: TextChange, b: TextChange, next?: string[]): TextChange {const [x, y] = a.from <= b.from ? [a, b] : [b, a];- const between = words.slice(x.to, y.from).map((w) => w.text);- return { from: x.from, to: Math.max(x.to, y.to), words: [...x.words, ...between, ...y.words] };+ // The words between, as edited (they match the old ones but for case and punctuation, which the new text decides).+ const edited = next && x.at !== undefined && y.at !== undefined ? next.slice(x.at + x.words.length, y.at) : null;+ const between = edited && edited.length === y.from - x.to ? edited : words.slice(x.to, y.from).map((w) => w.text);+ return { from: x.from, to: Math.max(x.to, y.to), words: [...x.words, ...between, ...y.words], ...(x.at !== undefined && { at: x.at }) };}/*** Text edits → AuK windows, one per change (changes whose windows would overlap or nearly touch become one), in time* order. Each window's new length is its old length plus the change in the words' time: the difference in characters at the- * speaker's rate for a replacement, the new words' time for an insertion, the old words' time for a removal. Throws (in words the page shows) when a change is too long for one AuK call or there are too many.+ * speaker's rate for a replacement, the new words' time for an insertion, the old words' time for a removal (with+ * keepLength, a video's, the old length as it is). Throws (in words the page shows) when a change is too long for one AuK call or there are too many.*/export function planTextWindows(input: PlanInput, changes: TextChange[]): SpeechWindow[] {- const { words, duration } = input;+ const { words, duration, keepLength } = input;if (!changes.length) return [];const rate = speakingRate(words);const cuts = cutsOf(words, duration);@@ -390,7 +404,7 @@ export function planTextWindows(input: PlanInput, changes: TextChange[]): Speech// A replacement keeps the old words' real length and adds or takes the difference in characters at the speaker's// rate; an insertion adds the new words' time, a removal takes the old words' time away.const oldText = texts.slice(c.from, c.to).join(" ");- const delta = c.from === c.to ? sayLength(c.words.join(" "), rate) : c.words.length ? round3((c.words.join(" ").length - oldText.length) * rate) : -(e - s);+ const delta = keepLength ? 0 : c.from === c.to ? sayLength(c.words.join(" "), rate) : c.words.length ? round3((c.words.join(" ").length - oldText.length) * rate) : -(e - s);// The window's old length L and new length L + delta must fit together: 2L + delta ≤ max.const budget = windowBudget(1, Math.max(0, delta));const w = windowAround(s, e, cuts, duration, budget);@@ -401,7 +415,7 @@ export function planTextWindows(input: PlanInput, changes: TextChange[]): Speechfor (let k = 0; k < list.length - 1; ) {const [p, n] = [list[k], list[k + 1]];if (p.w && n.w && n.w.start - p.w.end < 2 * SPEECH_CROSSFADE) {- list.splice(k, 2, build(mergeChanges(words, p.c, n.c)));+ list.splice(k, 2, build(mergeChanges(words, p.c, n.c, input.next)));if (k > 0) k--;} else k++;}@@ -420,6 +434,9 @@ export function planTextWindows(input: PlanInput, changes: TextChange[]): Speechreturn out;}+/** Why a video has no Pace: a faster or slower line changes the sound's length, and the picture would no longer match it. */+export const PACE_ON_VIDEO_PROBLEM = "Pace changes how long the line runs, so on a video the sound would drift from the picture. Change the pace on the audio alone, or pick another effect.";+/*** An effect on [start, end] → AuK windows: the range widened to the nearest silence (within half a second, else a word* boundary), then split at silences into pieces short enough for one call each (same-length effects up to about 14.7 s,@@ -427,6 +444,7 @@ export function planTextWindows(input: PlanInput, changes: TextChange[]): Speech*/export function planEffectWindows(input: PlanInput, range: { start: number; end: number }, effect: SpeechEffect): SpeechWindow[] {const { words, duration } = input;+ if (input.keepLength && effect.kind === "pace") throw new SpeechEditError(PACE_ON_VIDEO_PROBLEM);const s0 = Math.max(0, Math.min(range.start, range.end));const e0 = Math.min(duration, Math.max(range.start, range.end));if (!(e0 - s0 >= 0.3)) throw new SpeechEditError("Pick at least a third of a second to change");@@ -515,8 +533,8 @@ export function mapTime(windows: SpeechWindow[], t: number): number {/** AuK checkpoints: Flash (distilled, 4 steps, no guidance) by default, the base model for the best take. */export type AukQuality = "fast" | "best";export const AUK_SETTINGS: Record<AukQuality, { checkpoint: string; steps: number; cfg: number; label: string }> = {- fast: { checkpoint: "ckpts/AuK-Flash/auk_flash.safetensors", steps: 4, cfg: 0, label: "Fast (AuK-Flash)" },- best: { checkpoint: "ckpts/AuK/auk_base.safetensors", steps: 32, cfg: 2, label: "Best (AuK)" },+ fast: { checkpoint: "ckpts/AuK-Flash/auk_flash.safetensors", steps: 4, cfg: 0, label: "Fast" },+ best: { checkpoint: "ckpts/AuK/auk_base.safetensors", steps: 32, cfg: 2, label: "Best" },};/** Why the cloud engine has no Edit speech (and what Engine mode "Paid APIs only" says). */@@ -531,6 +549,12 @@ export const PROMPT_HELPER_NOTE =/** AuK's languages, from its technical report (arXiv 2609.08936: a bilingual Chinese and English corpus). */export const AUK_LANGUAGES = ["English", "Chinese"];+/** What each quality setting means, in the page's words. */+export const QUALITY_HINTS: Record<AukQuality, string> = {+ fast: "The quickest take, in the same voice",+ best: "Slower, a little closer to the voice",+};+export const engineProblem = (engine: Engine): string | null => (engine === "cloud" ? CLOUD_SPEECH_EDIT_PROBLEM : null);/** A changed word shown on camera: the result can say so and offer Lipsync, with the times it should cover. */@@ -544,6 +568,8 @@ export function onCameraWarning(kind: "audio" | "video", windows: SpeechWindow[]/** The longest line AuK says in one call: its 30 s holds a voice sample and the line together. */export const AUK_TTS_MAX_SECONDS = 30;+/** The longest voice sample taken: it leaves about 9.5 s for the line. */+export const AUK_TTS_MAX_SAMPLE = 20;/*** AuK's text-to-speech instruction (Cookbook §1): with a voice sample, "Say the following with the same voice"; without@@ -559,8 +585,8 @@ export function ttsInstruction(text: string, o: { hasVoice: boolean; voiceDescri/** How long AuK should make a line, and the room its 30 s leaves (a voice sample of `sample` seconds takes the rest). */export function ttsLength(text: string, sample = 0): number {const want = Math.max(1, sayLength(text, DEFAULT_SECONDS_PER_CHAR) + 0.4);+ if (sample > AUK_TTS_MAX_SAMPLE) throw new SpeechEditError(`That voice sample is too long: use one under ${AUK_TTS_MAX_SAMPLE} seconds`);const room = AUK_SEQUENCE_MAX - SEQUENCE_MARGIN - sample;- if (room < 1) throw new SpeechEditError("That voice sample is too long for AuK: use one under 20 seconds");if (want > room) throw new SpeechEditError(`AuK says up to about ${Math.floor(room)} s per line${sample ? " with this voice sample" : ""}: split the line`);return round3(want);}@@ -595,18 +621,20 @@ export interface SpeechEditPlan extends ToolPlan {}/** The windows a request makes (word edits, else an effect on a range); throws what's in the way, in words the page shows. */-export function speechWindowsFor(i: Pick<SpeechEditInput, "cues" | "edited" | "range" | "effect" | "seconds">): SpeechWindow[] {+export function speechWindowsFor(i: Pick<SpeechEditInput, "cues" | "edited" | "range" | "effect" | "seconds"> & { kind?: SpeechEditInput["kind"] }): SpeechWindow[] {const duration = i.seconds ?? Math.max(0, ...i.cues.map((c) => c.end));if (!(duration > 0)) throw new SpeechEditError("Couldn't tell how long the recording runs: add it again");const words = wordsFromCues(i.cues);+ // A video's sound must keep its length: the picture is copied as it is, so every word after a change stays in step.+ const keepLength = i.kind === "video";if (i.effect) {if (!i.range) throw new SpeechEditError("Pick the part to change");- return planEffectWindows({ words, duration }, i.range, i.effect);+ return planEffectWindows({ words, duration, keepLength }, i.range, i.effect);}if (!i.edited) throw new SpeechEditError("Edit the words, or pick a range and an effect");const changes = diffLines(i.cues, i.edited);if (!changes.length) throw new SpeechEditError("Nothing changed yet: edit a line's words first");- return planTextWindows({ words, duration }, changes);+ return planTextWindows({ words, duration, keepLength, next: i.edited.flatMap(splitWords) }, changes);}/** One AuK job for every window (and, on a video, the stream-copy mux that puts the new sound on the picture). */@@ -662,7 +690,8 @@ export function planSpeechEdit(i: SpeechEditInput): SpeechEditPlan {summary: `${windows.length === 1 ? "One part" : `${windows.length} parts`} of the ${video ? "video's sound" : "recording"} ${i.effect ? "changed" : "re-spoken"} by AuK in the same voice and crossfaded back${video ? ", then put back on the picture (copied as it is)" : ""}.`,notes: ["Each part keeps at least a second of the untouched recording around it, so the new words match the voice and the room.",- ...(i.quality === "best" ? [] : ["Fast uses AuK-Flash (4 steps). Best uses the full AuK model: slower, a little closer to the voice."]),+ ...(video ? ["On a video every changed part keeps its length, so the sound stays in step with the picture: the new words are fitted into the same time."] : []),+ ...(i.quality === "best" ? [] : ["Best quality is slower and a little closer to the voice."]),...(warning ? [warning] : []),],};diff --git a/src/server/assist/studio-tools.ts b/src/server/assist/studio-tools.tsindex 1dc53a3d..f6a148d8 100644--- a/src/server/assist/studio-tools.ts+++ b/src/server/assist/studio-tools.ts@@ -778,7 +778,7 @@ const blockoutFilm = defineTool({const SpeechEditInput = z.object({media: ref.describe("The recording (or video) whose speech to change."),- kind: z.enum(["audio", "video"]).describe("audio, or video (its sound is edited and put back on the picture, copied as it is)."),+ kind: z.enum(["audio", "video"]).describe("audio, or video (its sound is edited and put back on the picture, copied as it is; each change keeps its length so the sound stays in step, and pace is for audio only)."),transcript: z.array(z.object({ start: z.number().min(0), end: z.number().min(0), text: z.string() })).min(1)@@ -792,7 +792,7 @@ const SpeechEditInput = z.object({.object({kind: z.enum(["pitch", "pace", "volume", "emotion", "whisper", "unwhisper", "clean", "isolate"]),semitones: z.number().int().optional().describe(`pitch: one of ${PITCH_STEPS.join(", ")}.`),- speed: z.number().optional().describe(`pace: one of ${PACES.join(", ")}.`),+ speed: z.number().optional().describe(`pace (audio only): one of ${PACES.join(", ")}.`),db: z.number().int().optional().describe(`volume: one of ${VOLUME_STEPS.join(", ")}.`),emotion: z.enum(EMOTIONS as [string, ...string[]]).optional().describe("emotion: the new mood."),})diff --git a/tests/speech-edit.test.ts b/tests/speech-edit.test.tsindex 34c760e3..3c636288 100644--- a/tests/speech-edit.test.ts+++ b/tests/speech-edit.test.ts@@ -45,17 +45,17 @@ const fits = (w: SpeechWindow) => w.end - w.start + w.seconds <= AUK_SEQUENCE_MAdescribe("Edit speech: the diff", () => {it("finds the smallest changed span, ignoring case and punctuation around words", () => {const old = splitWords("We open the new studio on Tuesday morning.");- expect(diffWords(old, splitWords("we open the new studio, on Thursday morning"))).toEqual([{ from: 6, to: 7, words: ["Thursday"] }]);+ expect(diffWords(old, splitWords("we open the new studio, on Thursday morning"))).toEqual([{ from: 6, to: 7, words: ["Thursday"], at: 6 }]);expect(diffWords(old, old)).toEqual([]);});it("tells insertions, removals and replacements apart, and keeps far-apart edits separate", () => {const old = splitWords("one two three four five six seven eight");- expect(diffWords(old, splitWords("one two really three four five six seven eight"))).toEqual([{ from: 2, to: 2, words: ["really"] }]);- expect(diffWords(old, splitWords("one two four five six seven eight"))).toEqual([{ from: 2, to: 3, words: [] }]);+ expect(diffWords(old, splitWords("one two really three four five six seven eight"))).toEqual([{ from: 2, to: 2, words: ["really"], at: 2 }]);+ expect(diffWords(old, splitWords("one two four five six seven eight"))).toEqual([{ from: 2, to: 3, words: [], at: 2 }]);expect(diffWords(old, splitWords("uno two three four five six seven ocho"))).toEqual([- { from: 0, to: 1, words: ["uno"] },- { from: 7, to: 8, words: ["ocho"] },+ { from: 0, to: 1, words: ["uno"], at: 0 },+ { from: 7, to: 8, words: ["ocho"], at: 7 },]);});@@ -64,7 +64,7 @@ describe("Edit speech: the diff", () => {{ start: 0, end: 2, text: "Hello there" },{ start: 2.5, end: 4, text: "how are you" },];- expect(diffLines(cues, ["Hello there", "how were you"])).toEqual([{ from: 3, to: 4, words: ["were"] }]);+ expect(diffLines(cues, ["Hello there", "how were you"])).toEqual([{ from: 3, to: 4, words: ["were"], at: 3 }]);});});@@ -85,8 +85,10 @@ describe("Edit speech: AuK's instructions", () => {it("writes the effects in AuK's words", () => {expect(effectInstruction({ kind: "pitch", semitones: 2 })).toBe("Raise the pitch by 2 semitones.");- expect(effectInstruction({ kind: "pitch", semitones: -1 })).toBe("Lower the pitch by 1 semitone.");+ // The Cookbook's templates as they are: "by {1/2/3} semitones", "to {0.5/0.75/1.25/1.5/2.0}x".+ expect(effectInstruction({ kind: "pitch", semitones: -1 })).toBe("Lower the pitch by 1 semitones.");expect(effectInstruction({ kind: "pace", speed: 1.5 })).toBe("Adjust the speech speed to 1.5x.");+ expect(effectInstruction({ kind: "pace", speed: 2 })).toBe("Adjust the speech speed to 2.0x.");expect(effectInstruction({ kind: "volume", db: -10 })).toBe("Decrease the volume by 10 dB.");expect(effectInstruction({ kind: "emotion", emotion: "happy" })).toBe("Change the emotion to happy.");expect(effectInstruction({ kind: "clean" })).toMatch(/remove noise and reverberation/);@@ -99,6 +101,9 @@ describe("Edit speech: AuK's instructions", () => {expect(ttsLength("Hello there")).toBeGreaterThan(1);expect(() => ttsLength("x".repeat(600))).toThrow(SpeechEditError);expect(() => ttsLength("Hello", 29.5)).toThrow(/voice sample is too long/);+ // The limit it states is the limit it keeps.+ expect(() => ttsLength("Hello", 21)).toThrow(/under 20 seconds/);+ expect(ttsLength("Hello", 19)).toBeGreaterThanOrEqual(1);});});@@ -253,4 +258,32 @@ describe("Edit speech: a run", () => {expect(() => planSpeechEdit({ engine: "open", media: "/api/files/a.wav", kind: "audio", cues, edited: cues.map((c) => c.text), seconds: 8 })).toThrow(/Nothing changed/);expect(() => planSpeechEdit({ engine: "open", media: "/api/files/a.wav", kind: "audio", cues, edited: ["x"], seconds: 8, hasAudio: false })).toThrow(/no sound/);});++ it("keeps a video's sound in step with the picture: every change keeps its length, and Pace is refused", () => {+ const edited = [cues[0].text, "on a very sunny Wednesday morning in late October.", cues[2].text];+ const audio = planSpeechEdit({ engine: "open", media: "/api/files/a.wav", kind: "audio", cues, edited, seconds: 9 });+ expect(audio.length).toBeGreaterThan(10);+ const video = planSpeechEdit({ engine: "open", media: "/api/files/v.mp4", kind: "video", cues, edited, seconds: 9 });+ expect(video.length).toBe(9);+ for (const w of video.windows) expect(w.seconds).toBeCloseTo(w.end - w.start, 6);+ expect(video.notes.join(" ")).toMatch(/keeps its length/);+ // A removal on a video keeps its length too.+ const removed = planSpeechEdit({ engine: "open", media: "/api/files/v.mp4", kind: "video", cues, edited: [cues[0].text, "on morning.", cues[2].text], seconds: 9 });+ expect(removed.length).toBe(9);+ expect(() => planSpeechEdit({ engine: "open", media: "/api/files/v.mp4", kind: "video", cues, range: { start: 0, end: 9 }, effect: { kind: "pace", speed: 2 }, seconds: 9 })).toThrow(/drift from the picture/);+ // Same-length effects still run on a video; Pace still runs on audio.+ expect(planSpeechEdit({ engine: "open", media: "/api/files/v.mp4", kind: "video", cues, range: { start: 0, end: 9 }, effect: { kind: "volume", db: 5 }, seconds: 9 }).length).toBe(9);+ expect(planSpeechEdit({ engine: "open", media: "/api/files/a.wav", kind: "audio", cues, range: { start: 0, end: 9 }, effect: { kind: "pace", speed: 2 }, seconds: 9 }).length).toBeLessThan(5);+ });++ it("merges two nearby changes with the edited words between them, not the old punctuation", () => {+ const plan = planSpeechEdit({ engine: "open", media: "/api/files/a.wav", kind: "audio", cues, edited: [cues[0].text, "on a very sunny Wednesday morning in late October.", cues[2].text], seconds: 9 });+ expect(plan.windows).toHaveLength(1);+ expect(plan.windows[0].instruction).toBe("Replace 'Tuesday morning.' with 'a very sunny Wednesday morning in late October.'.");+ });++ it("writes no model names or step counts in what the page shows", () => {+ const plan = planSpeechEdit({ engine: "open", media: "/api/files/a.wav", kind: "audio", cues, edited: [cues[0].text, "on Thursday morning.", cues[2].text], seconds: 9 });+ expect(plan.notes.join(" ")).not.toMatch(/Flash|steps/);+ });});