跳到正文

视频生成

本指南介绍视频生成的调用流程与常见用法。每个参数的取值、各模型的时长与分辨率、素材上限和错误码请查阅 API 参考。

  1. 准备密钥在控制台的“API 密钥”中创建密钥。
  2. 选择模型查询当前密钥可调用的模型。
  3. 创建任务填写模型、提示词和可选素材。
  4. 轮询状态每隔 3–5 秒查询一次任务状态。
  5. 获取视频用 metadata.url 中的地址播放或下载视频。

1. 准备密钥

在控制台创建 API Key 并保存在服务端。请求发送到 https://ling-light.com,携带 Authorization: Bearer <API_KEY>。创建任务时建议带上 Idempotency-Key,超时重试时保持不变,避免重复创建和扣费。

在服务端终端设置环境变量bash
export VIDEO_API_BASE="https://ling-light.com"
export VIDEO_API_KEY="sk-your-api-key"
# 每个新业务请求生成一次;重试时沿用同一个值。
export VIDEO_REQUEST_ID="$(uuidgen)"

2. 选择模型

  • Seedance 2.0 / 2.5 系列:支持文生视频、首尾帧和参考素材(seedance2.0-mini 不支持首尾帧),2.5 系列可使用更多参考素材;按秒计费。
  • Seedance 2.5 LL:支持文生视频和参考素材,不支持首尾帧与延长 / 编辑;按条计费,必须传 resolution。
  • 各模型可用的分辨率、时长范围和能力会随配置变化,请以 API 参考中的实时模型表为准。
创建视频 · 参数与模型表
查看每个模型的分辨率、时长与素材上限。
模型列表与价格
查看各分辨率的价格。

3. 创建视频任务

POST /v1/videos 接收模型、提示词和可选素材,生成模式由素材自动判断:不传素材为文生视频;传入首帧和尾帧为首尾帧生成;传入参考图片、视频或音频为参考素材生成。

文生视频

创建视频任务
curl --request POST "$VIDEO_API_BASE/v1/videos" \
  --header "Authorization: Bearer $VIDEO_API_KEY" \
  --header "Content-Type: application/json" \
  --header "Idempotency-Key: $VIDEO_REQUEST_ID" \
  --data '{
  "model": "seedance2.0",
  "prompt": "一只橘猫坐在窗台上看雨,镜头缓慢推近",
  "seconds": 5,
  "resolution": "720p",
  "aspect_ratio": "16:9"
}'
const baseUrl = process.env.VIDEO_API_BASE
const apiKey = process.env.VIDEO_API_KEY
if (!baseUrl || !apiKey) throw new Error('Set VIDEO_API_BASE and VIDEO_API_KEY first')

async function call(path, init = {}) {
  const response = await fetch(new URL(path, baseUrl), {
    ...init,
    signal: AbortSignal.timeout(30_000),
    headers: {
      Authorization: `Bearer ${apiKey}`,
      Accept: 'application/json',
      ...(init.body ? { 'Content-Type': 'application/json' } : {}),
      ...init.headers,
    },
  })
  const body = await response.json()
  if (!response.ok || body.error) {
    throw new Error(`HTTP ${response.status}: ${body.message || body.error?.message || 'Request failed'}`)
  }
  return body
}

// 重试同一业务请求时,沿用此 ID 和完全相同的请求体。
const requestId = process.env.VIDEO_REQUEST_ID
if (!requestId) throw new Error('Set VIDEO_REQUEST_ID first')
const task = await call('/v1/videos', {
  method: 'POST',
  headers: { 'Idempotency-Key': requestId },
  body: JSON.stringify({
  "model": "seedance2.0",
  "prompt": "一只橘猫坐在窗台上看雨,镜头缓慢推近",
  "seconds": 5,
  "resolution": "720p",
  "aspect_ratio": "16:9"
}),
})
console.log(task)
创建任务的响应 · 保存 idjson
{
  "id": "task_xxxxxxxxxxxxxxxx",
  "object": "video",
  "model": "seedance2.0",
  "status": "queued",
  "progress": 0,
  "seconds": "5",
  "metadata": {
    "resolution": "720p",
    "aspect_ratio": "16:9"
  }
}

首尾帧生成

在 references 中放一张 first_frame 图片和一张 last_frame 图片,二者必须成对出现,且不能再混入其他参考素材。aspect_ratio 可传 auto(也接受 adaptive),由模型按首尾帧选择画幅;seedance2.5 系列(含 -flash、-vip)的首尾帧只支持 auto / adaptive,省略时自动使用 adaptive。seedance2.0-mini 不支持首尾帧。

