
Branch Imagine video edits from an intermediate clip
Branch Imagine video edits from an intermediate clip
When every variant should share the same first change (party hat, then sunglasses vs scarf), finish one edit, take that output URL (or file_id), and fan out concurrent follow-up edits from it. Parallel edits from the original source alone are covered in Run concurrent Imagine video edits from one source. Edits keep source duration and aspect ratio; output is capped at 720p. duration, aspect_ratio, and resolution on an edit call are ignored.
AI SDK: shared first edit, then Promise.all
import { xai, type XaiVideoModelOptions } from "@ai-sdk/xai";
import { experimental_generateVideo as generateVideo } from "ai";
const providerOptions = {
xai: {
mode: "edit-video",
videoUrl: "https://example.com/source-video.mp4",
pollTimeoutMs: 600000,
} satisfies XaiVideoModelOptions,
};
const step1 = await generateVideo({
model: xai.video("grok-imagine-video"),
prompt: "Add a party hat to the person",
providerOptions,
});
const step1VideoUrl = step1.providerMetadata?.xai?.videoUrl;
const [withSunglasses, withScarf] = await Promise.all([
generateVideo({
model: xai.video("grok-imagine-video"),
prompt: "Add sunglasses",
providerOptions: {
xai: {
mode: "edit-video",
videoUrl: step1VideoUrl,
pollTimeoutMs: 600000,
} satisfies XaiVideoModelOptions,
},
}),
generateVideo({
model: xai.video("grok-imagine-video"),
prompt: "Add a scarf",
providerOptions: {
xai: {
mode: "edit-video",
videoUrl: step1VideoUrl,
pollTimeoutMs: 600000,
} satisfies XaiVideoModelOptions,
},
}),
]);
console.log(withSunglasses.providerMetadata?.xai?.videoUrl);
console.log(withScarf.providerMetadata?.xai?.videoUrl);
REST / Python
POST /v1/videos/edits for the shared first pass, poll GET /v1/videos/{request_id} until done, then start one edit job per branch with video.url (or video.file_id) set to that intermediate. Python AsyncClient can asyncio.gather the branch jobs the same way as concurrent edits from one source.
Store the intermediate with storage_options if you need a durable file_id instead of an ephemeral vidgen URL (Persist an Imagine output with a public URL).
Pitfalls
- Fan-out from the original source when you meant to keep the shared first change.
- Treating edit
durationas total length; edits inherit length from the source. - Letting intermediate URLs expire before the branch jobs start.
- Console API credits are a separate bill from SuperGrok's weekly pool on grok.com.