Sora 2 视频生成 API
使用 OpenAI Sora 2 模型生成视频的 API,支持文生视频和图生视频模式。
在线体验: https://nanophoto.ai/sora-2
| 方法 | 路径 | 说明 |
|---|
| POST | /api/sora-2/generate | 生成视频 |
| POST | /api/sora-2/check-status | 查询任务状态 |
Authorization: Bearer YOUR_API_KEY
| 模型 | 时长 | 积分 |
|---|
sora2 | 10s | 4 |
sora2 | 15s | 8 |
sora2-pro-standard (720p) | 10s | 60 |
sora2-pro-standard (720p) | 15s | 100 |
sora2-pro-high (1080p) | 10s | 120 |
sora2-pro-high (1080p) | 15s | 240 |
POST /api/sora-2/generate
| Header | 值 |
|---|
Content-Type | application/json |
Authorization | Bearer YOUR_API_KEY |
| 参数 | 类型 | 必填 | 说明 |
|---|
prompt | string | 是 | 视频生成提示词 |
mode | string | 是 | textToVideo(文生视频)或 imageToVideo(图生视频) |
modelTier | string | 否 | sora2(默认)、sora2-pro-standard 或 sora2-pro-high |
aspectRatio | string | 否 | portrait(竖屏,默认)或 landscape(横屏) |
videoDuration | string | 否 | 10 或 15(秒) |
imageUrls | string[] | 条件必填 | 图生视频模式的图片公开 URL |
API 调用图生视频模式仅接受 imageUrls(公开 URL),不支持 Base64 图片上传。
curl -X POST "https://nanophoto.ai/api/sora-2/generate" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
--data-raw '{
"prompt": "一只金毛犬在日落时分的海滩上奔跑,电影级光影",
"mode": "textToVideo",
"modelTier": "sora2",
"aspectRatio": "landscape",
"videoDuration": "10"
}'
curl -X POST "https://nanophoto.ai/api/sora-2/generate" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
--data-raw '{
"prompt": "画中的人物动起来,并朗读诗词",
"mode": "imageToVideo",
"modelTier": "sora2",
"aspectRatio": "landscape",
"videoDuration": "10",
"imageUrls": ["https://static.nanophoto.ai/demo/nano-banana-pro.webp"]
}'
async function generateVideo() {
// 第一步:提交生成任务
const response = await fetch("https://nanophoto.ai/api/sora-2/generate", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer YOUR_API_KEY",
},
body: JSON.stringify({
prompt: "一只金毛犬在日落时分的海滩上奔跑",
mode: "textToVideo",
modelTier: "sora2",
aspectRatio: "landscape",
videoDuration: "10",
}),
});
const data = await response.json();
if (!data.success) {
console.error("生成失败:", data.error);
return;
}
// 第二步:轮询任务状态
const taskId = data.taskId;
while (true) {
await new Promise((resolve) => setTimeout(resolve, 5000)); // 等待 5 秒
const statusRes = await fetch(
"https://nanophoto.ai/api/sora-2/check-status",
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer YOUR_API_KEY",
},
body: JSON.stringify({ taskId }),
}
);
const status = await statusRes.json();
if (status.status === "completed") {
console.log("视频地址:", status.videoUrl);
return;
}
if (status.status === "failed") {
console.error("生成失败:", status.error);
return;
}
console.log("处理中...", status.progress);
}
}
generateVideo();
{
"success": true,
"status": "processing",
"taskId": "task_abc123",
"message": "Video generation in progress"
}
收到 status: "processing" 时,请每隔 5-10 秒轮询 check-status 接口。
{
"error": "Insufficient credits",
"errorCode": "INSUFFICIENT_CREDITS",
"requiredCredits": 4
}
POST /api/sora-2/check-status
| Header | 值 |
|---|
Content-Type | application/json |
Authorization | Bearer YOUR_API_KEY |
| 参数 | 类型 | 必填 | 说明 |
|---|
taskId | string | 是 | 生成接口返回的任务 ID |
curl -X POST "https://nanophoto.ai/api/sora-2/check-status" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
--data-raw '{"taskId": "task_abc123"}'
{
"success": true,
"status": "processing",
"progress": "generating",
"message": "Video generation generating",
"taskId": "task_abc123"
}
{
"success": true,
"status": "completed",
"videoUrl": "https://video.nanophoto.ai/sora/...",
"taskId": "task_abc123",
"generationTime": 120,
"creditsUsed": 4
}
{
"success": false,
"status": "failed",
"error": "Video generation failed",
"errorCode": "GENERATION_FAILED",
"taskId": "task_abc123"
}
- 每 5-10 秒 轮询一次
/api/sora-2/check-status
- 典型生成时间:
sora2 约 1-3 分钟,Pro 模型约 3-8 分钟
- 生成失败时积分会自动退还
| errorCode | HTTP 状态码 | 说明 |
|---|
LOGIN_REQUIRED | 401 | 需要认证 |
API_KEY_RATE_LIMIT_EXCEEDED | 429 | 超出频率限制(100 次/小时) |
INSUFFICIENT_CREDITS | 402 | 积分不足 |
PROMPT_REQUIRED | 400 | 缺少提示词 |
IMAGE_REQUIRED | 400 | 图生视频模式需要图片 |
IMAGE_URLS_REQUIRED | 400 | API 调用需要 imageUrls(不支持 base64) |
GENERATION_FAILED | 500 | 视频生成失败 |
TASK_ID_REQUIRED | 400 | 缺少任务 ID |
TASK_NOT_FOUND | 404 | 任务不存在或无权访问 |
INTERNAL_ERROR | 500 | 内部服务器错误 |