首尾帧请求 · 替换图片示例地址
curl --request POST "$VIDEO_API_BASE/v1/videos" \
  --header "Authorization: Bearer $VIDEO_API_KEY" \
  --header "Content-Type: application/json" \
  --header "Idempotency-Key: $VIDEO_REQUEST_ID" \
  --data '{
  "model": "seedance2.0",
  "prompt": "人物从 @图片1 自然走到 @图片2 的位置,镜头保持稳定",
  "seconds": 5,
  "resolution": "720p",
  "aspect_ratio": "16:9",
  "references": [
    {
      "type": "image",
      "role": "first_frame",
      "source": "https://cdn.example.com/first-frame.png"
    },
    {
      "type": "image",
      "role": "last_frame",
      "source": "https://cdn.example.com/last-frame.png"
    }
  ]
}'
const baseUrl = process.env.VIDEO_API_BASE
const apiKey = process.env.VIDEO_API_KEY
if (!baseUrl || !apiKey) throw new Error('Set VIDEO_API_BASE and VIDEO_API_KEY first')

async function call(path, init = {}) {
  const response = await fetch(new URL(path, baseUrl), {
    ...init,
    signal: AbortSignal.timeout(30_000),
    headers: {
      Authorization: `Bearer ${apiKey}`,
      Accept: 'application/json',
      ...(init.body ? { 'Content-Type': 'application/json' } : {}),
      ...init.headers,
    },
  })
  const body = await response.json()
  if (!response.ok || body.error) {
    throw new Error(`HTTP ${response.status}: ${body.message || body.error?.message || 'Request failed'}`)
  }
  return body
}

// 重试同一业务请求时,沿用此 ID 和完全相同的请求体。
const requestId = process.env.VIDEO_REQUEST_ID
if (!requestId) throw new Error('Set VIDEO_REQUEST_ID first')
const task = await call('/v1/videos', {
  method: 'POST',
  headers: { 'Idempotency-Key': requestId },
  body: JSON.stringify({
  "model": "seedance2.0",
  "prompt": "人物从 @图片1 自然走到 @图片2 的位置,镜头保持稳定",
  "seconds": 5,
  "resolution": "720p",
  "aspect_ratio": "16:9",
  "references": [
    {
      "type": "image",
      "role": "first_frame",
      "source": "https://cdn.example.com/first-frame.png"
    },
    {
      "type": "image",
      "role": "last_frame",
      "source": "https://cdn.example.com/last-frame.png"
    }
  ]
}),
})
console.log(task)

参考素材生成

素材使用公网 HTTPS 直链或素材库中的 asset://<素材ID>。提示词可以用 @图片1、@视频1、@音频1 指代素材,同类素材按提交顺序从 1 编号。

混合参考素材请求
curl --request POST "$VIDEO_API_BASE/v1/videos" \
  --header "Authorization: Bearer $VIDEO_API_KEY" \
  --header "Content-Type: application/json" \
  --header "Idempotency-Key: $VIDEO_REQUEST_ID" \
  --data '{
  "model": "seedance2.5",
  "prompt": "@图片1 中的女孩转头看向镜头,语气参考 @音频1,动作节奏参考 @视频1",
  "seconds": 5,
  "resolution": "720p",
  "aspect_ratio": "16:9",
  "references": [
    {
      "type": "image",
      "role": "reference",
      "source": "https://cdn.example.com/girl.png"
    },
    {
      "type": "audio",
      "role": "reference",
      "source": "https://cdn.example.com/voice.mp3"
    },
    {
      "type": "video",
      "role": "reference",
      "source": "https://cdn.example.com/motion.mp4"
    }
  ]
}'
const baseUrl = process.env.VIDEO_API_BASE
const apiKey = process.env.VIDEO_API_KEY
if (!baseUrl || !apiKey) throw new Error('Set VIDEO_API_BASE and VIDEO_API_KEY first')

async function call(path, init = {}) {
  const response = await fetch(new URL(path, baseUrl), {
    ...init,
    signal: AbortSignal.timeout(30_000),
    headers: {
      Authorization: `Bearer ${apiKey}`,
      Accept: 'application/json',
      ...(init.body ? { 'Content-Type': 'application/json' } : {}),
      ...init.headers,
    },
  })
  const body = await response.json()
  if (!response.ok || body.error) {
    throw new Error(`HTTP ${response.status}: ${body.message || body.error?.message || 'Request failed'}`)
  }
  return body
}

