模型能力
参考视频生成
提供参考图像、预设语音或两者兼有,以指导生成视频。图像会融入特定人物、物体、服装或其他视觉元素,而不会锁定第一帧(与 image-to-video 不同)。这对于虚拟试穿、产品植入、角色一致性的故事叙述和语音身份识别非常有用。在 grok-imagine-video-1.5 上,您还可以选择角色说话的语音(参见 参考音频)。
每个参考图像可以作为公共 HTTPS URL、base64 编码的数据 URI 或来自 Files API 的 file_id 提供 — 您可以在单个请求中混合使用不同类型。有关 file_id 的详细信息和示例,请参见 Imagine → Files API 集成。
在 Vercel AI SDK 中,将 providerOptions.xai.mode 设置为 "reference-to-video",并通过 providerOptions.xai.referenceImageUrls 传递图像。
WARNING
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)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": "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.",
"reference_images": [
{"url": "<IMAGE_URL_1>"},
{"url": "<IMAGE_URL_2>"},
{"url": "<IMAGE_URL_3>"},
],
"duration": 10,
"aspect_ratio": "16:9",
"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)import { xai } from "@ai-sdk/xai";
import { experimental_generateVideo as generateVideo } from "ai";
const result = await generateVideo({
model: xai.video("grok-imagine-video-1.5"),
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.",
duration: 10,
aspectRatio: "16:9",
providerOptions: {
xai: {
mode: "reference-to-video",
referenceImageUrls: [
"<IMAGE_URL_1>",
"<IMAGE_URL_2>",
"<IMAGE_URL_3>",
],
resolution: "720p",
pollTimeoutMs: 600000,
},
},
});
const videoUrl = result.providerMetadata?.xai?.videoUrl;
console.log(videoUrl);# Start the reference-to-video request
REQUEST_ID=$(curl -s -X POST https://api.x.ai/v1/videos/generations \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $XAI_API_KEY" \
-d '{
"model": "grok-imagine-video-1.5",
"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.",
"reference_images": [
{"url": "<IMAGE_URL_1>"},
{"url": "<IMAGE_URL_2>"},
{"url": "<IMAGE_URL_3>"}
],
"duration": 10,
"aspect_ratio": "16:9",
"resolution": "720p"
}' | jq -r '.request_id')
# Poll until the video is ready
while true; do
RESULT=$(curl -s https://api.x.ai/v1/videos/$REQUEST_ID \
-H "Authorization: Bearer $XAI_API_KEY")
STATUS=$(echo "$RESULT" | jq -r '.status')
if [ "$STATUS" = "done" ]; then
echo "$RESULT" | jq -r '.video.url'
break
elif [ "$STATUS" = "failed" ] || [ "$STATUS" = "expired" ]; then
echo "Request $STATUS"; echo "$RESULT" | jq .
break
fi
sleep 5
done参考音频
WARNING
参考音频目前仅在美国对可信合作伙伴可用。
在 grok-imagine-video-1.5 上,通过 reference_audios 传递最多 3 个预设语音,为您的角色赋予声音。每个条目通过 voice_id 命名语音,该标识取自与 文本转语音 相同的内置语音列表,因此 {"voice_id": "eve"} 会以 Eve 的声音说话。标识符不区分大小写;未知标识符会返回 400 并附带可用语音列表。您可以在 旗舰语音公告 中听取每种语音。
reference_audios 仅接受预设语音;您无法上传自己的音频片段。可以将语音与参考图像一起使用,或单独使用,并在提示中将语音标记为 <AUDIO_0>、<AUDIO_1> 和 <AUDIO_2>(当同时传递图像时,与 <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)REQUEST_ID=$(curl -s -X POST https://api.x.ai/v1/videos/generations \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $XAI_API_KEY" \
-d '{
"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"
}' | jq -r '.request_id')
while true; do
RESULT=$(curl -s https://api.x.ai/v1/videos/$REQUEST_ID \
-H "Authorization: Bearer $XAI_API_KEY")
STATUS=$(echo "$RESULT" | jq -r '.status')
if [ "$STATUS" = "done" ]; then
echo "$RESULT" | jq -r '.video.url'
break
elif [ "$STATUS" = "failed" ] || [ "$STATUS" = "expired" ]; then
echo "Request $STATUS"; echo "$RESULT" | jq .
break
fi
sleep 5
done相关内容
- 视频生成 — 根据文本提示生成视频
- 图像转视频 — 使静态图像动起来
- 视频编辑 — 编辑现有视频
- API 参考 — 完整的端点文档
- Imagine API 着陆页 — Imagine API 实际应用展示