Extend an Imagine video with the API
Extend an Imagine video with the API
Video extension continues an existing clip from its last frame. You send the source video, a prompt for what happens next, and a duration for the new segment only. The API returns one file: original plus extension.
Extend with the Python SDK
- Create an API key in console.x.ai.
- Install
xai-sdkand setXAI_API_KEY. - Call
client.video.extendwith the source URL, the next-shot prompt, model id, anddurationfor the added seconds.
import os
import xai_sdk
client = xai_sdk.Client(api_key=os.getenv("XAI_API_KEY"))
response = client.video.extend(
prompt="The shot pans over the shoulder. Calm, controlled scene.",
model="grok-imagine-video",
video_url="<VIDEO_URL>",
duration=10,
)
print(response.url)
duration is the length of the new tail. A 10s source plus duration=5 yields about 15s total.
REST shape
POST https://api.x.ai/v1/videos/extensions with model, prompt, duration, and video: { "url": "..." }. Poll GET /v1/videos/{request_id} until status is done, then read video.url. Source video can also be a data URI or a Files API file_id.
Pitfalls
- Edit and extend are different endpoints. Edits use
/v1/videos/editsand keep the original length; extensions add time from the last frame. - Output URLs are temporary. Download or persist through the Files API if you need a lasting copy.
- Model ids in the docs sometimes read
grok-imagine-videoand sometimesgrok-imagine-video-1.5. Use the id your console lists for extension, and fail closed onfailed_preconditionif the team lacks the capability.