// 重试同一业务请求时,沿用此 ID 和完全相同的请求体。
const requestId = process.env.VIDEO_REQUEST_ID
if (!requestId) throw new Error('Set VIDEO_REQUEST_ID first')
const task = await call('/v1/videos', {
  method: 'POST',
  headers: { 'Idempotency-Key': requestId },
  body: JSON.stringify({
  "model": "seedance2.5",
  "prompt": "@图片1 中的女孩转头看向镜头,语气参考 @音频1,动作节奏参考 @视频1",
  "seconds": 5,
  "resolution": "720p",
  "aspect_ratio": "16:9",
  "references": [
    {
      "type": "image",
      "role": "reference",
      "source": "https://cdn.example.com/girl.png"
    },
    {
      "type": "audio",
      "role": "reference",
      "source": "https://cdn.example.com/voice.mp3"
    },
    {
      "type": "video",
      "role": "reference",
      "source": "https://cdn.example.com/motion.mp4"
    }
  ]
}),
})
console.log(task)

延长与编辑

支持延长 / 编辑的模型可在请求中加 task_mode:extend 从原视频结尾续拍,seconds 填新增秒数;edit 修改原视频内容,seconds 填原视频时长。两种模式的 references 中都只放一段原视频。

延长请求体示例json
{
  "model": "seedance2.5",
  "task_mode": "extend",
  "prompt": "沿用 @视频1 的场景和镜头风格,从片尾自然续拍 5 秒",
  "seconds": 5,
  "resolution": "720p",
  "aspect_ratio": "16:9",
  "references": [
    {
      "type": "video",
      "role": "reference",
      "source": "https://cdn.example.com/source.mp4"
    }
  ]
}

4. 轮询任务状态

保存返回的 id,每隔 3–5 秒查询一次,同一任务只保留一个轮询循环。以 status 判断是否结束:queued 和 in_progress 继续等待,completed 读取结果,failed 读取 error.message。客户端超时后继续查询原任务,不要重新创建。

查询任务 · Node.js 自动轮询
# 替换为创建任务响应中的 id。
export VIDEO_TASK_ID="task_xxxxxxxxxxxxxxxx"
# 每 3–5 秒执行一次,直到状态为 completed 或 failed。
curl "$VIDEO_API_BASE/v1/videos/$VIDEO_TASK_ID" \
  --header "Authorization: Bearer $VIDEO_API_KEY"
const baseUrl = process.env.VIDEO_API_BASE
const apiKey = process.env.VIDEO_API_KEY
if (!baseUrl || !apiKey) throw new Error('Set VIDEO_API_BASE and VIDEO_API_KEY first')

async function call(path, init = {}) {
  const response = await fetch(new URL(path, baseUrl), {
    ...init,
    signal: AbortSignal.timeout(30_000),
    headers: {
      Authorization: `Bearer ${apiKey}`,
      Accept: 'application/json',
      ...(init.body ? { 'Content-Type': 'application/json' } : {}),
      ...init.headers,
    },
  })
  const body = await response.json()
  if (!response.ok || body.error) {
    throw new Error(`HTTP ${response.status}: ${body.message || body.error?.message || 'Request failed'}`)
  }
  return body
}

const taskId = process.env.VIDEO_TASK_ID
if (!taskId) throw new Error('Set VIDEO_TASK_ID to the id from the create response')
const deadline = Date.now() + 20 * 60_000
let task
while (Date.now() < deadline) {
  task = await call(`/v1/videos/${encodeURIComponent(taskId)}`)
  if (task.status === 'failed') throw new Error(task.error?.message || 'Video generation failed')
  if (task.status === 'completed') break
  await new Promise(resolve => setTimeout(resolve, 4000))
}
if (task?.status !== 'completed') {
  throw new Error('Polling stopped after 20 minutes. Resume querying the same task ID; do not create another task.')
}
console.log(task.metadata?.url)

5. 获取视频

任务完成的响应 · 读取 metadata.urljson
{
  "id": "task_xxxxxxxxxxxxxxxx",
  "object": "video",
  "model": "seedance2.0",
  "status": "completed",
  "progress": 100,
  "seconds": "5",
  "metadata": {
    "resolution": "720p",
    "aspect_ratio": "16:9",
    "url": "https://media.example.com/videos/task_xxxxxxxxxxxxxxxx.mp4"
  }
}

直接使用 metadata.url 下载:公共存储直链无需 API Key,本站 /v1/videos/{id}/content 地址需在服务端携带 API Key。需要长期保留的视频请及时保存到自己的存储。

错误与重试

  • 请求被拒绝时读取 message,按提示修正参数;被拒绝的请求不会创建任务,也不会扣费。
  • 超时、断连或 5xx 时任务可能已创建。用原 Idempotency-Key 和相同请求体重试,会返回原任务而不是重复创建。
  • 遇到 429 时按 Retry-After 等待后再查询或提交。
API 参考 · 创建视频
全部请求参数、各模型限制与错误码。
图片生成
GPT Image 与 Seedream 图片生成。
最后更新