Skip to content

Open source CapCut alternative.

TypeScript

feat(site): fold audio references into image and video generation

David Lefdfa716dc6cf
diff --git a/site/src/cut/lib/aiTools.ts b/site/src/cut/lib/aiTools.ts
index 7e775d8f..d064a1f9 100644
--- a/site/src/cut/lib/aiTools.ts
+++ b/site/src/cut/lib/aiTools.ts
@@ -31,7 +31,7 @@ import {
renderAudioSpanWav,
} from "./media";
import { requestSidePanel, SIDE_PANEL_TABS, type SidePanelTab } from "./panelRequest";
-import { blobToInlineAudio, refToInlineAudio, type InlineImage } from "./refMedia";
+import { blobToInlineAudio, refToInlineAudio, visualRefs, type InlineImage } from "./refMedia";
import { characterPrompt, stockTitle } from "./stock";
import { STOCK_IMAGES } from "./stockManifest";
import { STOCK_VIDEOS } from "./stockVideoManifest";
@@ -771,6 +771,9 @@ export async function runAiTool(
input.reference_asset_id === undefined
? []
: resolveRefAssets([input.reference_asset_id]);
+ // Audio references carry no picture: they ride in `refs` for the compose
+ // pass to hear, while anchor rungs take the first ref that has a frame.
+ const anchors = visualRefs(refs);
// The model renders the whole clip with audio in one pass and picks its
// own length (up to ~10s) — aspect is the only knob.
const baseOpts: Omit<VideoGenOptions, "onDone"> = {
@@ -793,12 +796,16 @@ export async function runAiTool(
// the render untouched — no image-model redesign in between, and no
// text-only rung (a render without the image isn't what was asked).
if (refs.length > 0 && input.animate_reference === true) {
+ if (anchors.length === 0)
+ throw new ToolError(
+ "animate_reference needs an image or video reference — audio has no frame to animate."
+ );
return launchVideoJob(
projectId,
input,
[
- { prompt, opts: { ...baseOpts, refs: [refs[0]], composeRefs: false } },
- asReference(refs[0]),
+ { prompt, opts: { ...baseOpts, refs: [anchors[0]], composeRefs: false } },
+ asReference(anchors[0]),
],
{
note: "Animating the referenced image as the literal opening frame — the clip previews in this chat when it lands, in a minute or two.",
@@ -811,7 +818,7 @@ export async function runAiTool(
// a chat asset (its card previews above the render's), and the render
// seeds from it with the prompt as written. Text-only asks skip
// straight to video; a failed still degrades to the composed-seed hop.
- if (refs.length > 0) {
+ if (anchors.length > 0) {
const still = await gen.generateImage(projectId, prompt, {
refs,
aspect:
@@ -830,7 +837,7 @@ export async function runAiTool(
input,
[
{ prompt, opts: { ...baseOpts, refs: [refFromAsset(stillAsset)], composeRefs: false } },
- asReference(refs[0]),
+ asReference(anchors[0]),
textOnly,
],
{
@@ -841,10 +848,15 @@ export async function runAiTool(
}
return launchVideoJob(projectId, input, [
{ prompt, opts: { ...baseOpts, refs } },
- asReference(refs[0]),
+ asReference(anchors[0]),
textOnly,
]);
}
+ // Audio-only reference: no frame to anchor, so a single rung renders with
+ // the compose pass folding what it hears into the prompt.
+ if (refs.length > 0) {
+ return launchVideoJob(projectId, input, [{ prompt, opts: { ...baseOpts, refs } }]);
+ }
// No reference: text is the whole request, so the single rung runs ungated.
return launchVideoJob(projectId, input, [{ prompt, opts: { ...baseOpts } }]);
}
@@ -1473,7 +1485,7 @@ export async function runAiTool(
// reference and folds a description of its sound into the prompt. A failed
// compose falls back to the raw prompt so the render still happens. Keep
// the user's own words as the track name — the composed prompt is verbose.
- const refs = resolveMusicRefs(input.reference_asset_ids);
+ const refs = resolveRefAssets(input.reference_asset_ids);
let sent = prompt;
if (refs.length > 0) {
const composed = await composeMusicPrompt(prompt, refs);
@@ -1967,25 +1979,10 @@ function targetSubtitleTrack(input: Record<string, unknown>): number {
return lane;
}
-/** Map generation reference ids to project assets. Audio can't be looked at,
- * so only image and video assets resolve. */
+/** Map generation reference ids to project asset refs. Any media resolves:
+ * images and video frames ride as pictures; audio rides to the compose pass,
+ * which folds what it hears (speech, sound) into the prompt. */
function resolveRefAssets(ids: unknown): AssetRef[] {
- if (ids === undefined || ids === null) return [];
- const s = useEditor.getState();
- return (Array.isArray(ids) ? ids : [ids]).map((raw) => {
- const asset = s.assets.find((a) => a.id === String(raw));
- if (!asset)
- throw new ToolError(`No project asset with id ${String(raw)} — see media in the editor state.`);
- if (asset.type === "audio")
- throw new ToolError(`"${asset.name}" is audio — visual references must be images or videos.`);
- return refFromAsset(asset);
- });
-}
-
-/** Map music-reference ids to project asset refs. Music takes any media as a
- * style reference — an audio track to match, or video/images for tone — so
- * unlike the visual generators, audio resolves here too. */
-function resolveMusicRefs(ids: unknown): AssetRef[] {
if (ids === undefined || ids === null) return [];
const s = useEditor.getState();
return (Array.isArray(ids) ? ids : [ids]).map((raw) => {
diff --git a/site/src/cut/lib/composeGen.ts b/site/src/cut/lib/composeGen.ts
index ca24e913..a4ba14d2 100644
--- a/site/src/cut/lib/composeGen.ts
+++ b/site/src/cut/lib/composeGen.ts
@@ -20,12 +20,16 @@ import { readRefText, refsToParts, type InlineImage } from "./refMedia";
const VIDEO_INSTRUCTIONS = `You prepare prompts for a text+image-to-video model. The numbered images and attached files are the user's references; their request comes last. The model accepts at most one input image and plays it as the video's literal first frame — it can animate what is pictured but cannot change who or what is in it.
Reply with JSON {"prompt": string, "keepImages": number[]}:
- keepImages lists at most one image number: the one whose exact pixels should open the video. Keep an image only when the request uses its subject as-is (animate it, continue the shot, have them speak). Keep none when the request transforms or replaces what any reference shows.
-- prompt must stand alone: fold in everything the video needs from every reference — setting, framing, lighting, wardrobe, camera, mood, action, any script or text from attached files — with the requested changes applied. Describe subjects concretely; if anyone speaks, give them a matching voice and the words to say.`;
+- prompt must stand alone: fold in everything the video needs from every reference — setting, framing, lighting, wardrobe, camera, mood, action, any script or text from attached files — with the requested changes applied. Describe subjects concretely; if anyone speaks, give them a matching voice and the words to say.
+- A frame labelled as pinned is the user's chosen moment; frames sampled near it are the same moment a beat earlier/later. Treat the pinned frame as the reference, and when one of them should ride, keep the one that shows the subject best.
+- The video model never hears attached audio. Fold what it carries into the prompt: speech becomes the words a speaker says, music or ambience becomes a concrete description of the video's own sound.`;
const IMAGE_INSTRUCTIONS = `You prepare prompts for an image generation and editing model. The numbered images and attached files are the user's references; their request comes last. The model receives the kept images as inputs it can edit, combine, and draw from.
Reply with JSON {"prompt": string, "keepImages": number[]}:
- keepImages lists the image numbers the model should receive: keep the ones the request edits, combines, or borrows a subject or style from; drop ones whose contribution is better said in words.
-- prompt must stand alone given only the kept images: refer to them by their content, and fold in whatever matters from the dropped references and any script or text from attached files, with the requested changes applied.`;
+- prompt must stand alone given only the kept images: refer to them by their content, and fold in whatever matters from the dropped references and any script or text from attached files, with the requested changes applied.
+- A frame labelled as pinned is the user's chosen moment; frames sampled near it are the same moment a beat earlier/later. Treat the pinned frame as the reference, and when one of them should ride, keep the one that shows the subject best.
+- The image model never hears attached audio. Fold what it carries into the prompt: transcribe speech that should appear, translate music or ambience into the mood and setting it implies.`;
const MUSIC_INSTRUCTIONS = `You prepare prompts for a text-to-music model that generates a track from a text description alone — it never hears or sees the references. The attachments are what the user wants the music to match: an audio track to emulate, or video/images whose mood the score should carry. Their request comes last.
Reply with JSON {"prompt": string}:
diff --git a/site/src/cut/lib/generate.ts b/site/src/cut/lib/generate.ts
index 0930fedb..9c45b653 100644
--- a/site/src/cut/lib/generate.ts
+++ b/site/src/cut/lib/generate.ts
@@ -163,9 +163,9 @@ export interface VideoGenOptions {
* on-screen text. The model has no negative-prompt parameter, so the
* adapter folds this into the prompt as an avoid clause. */
negativePrompt?: string;
- /** References of any kind — video, image, text file. The model takes one
- * seed image, so at most one picture seeds the render as its literal
- * opening frame. */
+ /** References of any kind — video, image, text file, audio. The model takes
+ * one seed image, so at most one picture seeds the render as its literal
+ * opening frame; audio and text contribute through the compose rewrite. */
refs?: AssetRef[];
/** Identity anchors (up to the registry's maxReferenceImages): the render
* keeps these characters/objects/scenes consistent instead of playing one