xAI
创建 xAI 视频编辑
使用 POST /grok/v1/videos/edits 通过文本 Prompt 编辑源视频、保留运动效果,并获取用于异步轮询结果的 request_id。
POST
/
grok
/
v1
/
videos
/
edits
cURL
curl https://api.cometapi.com/grok/v1/videos/edits \
-H "Authorization: Bearer $COMETAPI_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "grok-imagine-video",
"prompt": "Add snow to the scene.",
"video": {"url": "https://your-video-host/source.mp4"}
}'import os
import requests
response = requests.post(
"https://api.cometapi.com/grok/v1/videos/edits",
headers={"Authorization": "Bearer " + os.environ["COMETAPI_KEY"]},
json={
"model": "grok-imagine-video",
"prompt": "Add snow to the scene.",
"video": {"url": "https://your-video-host/source.mp4"},
},
)
task = response.json()
print(task["request_id"]) # poll GET /grok/v1/videos/{request_id}const response = await fetch("https://api.cometapi.com/grok/v1/videos/edits", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.COMETAPI_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "grok-imagine-video",
prompt: "Add snow to the scene.",
video: { url: "https://your-video-host/source.mp4" },
}),
});
const task = await response.json();
console.log(task.request_id); // poll GET /grok/v1/videos/{request_id}<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.cometapi.com/grok/v1/videos/edits",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'prompt' => 'Add snow to the scene.',
'video' => [
'url' => 'https://example.com/source.mp4'
]
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.cometapi.com/grok/v1/videos/edits"
payload := strings.NewReader("{\n \"prompt\": \"Add snow to the scene.\",\n \"video\": {\n \"url\": \"https://example.com/source.mp4\"\n }\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.cometapi.com/grok/v1/videos/edits")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"prompt\": \"Add snow to the scene.\",\n \"video\": {\n \"url\": \"https://example.com/source.mp4\"\n }\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.cometapi.com/grok/v1/videos/edits")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"prompt\": \"Add snow to the scene.\",\n \"video\": {\n \"url\": \"https://example.com/source.mp4\"\n }\n}"
response = http.request(request)
puts response.read_body{
"request_id": "<request_id>"
}使用此端点可通过文本指令编辑现有 MP4。与全新生成请求相比,输出会更贴近源片段的时序和构图。
发送请求之前
- 提供可访问的
video.url - 保持源片段较短;xAI 官方建议将编辑时长限制在约 8.7 秒以内
- 使用聚焦的指令,描述一个明确的改动
- 保存返回的
request_id,因为编辑使用与生成相同的轮询流程
编辑流程
1
提交编辑请求
发送源视频 URL、编辑 Prompt 和
model: grok-imagine-video。2
轮询最终结果
调用获取 xAI 视频结果,直到任务完成。
3
存储编辑后资源
下载已完成的输出,或将返回的 URL 转入你自己的存储流程。
CometAPI 上的变化
xAI 将视频编辑文档描述为与生成相同的异步生命周期,只是将可选的源图像替换为源视频。CometAPI 保持了这一行为以及相同的轮询端点,因此你的编辑工作流仍然是“启动 -> 轮询 -> 下载”。授权
Bearer token authentication. Use your CometAPI key.
请求体
application/json
Edit instruction describing the change you want.
示例:
"Add snow to the scene."
Source video to edit. xAI documents an input limit of about 8.7 seconds.
Show child attributes
Show child attributes
xAI video model id.
示例:
"grok-imagine-video"
Optional output delivery configuration.
Optional end-user identifier.
响应
200 - application/json
Request accepted.
Deferred request id used for polling.
⌘I
cURL
curl https://api.cometapi.com/grok/v1/videos/edits \
-H "Authorization: Bearer $COMETAPI_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "grok-imagine-video",
"prompt": "Add snow to the scene.",
"video": {"url": "https://your-video-host/source.mp4"}
}'import os
import requests
response = requests.post(
"https://api.cometapi.com/grok/v1/videos/edits",
headers={"Authorization": "Bearer " + os.environ["COMETAPI_KEY"]},
json={
"model": "grok-imagine-video",
"prompt": "Add snow to the scene.",
"video": {"url": "https://your-video-host/source.mp4"},
},
)
task = response.json()
print(task["request_id"]) # poll GET /grok/v1/videos/{request_id}const response = await fetch("https://api.cometapi.com/grok/v1/videos/edits", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.COMETAPI_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "grok-imagine-video",
prompt: "Add snow to the scene.",
video: { url: "https://your-video-host/source.mp4" },
}),
});
const task = await response.json();
console.log(task.request_id); // poll GET /grok/v1/videos/{request_id}<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.cometapi.com/grok/v1/videos/edits",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'prompt' => 'Add snow to the scene.',
'video' => [
'url' => 'https://example.com/source.mp4'
]
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.cometapi.com/grok/v1/videos/edits"
payload := strings.NewReader("{\n \"prompt\": \"Add snow to the scene.\",\n \"video\": {\n \"url\": \"https://example.com/source.mp4\"\n }\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.cometapi.com/grok/v1/videos/edits")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"prompt\": \"Add snow to the scene.\",\n \"video\": {\n \"url\": \"https://example.com/source.mp4\"\n }\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.cometapi.com/grok/v1/videos/edits")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"prompt\": \"Add snow to the scene.\",\n \"video\": {\n \"url\": \"https://example.com/source.mp4\"\n }\n}"
response = http.request(request)
puts response.read_body{
"request_id": "<request_id>"
}