模型能力
Reference-to-Video
提供参考图像、preset voice,或同时提供两者来引导视频生成。图像可以将特定人物、物体、服装或其他视觉元素融入视频,同时不会锁定首帧(不同于 image-to-video)。这适用于虚拟试穿、产品植入、保持角色一致的故事讲述以及 voice identity。在 grok-imagine-video-1.5 上,你还可以选择主体说话时使用的 voice(参见 Reference audio)。
每张参考图像都可以通过公开 HTTPS URL、base64-encoded data URI 或 file_id(来自 Files API)提供,并且可以在一次请求中混用这些类型。详见 Imagine → Files API 集成,了解 file_id 的详细信息和示例。
在 Vercel AI SDK 中,将 providerOptions.xai.mode 设置为 "reference-to-video",并通过 providerOptions.xai.referenceImageUrls 传入图像。
import os
import xai_sdk
client = xai_sdk.Client(api_key=os.getenv("XAI_API_KEY"))
response = client.video.generate(
prompt="slow zoom in on the white fashion runway stage. then, the model from <IMAGE_1> walks in from the back of the shot from the white opening, and gracefully walk out onto the front of the white stage platform. they wear the shirt from <IMAGE_2> and black flared jeans. they look dramatically at the camera. high quality slow motion shot. fun, playful. skin pores. highly detailed faces. perfect shot. they reach the end of the runway and look at the camera as the camera slowly zooms. subtle smile.",
model="grok-imagine-video-1.5",
reference_image_urls=[
"<IMAGE_URL_1>",
"<IMAGE_URL_2>",
"<IMAGE_URL_3>",
],
duration=10,
aspect_ratio="16:9",
resolution="720p",
)
print(response.url)Reference audio
在 grok-imagine-video-1.5 上,最多可通过 3 传入 3 个 preset voice,为主体指定 voicereference_audios。每个条目通过 voice_id 指定 voice;这些 voice 来自与 Text to Speech 相同的内置列表,因此 {"voice_id": "eve"} 会使用 Eve 的 voice 说话。标识符不区分大小写;未知标识符会返回 400,并附带可用 voice 列表。你可以在旗舰 voice 公告 传入图像。
reference_audios中试听每种 voice。该功能仅接受 preset voice,不能上传自己的音频 clip。voice 可以与参考图像一起使用,也可以单独使用;在 prompt 中使用 <AUDIO_0>、<AUDIO_1>和<AUDIO_2> 标记 voice(如果还传入图像,则使用 <IMAGE_0>……)。
import os
import time
import requests
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {os.environ['XAI_API_KEY']}",
}
response = requests.post(
"https://api.x.ai/v1/videos/generations",
headers=headers,
json={
"model": "grok-imagine-video-1.5",
"prompt": "The person from <IMAGE_1> speaks to camera with the voice from <AUDIO_0>.",
"reference_images": [{"url": "<IMAGE_URL_1>"}],
"reference_audios": [{"voice_id": "eve"}],
"duration": 8,
"aspect_ratio": "9:16",
"resolution": "720p",
},
)
request_id = response.json()["request_id"]
while True:
result = requests.get(
f"https://api.x.ai/v1/videos/{request_id}",
headers={"Authorization": headers["Authorization"]},
)
data = result.json()
if data["status"] == "done":
print(data["video"]["url"])
break
elif data["status"] == "expired":
print("Request expired")
break
time.sleep(5)相关内容
视频生成 — 根据文本 prompt 生成视频
Image-to-Video — 让静态图像动起来
视频编辑 — 编辑现有视频
API 参考 — 完整的 endpoint 文档
Imagine API 主页 — 查看 Imagine API 的实际效果