热门模型
视频生成通常比图像生成耗时更长。请根据视频时长和质量设置准备较长的处理时间。
步骤 1:生成视频
向/generate/video/kling/kling-2.5 发送 POST 请求,附带你的提示词和视频参数。API 会立即返回一个 job ID。
// npm install @krea-ai/sdk
import { Krea } from "@krea-ai/sdk";
const krea = new Krea({ apiKey: process.env.KREA_API_KEY });
const job = await krea.video("kling/kling-2.5", {
prompt: "a majestic eagle soaring over snow-capped mountains at sunrise",
duration: 5,
aspect_ratio: "16:9"
});
console.log(`Job ID: ${job.job_id}`);
curl -X POST https://api.krea.ai/generate/video/kling/kling-2.5 \
-H "Authorization: Bearer YOUR_API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"prompt": "a majestic eagle soaring over snow-capped mountains at sunrise",
"duration": 5,
"aspect_ratio": "16:9"
}'
import requests
API_BASE = "https://api.krea.ai"
API_TOKEN = "YOUR_API_TOKEN"
response = requests.post(
f"{API_BASE}/generate/video/kling/kling-2.5",
headers={
"Authorization": f"Bearer {API_TOKEN}",
"Content-Type": "application/json"
},
json={
"prompt": "a majestic eagle soaring over snow-capped mountains at sunrise",
"duration": 5,
"aspect_ratio": "16:9"
}
)
job = response.json()
print(f"Job ID: {job['job_id']}")
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
func main() {
apiBase := "https://api.krea.ai"
apiToken := "YOUR_API_TOKEN"
payload := map[string]interface{}{
"prompt": "a majestic eagle soaring over snow-capped mountains at sunrise",
"duration": 5,
"aspect_ratio": "16:9",
}
jsonData, _ := json.Marshal(payload)
req, _ := http.NewRequest("POST", apiBase+"/generate/video/kling/kling-2.5", bytes.NewBuffer(jsonData))
req.Header.Set("Authorization", "Bearer "+apiToken)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, _ := client.Do(req)
defer resp.Body.Close()
var job map[string]interface{}
json.NewDecoder(resp.Body).Decode(&job)
fmt.Printf("Job ID: %s\n", job["job_id"])
}
替换为你的 API Token要替换上述示例中的 YOUR_API_TOKEN 占位符,你需要在 krea.ai/settings/api-tokens 生成一个 API token。如果需要帮助,请参考 API Keys & Billing 页面上的说明。
{
"job_id": "550e8400-e29b-41d4-a716-446655440000",
"status": "queued",
"created_at": "2025-01-15T10:30:00.000Z",
"estimated_time": "60-120 seconds"
}
步骤 2:轮询结果
视频生成比图像生成耗时更长。每 5 秒轮询一次/jobs/{job_id} 以检查进度。
Webhook 可用!设置 webhook,可在任务完成时收到通知。参见 Webhooks 指南 开始使用。
// npm install @krea-ai/sdk
import { Krea } from "@krea-ai/sdk";
const krea = new Krea({ apiKey: process.env.KREA_API_KEY });
async function waitForVideo(jobId) {
const completed = await krea.jobs.wait(jobId, { intervalMs: 5000 });
return completed.result.urls[0];
}
const videoUrl = await waitForVideo(job.job_id);
console.log(`Video ready: ${videoUrl}`);
curl -X GET https://api.krea.ai/jobs/YOUR_JOB_ID \
-H "Authorization: Bearer YOUR_API_TOKEN"
import time
def wait_for_video(job_id):
while True:
response = requests.get(
f"{API_BASE}/jobs/{job_id}",
headers={"Authorization": f"Bearer {API_TOKEN}"}
)
job = response.json()
if job["status"] == "completed":
return job["result"]["urls"][0]
if job["status"] in ("failed", "cancelled"):
raise Exception(f"Job failed: {job['status']}")
# Show progress if available
if "progress" in job:
print(f"Progress: {job['progress']}%")
else:
print(f"Status: {job['status']}")
time.sleep(5)
video_url = wait_for_video(job["job_id"])
print(f"Video ready: {video_url}")
func waitForVideo(jobID string) (string, error) {
for {
req, _ := http.NewRequest("GET", apiBase+"/jobs/"+jobID, nil)
req.Header.Set("Authorization", "Bearer "+apiToken)
resp, _ := client.Do(req)
var job map[string]interface{}
json.NewDecoder(resp.Body).Decode(&job)
resp.Body.Close()
switch job["status"] {
case "completed":
result := job["result"].(map[string]interface{})
urls := result["urls"].([]interface{})
return urls[0].(string), nil
case "failed", "cancelled":
return "", fmt.Errorf("job failed: %s", job["status"])
}
// Show progress if available
if progress, ok := job["progress"]; ok {
fmt.Printf("Progress: %.0f%%\n", progress)
} else {
fmt.Printf("Status: %s\n", job["status"])
}
time.Sleep(5 * time.Second)
}
}
{
"job_id": "550e8400-e29b-41d4-a716-446655440000",
"status": "completed",
"created_at": "2025-01-15T10:30:00.000Z",
"completed_at": "2025-01-15T10:31:45.000Z",
"result": {
"urls": ["https://gen.krea.ai/videos/your-video.mp4"]
}
}
Webhook 可用!设置 webhook,可在任务完成时收到通知。参见 Webhooks 指南 开始使用。
常用参数
要查看所有模型的详细参数列表,请访问 Model APIs 页面。
| 参数 | 类型 | 说明 |
|---|---|---|
prompt | string | 视频内容的详细描述 |
duration | number | 视频时长(秒)。可选值取决于具体模型。 |
aspect_ratio | string | 视频宽高比,例如 16:9、9:16 或 1:1 |
start_image | string | 可选,图像生成视频模型的源图像 URL |
end_image | string | 可选,支持的模型的结束帧 URL |
mode | string | 可选,部分模型公开的质量模式 |
model | string | 要使用的视频生成模型 |
获得更好视频的提示词技巧:
- 具体描述运动和相机运动
- 描述场景、光照和氛围
- 提及时间感(例如”缓慢平移”、“快速拉近”)
- 加入风格参考(例如”电影感”、“纪录片风格”)
视频生成的费用比图像生成更高。请查看 API Keys & Billing,了解各模型定价和如何为 API 余额充值。