Sora 2
OpenAI Sora 2 视频生成模型,支持文生视频和图生视频,可生成 4/8/12 秒的高质量视频。
模型
| 模型名称 | 时长 | 宽高比 |
|---|---|---|
sora-2 | 4s, 8s, 12s | 16:9, 9:16 |
定价
按秒计费:25 credits/s ($0.125/s)。
| 时长 | 积分 | 价格 |
|---|---|---|
| 4s | 100 credits | $0.50 |
| 8s | 200 credits | $1.00 |
| 12s | 300 credits | $1.50 |
创建任务
POST https://api.aivideoapi.ai/v1/videos/generations
请求体
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | 必须为 sora-2 |
input | object | 是 | 生成参数,见下方 |
callback_url | string | 否 | 接收任务完成/失败通知的 URL |
Input 对象
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
prompt | string | 是 | 文本描述(最多 5000 字符) |
duration | integer | 是 | 视频时长(秒):4、8 或 12 |
aspect_ratio | string | 是 | 宽高比:16:9(横屏)或 9:16(竖屏) |
image_urls | string[] | 否 | 首帧图片 URL,用于图生视频模式(仅支持 1 张图片) |
示例:文生视频
curl -X POST https://api.aivideoapi.ai/v1/videos/generations \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "sora-2",
"callback_url": "https://your-server.com/webhook",
"input": {
"prompt": "无人机航拍日落时分的山脉,电影级光影效果",
"duration": 8,
"aspect_ratio": "16:9"
}
}'
示例:图生视频
提供一张图片作为视频的首帧,模型会基于该图片和提示词生成视频。
curl -X POST https://api.aivideoapi.ai/v1/videos/generations \
-H "Authorization: Bearer sk-your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "sora-2",
"callback_url": "https://your-server.com/webhook",
"input": {
"prompt": "镜头缓慢推进,人物转身微笑",
"duration": 4,
"aspect_ratio": "9:16",
"image_urls": ["https://example.com/photo.jpg"]
}
}'
响应
{
"code": 200,
"msg": "success",
"data": {
"taskId": "cbf6b69d-4f03-4817-8ed7-94c0292184a8"
}
}
查询任务
curl https://api.aivideoapi.ai/v1/tasks/{taskId} \
-H "Authorization: Bearer sk-your-api-key"
状态变化:pending -> processing -> completed 或 failed。
处理中
{
"id": "e717a5ee-2ed4-47f4-8cc3-53394f1abb35",
"status": "processing",
"model": "sora-2",
"created_at": 1774706165,
"progress": null
}
完成
{
"id": "7d89c51e-9430-410e-909e-df347131ebaa",
"status": "completed",
"model": "sora-2",
"created_at": 1774790227,
"completed_at": 1774796529,
"output": {
"urls": [
"https://your-r2-bucket.example.com/videos/2026/04/02/7d89c51e.mp4?..."
],
"metadata": {
"duration": 8,
"prompt": "无人机航拍日落时分的山脉,电影级光影效果"
}
}
}
视频 URL 有效期为 24 小时,请在生成完成后及时下载。
失败
{
"id": "57c8772c-f834-46f3-9b7d-81f92e104050",
"status": "failed",
"model": "sora-2",
"created_at": 1774793758,
"error": {
"code": "upstream_error",
"message": "Video generation failed"
}
}
任务失败时,预扣的积分会自动退还。
回调 (Callback)
创建任务时传入 callback_url,系统会在任务完成或失败时自动向你的 URL 发送 POST 请求。
任务完成时回调
{
"id": "7d89c51e-9430-410e-909e-df347131ebaa",
"status": "completed",
"model": "sora-2",
"created_at": 1774790227,
"completed_at": 1774796529,
"output": {
"urls": [
"https://your-r2-bucket.example.com/videos/2026/04/02/7d89c51e.mp4?..."
],
"metadata": {
"duration": 8,
"prompt": "无人机航拍日落时分的山脉,电影级光影效果"
}
}
}
任务失败时回调
{
"id": "57c8772c-f834-46f3-9b7d-81f92e104050",
"status": "failed",
"model": "sora-2",
"created_at": 1774793758,
"error": {
"code": "upstream_error",
"message": "Video generation failed"
}
}
任务失败时,预扣的积分会自动退还。
输入要求
图片(首帧)
- 格式:JPEG、PNG、WebP
- 大小:不超过 10 MB
- 宽高比:2:5 ~ 5:2
- 尺寸:短边不小于 300px
常见错误码
请求失败时,API 返回 JSON 格式的错误响应:
{
"error": {
"code": "insufficient_credits",
"message": "Your credit balance is too low. Please top up.",
"type": "billing_error"
}
}
错误码一览
| HTTP 状态码 | 错误码 | 类型 | 说明 |
|---|---|---|---|
| 400 | invalid_request | invalid_request_error | 缺少必填参数或参数无效 |
| 401 | invalid_api_key | authentication_error | API Key 无效、已禁用或已删除 |
| 402 | insufficient_credits | billing_error | 积分余额不足,请充值 |
| 403 | ip_not_allowed | permission_error | 请求 IP 不在 Key 的白名单中 |
| 404 | model_not_found | invalid_request_error | 模型不存在或已停用 |
| 404 | task_not_found | invalid_request_error | 任务 ID 不存在 |
| 429 | rate_limit_exceeded | rate_limit_error | 请求过于频繁,请降低频率 |
| 429 | spend_limit_exceeded | billing_error | 达到 Key 的消费限额(每小时/每天/总量) |
| 500 | internal_error | api_error | 服务器内部错误 |
| 503 | upstream_error | upstream_error | 上游 AI 服务返回错误 |
常见场景
invalid_request (400)
缺少必填字段或参数格式错误时返回。
{
"error": {
"code": "invalid_request",
"message": "'model' is required.",
"type": "invalid_request_error"
}
}
insufficient_credits (402)
积分不足。可通过 GET /v1/credits 查询余额,前往 Dashboard > Billing 充值。
invalid_api_key (401)
可能原因:
- Key 不以
sk-开头 - Key 已被禁用或删除
- 用户账户已被封禁
upstream_error (503)
上游 AI 服务返回错误,可能原因:
- 输入内容包含敏感或违规信息
- 上游服务暂时不可用
- 请求参数不被上游支持
因上游错误导致任务失败时,预扣积分会自动退还。