模型能力

Video Editing

提供源视频和 prompt 来编辑现有视频。模型会理解视频内容,并应用你要求的更改。

源视频可以是公开 URL、base64 编码的 data URI,或来自file_idFiles API。有关如何使用Imagine → Files API Integration作为 input,请参阅file_id

下方 demo 展示了 video editing 的实际效果。grok-imagine-video能够在强力保留场景的同时实现高保真编辑,只修改你要求的内容,并保持视频其余部分不变:

视频预览
import os
import xai_sdk

client = xai_sdk.Client(api_key=os.getenv("XAI_API_KEY"))

response = client.video.generate(
    prompt="Give the woman a silver necklace",
    model="grok-imagine-video",
    video_url="https://data.x.ai/docs/video-generation/portrait-wave.mp4",
)

print(response.url)

在 Vercel AI SDK 中,将providerOptions.xai.mode设置为"edit-video",并通过providerOptions.xai.videoUrl传入源视频 URL,即可触发 video editing。prompt用于描述所需修改;durationaspectRatioresolution会被忽略,因为输出会继承输入视频的这些属性,最高为 720p。

并发请求

需要对同一个源视频应用多项编辑时,请并发运行请求。这适合从同一个中间结果分支出多个编辑版本。

import os
import asyncio
import xai_sdk

async def edit_concurrently():
    client = xai_sdk.AsyncClient(api_key=os.getenv("XAI_API_KEY"))

    source_video = "https://data.x.ai/docs/video-generation/portrait-wave.mp4"

    prompts = [
        "Give the woman a silver necklace",
        "Change the color of the woman's outfit to red",
        "Give the woman a wide-brimmed black hat",
    ]

    tasks = [
        client.video.generate(
            prompt=prompt,
            model="grok-imagine-video",
            video_url=source_video,
        )
        for prompt in prompts
    ]

    results = await asyncio.gather(*tasks)

    for prompt, result in zip(prompts, results):
        print(f"{prompt}: {result.url}")

asyncio.run(edit_concurrently())