SPB Git

Repository Public

extension.md 4.8 KB 140 lines markdown

1<!-- source: https://docs.x.ai/developers/model-capabilities/video/extension (from https://docs.x.ai/llms-full.txt, retrieved 2026-09-19T05:28:55Z) -->2#### Model Capabilities34# Video Extension56Extend an existing video by providing a source video and a text prompt describing what should happen next. The result is a single video that picks up seamlessly from the last frame of the input and continues with the generated content.78You can provide the source video as a public URL, a base64-encoded data URI, or a `file_id` from the [Files API](/developers/files). See [Imagine → Files API Integration](/developers/model-capabilities/imagine/files/inputs) for using `file_id` inputs.910> [!WARNING]1112The `duration` parameter controls the length of the **extended portion only**, not the total output. For example, if your input video is 10 seconds and you set `duration` to 5, the returned video will be 15 seconds long (10s original + 5s extension).1314```python customLanguage="pythonXAI"15import os16import xai_sdk1718client = xai_sdk.Client(api_key=os.getenv("XAI_API_KEY"))1920response = client.video.extend(21    prompt="The shot pans to an over the shoulder perspective. Calm controlled scene.",22    model="grok-imagine-video",23    video_url="<VIDEO_URL>",24    duration=10,25)2627print(response.url)28```2930```python customLanguage="pythonRequests"31import os32import time33import requests3435headers = {36    "Content-Type": "application/json",37    "Authorization": f"Bearer {os.environ['XAI_API_KEY']}",38}3940response = requests.post(41    "https://api.x.ai/v1/videos/extensions",42    headers=headers,43    json={44        "model": "grok-imagine-video",45        "prompt": "The shot pans to an over the shoulder perspective. Calm controlled scene.",46        "duration": 10,47        "video": {"url": "<VIDEO_URL>"},48    },49)5051request_id = response.json()["request_id"]5253while True:54    result = requests.get(55        f"https://api.x.ai/v1/videos/{request_id}",56        headers={"Authorization": headers["Authorization"]},57    )58    data = result.json()59    if data["status"] == "done":60        print(data["video"]["url"])61        break62    elif data["status"] in ("expired", "failed"):63        print(f"Request {data['status']}")64        break65    time.sleep(5)66```6768```javascript customLanguage="javascriptAISDK"69import { xai } from "@ai-sdk/xai";70import { experimental_generateVideo as generateVideo } from "ai";7172const source = await generateVideo({73    model: xai.video("grok-imagine-video-1.5"),74    prompt: "A cat sitting on a sunlit windowsill, tail gently swishing.",75    duration: 5,76    aspectRatio: "16:9",77    providerOptions: {78        xai: {79            pollTimeoutMs: 600000,80        },81    },82});8384const sourceUrl = source.providerMetadata?.xai?.videoUrl;8586const extended = await generateVideo({87    model: xai.video("grok-imagine-video"),88    prompt: "The cat turns its head, notices a butterfly, and leaps off.",89    duration: 6,90    providerOptions: {91        xai: {92            mode: "extend-video",93            videoUrl: sourceUrl,94            pollTimeoutMs: 600000,95        },96    },97});9899const extendedVideoUrl = extended.providerMetadata?.xai?.videoUrl;100console.log(extendedVideoUrl);101```102103```bash104# Start the video extension request105REQUEST_ID=$(curl -s -X POST https://api.x.ai/v1/videos/extensions \106  -H "Content-Type: application/json" \107  -H "Authorization: Bearer $XAI_API_KEY" \108  -d '{109    "model": "grok-imagine-video",110    "prompt": "The shot pans to an over the shoulder perspective. Calm controlled scene.",111    "duration": 10,112    "video": {"url": "<VIDEO_URL>"}113  }' | jq -r '.request_id')114115# Poll until the video is ready116while true; do117  RESULT=$(curl -s https://api.x.ai/v1/videos/$REQUEST_ID \118    -H "Authorization: Bearer $XAI_API_KEY")119  STATUS=$(echo "$RESULT" | jq -r '.status')120  if [ "$STATUS" = "done" ]; then121    echo "$RESULT" | jq -r '.video.url'122    break123  elif [ "$STATUS" = "failed" ] || [ "$STATUS" = "expired" ]; then124    echo "Request $STATUS"; echo "$RESULT" | jq .125    break126  fi127  sleep 5128done129```130131Video editing uses the `/v1/videos/edits` endpoint and `client.video.generate(video_url=...)` in the Python SDK. In the AI SDK, set `providerOptions.xai.mode` to `"edit-video"` or `"extend-video"` and pass `providerOptions.xai.videoUrl`. The same asynchronous polling pattern applies to both flows, and the AI SDK returns the xAI-hosted output URL in `providerMetadata.xai.videoUrl`.132133## Related134135* [Video Generation](/developers/model-capabilities/video/generation) — Generate videos from text prompts136* [Video Editing](/developers/model-capabilities/video/editing) — Edit existing videos137* [Image-to-Video](/developers/model-capabilities/video/image-to-video) — Animate a still image138* [API Reference](/developers/rest-api-reference) — Full endpoint documentation139* [Imagine API Landing Page](https://x.ai/api/imagine) — Showcase of the Imagine API in action140
Files main
Menu

Theme