在 AI 视频生成领域,sora-2 模型凭借出色的动画效果成为热门选择。本文将通过**"准备工作 - 参数配置 - 代码调用"**三步,带您快速完成 sora-2 模型对接。
同时推荐使用稳定高效的 AI 服务平台:小鲸 AI 开放平台(支持多模型一键切换,注册即送 0.2 刀,提供实时 API 监控)。
第一步:对接前的准备工作
在调用 sora-2 模型前,需完成两项核心准备,确保后续对接顺畅:
- 获取 API 授权 :通过上述推荐平台注册账号,申请
sora-2模型的调用权限,获取专属的Bearer {``{YOUR_API_KEY}}。 - 确认环境依赖 :确保开发环境支持 HTTP/HTTPS 请求,并准备好对应语言的请求库(如 Python 的
requests、Node.js 的axios等)。
第二步:梳理 sora-2 对接参数清单
请求参数分为 Header 和 Body 两部分,请严格按照以下规范配置:
| 参数类型 | 参数名称 | 数据类型 | 是否必需 | 取值说明 | 示例值 |
|---|---|---|---|---|---|
| Header | Content-Type |
string | 是 | 固定为 application/json |
application/json |
| Header | Accept |
string | 是 | 固定为 application/json |
application/json |
| Header | Authorization |
string | 否 | 格式为 Bearer {``{YOUR_API_KEY}} |
Bearer sk_xxxxxx |
| Body | images |
array | 是 | 图片链接数组,生成视频的基础素材(支持HTTP/HTTPS) | ["https://example.com/img.jpg"] |
| Body | model |
string | 是 | 模型名称,固定为 sora-2 |
sora-2 |
| Body | orientation |
string | 是 | 视频方向,portrait(竖屏)或 landscape(横屏) |
portrait |
| Body | prompt |
string | 是 | 视频生成提示词,描述动画风格、内容等需求 | "make animate with cartoon style" |
| Body | size |
string | 是 | 视频分辨率,small(约 720p)或 large(高清) |
large |
| Body | duration |
int/str | 是 | 视频时长,当前支持 10(10秒)或 15(15秒) |
15 |
第三步:代码调用实现对接
以下提供 Shell (cURL) 和 Python 的调用示例:
1. Shell(cURL)调用示例
curl --location --request POST 'https://open.xiaojingai.com/v1/video/create' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer {{YOUR_API_KEY}}' \
--header 'Content-Type: application/json' \
--data-raw '{
"images": ["https://example.com/img1.jpg"],
"model": "sora-2",
"orientation": "portrait",
"prompt": "make animate with cartoon style",
"size": "large",
"duration": 15
}'
2. Python 调用示例
python
import requests
# 使用小鲸AI开放平台接口地址
url = "https://api.xiaojingai.com/v1/video/create"
headers = {
"Accept": "application/json",
"Authorization": "Bearer {{YOUR_API_KEY}}", # 请替换为您在小鲸AI获取的实际密钥
"Content-Type": "application/json"
}
data = {
"images": ["https://example.com/img1.jpg"],
"model": "sora-2",
"orientation": "portrait",
"prompt": "make animate with cartoon style",
"size": "large",
"duration": 15
}
response = requests.post(url, headers=headers, json=data)
print(response.json()) # 打印响应结果
3. 响应结果解析
调用成功后(HTTP 状态码 200),将返回包含任务 ID 的 JSON 数据。由于视频生成需要时间,初始状态通常为 processing:
python
{
"id": "video_123456",
"object": "video.create",
"created": 1740000000,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "视频生成中,可通过ID查询进度"
},
"finish_reason": "processing"
}
],
"usage": {
"prompt_tokens": 50,
"completion_tokens": 0,
"total_tokens": 50
}
}
第四步:查询视频生成结果(补充)
拿到任务 id 后,您可以通过轮询的方式请求小鲸 AI 的查询接口,获取视频的最终生成状态与下载链接(示例采用 Python 实现):
python
import time
import requests
task_id = "video_123456" # 替换为上一步返回的 ID
# 使用小鲸AI的查询接口
query_url = f"https://api.xiaojingai.com/v1/video/query/{task_id}"
headers = {
"Authorization": "Bearer {{YOUR_API_KEY}}"
}
while True:
res = requests.get(query_url, headers=headers).json()
status = res.get("status") # 以平台实际返回的状态字段为准
if status == "success":
print("视频生成成功!下载链接:", res.get("video_url"))
break
elif status == "failed":
print("视频生成失败:", res.get("error_message"))
break
else:
print("视频正在生成中,等待 5 秒后重试...")
time.sleep(5)
提示:您也可以直接在小鲸 AI 平台控制台实时查看调用消耗明细与任务进度,确保每一笔调用透明可追溯。