Skip to content
Docs

AI Gateway Video Extension

Continue an existing video from its last frame. Describe what happens next and the model generates new footage that picks up where the source video left off.

Every model here also runs as a background job instead of one long-lived request. See asynchronous generation below.

Grok Imagine Video (by SpaceXAI) can extend existing videos using text prompts. Provide a source video URL and describe the scene that continues from the last frame.

To edit an existing video instead of extending it, see Video editing.

Browse the latest Grok video models on the AI Gateway Models page.

Only spacexai/grok-imagine-video supports extension. The spacexai/grok-imagine-video-1.5 and spacexai/grok-imagine-video-1.5-preview models support image-to-video only.

Extension requires providerOptions.spacexai.mode: 'extend-video'. Setting videoUrl alone triggers video editing mode, not extension.

ParameterTypeRequiredDescription
promptstringYesPrompt describing what should happen next in the video
durationnumberNoLength of the extension segment only, not total output (1 to 10 seconds). Defaults to 6
providerOptions.spacexai.mode'extend-video'YesRoutes the request to video extension instead of editing
providerOptions.spacexai.videoUrlstringYesURL of the source video to extend
providerOptions.spacexai.pollIntervalMsnumberNoHow often to check task status. Defaults to 5000
providerOptions.spacexai.pollTimeoutMsnumberNoMaximum wait time. Defaults to 600000 (10 minutes)

Generate a source video, then extend it using the ephemeral URL from provider metadata:

video-extension-two-step.ts
import { experimental_generateVideo as generateVideo } from 'ai';
import fs from 'node:fs';
 
// Step 1: Generate source video
const source = await generateVideo({
  model: 'spacexai/grok-imagine-video',
  prompt: 'A cat sitting on a sunlit windowsill, tail gently swishing.',
  duration: 5,
  aspectRatio: '16:9',
  providerOptions: {
    spacexai: { pollTimeoutMs: 600000 },
  },
});
 
const sourceUrl = source.providerMetadata?.spacexai?.videoUrl as string;
 
// Step 2: Extend from last frame
const extended = await generateVideo({
  model: 'spacexai/grok-imagine-video',
  prompt: 'The cat turns its head, notices a butterfly, and leaps off.',
  duration: 6,
  providerOptions: {
    spacexai: {
      mode: 'extend-video',
      videoUrl: sourceUrl,
      pollTimeoutMs: 600000,
    },
  },
});
 
fs.writeFileSync('extended.mp4', extended.videos[0].uint8Array);

Extend from a fixed source URL:

video-extension.ts
import { experimental_generateVideo as generateVideo } from 'ai';
import fs from 'node:fs';
 
const result = await generateVideo({
  model: 'spacexai/grok-imagine-video',
  prompt:
    'The cat continues walking slowly through the dandelions as the breeze picks up.',
  duration: 5,
  providerOptions: {
    spacexai: {
      mode: 'extend-video',
      videoUrl: 'https://example.com/source-video.mp4',
      pollTimeoutMs: 600000,
    },
  },
});
 
fs.writeFileSync('output.mp4', result.videos[0].uint8Array);

Video generation can take several minutes. Set pollTimeoutMs to at least 10 minutes (600000ms) for reliable operation. Generated video URLs are ephemeral and should be downloaded promptly.

Pass a webhook factory and the SDK registers your URL with the job instead of polling for it. The factory returns that URL and a received promise it waits on, then it fetches the videos itself.

async-video-extension.ts
const result = await generateVideo({
  model: gateway.videoModel('spacexai/grok-imagine-video'),
  prompt: 'The cat turns its head, notices a butterfly, and leaps off.',
  duration: 6,
  providerOptions: {
    spacexai: {
      mode: 'extend-video',
      // Already a hosted URL, which is what the asynchronous flow needs.
      videoUrl: 'https://example.com/source-video.mp4',
    },
  },
  webhook: async () => ({
    url: callbackUrl,
    received: waitForDelivery(token),
  }),
  poll: { timeoutMs: 10 * 60 * 1000 },
});

The start request AI Gateway persists is capped at 300 KiB, so pass your source video as a hosted URL on this flow rather than inline data.

token, callbackUrl, and waitForDelivery come from webhook-driven completion, which also covers the receiver, signature verification, and the payload shape.


Last updated September 8, 2026

Was this helpful?

supported.