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>"
}