# WAN 影片生成

OpenAI 相容：`POST /v1/video/generations`。原生路徑：`/api/v1/services/aigc/video-generation/video-synthesis`。[影片生成 API](https://help.aliyun.com/zh/model-studio/wan3-video-generation-api-reference?spm=a2c4g.11186623.help-menu-2400256.d_2_3_1_0.204b56c3vDOeBa&scm=20140722.H_3049634._.OR_help-T_cn~zh-V_1)

- 提交：`POST /v1/video/generations`
- 輪詢：`GET /v1/video/generations/{task_id}` 或 `GET /v1/tasks/{task_id}`

> **場景互斥**：首尾影格（`first_frame` / `last_frame`）與參考類素材（`reference_*`、`file`、`link`）不可混用。

---

## 1. OpenAI 相容串接 (POST)

* **路徑**: `https://www.tokenpad.ai/v1/video/generations`
* **驗證**: `Authorization: Bearer sk-your_token`

### A. 文字生成影片

```bash
curl -X POST https://www.tokenpad.ai/v1/video/generations \
  -H "Authorization: Bearer sk-your_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "wan3.0-video",
    "prompt": "一隻黃金獵犬在金色秋葉中奔跑，電影感慢鏡頭，陽光穿透樹林",
    "resolution": "1080P",
    "ratio": "16:9",
    "duration": 5
  }'
```

### B. 圖片生成影片（首影格 / 首尾影格）

1 張圖 → 首影格；2 張圖 → 首尾影格。亦可明確傳入 `{ "url", "role" }`（`role` 可寫為 `type`）。

```bash
curl -X POST https://www.tokenpad.ai/v1/video/generations \
  -H "Authorization: Bearer sk-your_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "wan3.0-video",
    "prompt": "鏡頭緩慢推進，人物自然轉身，光影連貫",
    "images": [
      { "url": "https://example.com/first.png", "role": "first_frame" },
      { "url": "https://example.com/last.png", "role": "last_frame" }
    ],
    "resolution": "720P",
    "duration": 5
  }'
```

### C. 多圖參考生成影片

```bash
curl -X POST https://www.tokenpad.ai/v1/video/generations \
  -H "Authorization: Bearer sk-your_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "wan3.0-video",
    "prompt": "參考圖一角色外形與圖二街景，平穩跟拍，角色自然行走",
    "images": [
      { "url": "https://example.com/character.png", "role": "reference_image" },
      { "url": "https://example.com/street.png", "role": "reference_image" }
    ],
    "resolution": "1080P",
    "ratio": "16:9",
    "duration": 5
  }'
```

### D. 圖片 + 影片 + 音訊多模態參考

至少需一張參考圖或一段參考影片；**不可僅傳入音訊**。

```bash
curl -X POST https://www.tokenpad.ai/v1/video/generations \
  -H "Authorization: Bearer sk-your_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "wan3.0-video",
    "prompt": "角色動作跟隨參考影片，嘴型與參考音訊一致",
    "images": [
      { "url": "https://example.com/character.png", "role": "reference_image" }
    ],
    "videos": ["https://example.com/motion_ref.mp4"],
    "audios": ["https://example.com/voice_ref.mp3"],
    "resolution": "1080P",
    "duration": 5
  }'
```

### E. 檔案 / 網頁連結參考

`files`、`links` 為 OpenAI 扁平擴充欄位；預設對應為 `file`、`link` role。

```bash
curl -X POST https://www.tokenpad.ai/v1/video/generations \
  -H "Authorization: Bearer sk-your_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "wan3.0-video",
    "prompt": "根據參考文件與產品頁內容生成講解影片",
    "files": ["https://example.com/brief.pdf"],
    "links": ["https://example.com/product-page"],
    "resolution": "1080P",
    "ratio": "16:9",
    "duration": 5
  }'
```

---

## 2. 輪詢結果 (GET)

```bash
curl -X GET https://www.tokenpad.ai/v1/video/generations/video_task_001 \
  -H "Authorization: Bearer sk-your_token_here"
```

---

## 3. OpenAI 相容參數字典

| 參數 | 型別 | 必填 | 說明 |
| :--- | :--- | :---: | :--- |
| `model` | `string` | 是 | 如 `wan3.0-video` |
| `prompt` | `string` | 是 | 文字描述 |
| `images` / `image_urls` | `array` | 否 | 見下方「媒體格式」與 role 表 |
| `videos` | `array` | 否 | 參考影片；預設 role `reference_video` |
| `audios` | `array` | 否 | 參考音訊；預設 role `reference_audio`；不可單獨作為唯一參考 |
| `files` | `array` | 否 | 參考檔案 URL；預設 role `file` |
| `links` | `array` | 否 | 參考網頁 URL；預設 role `link` |
| `resolution` / `size` | `string` | 否 | 如 `720P`、`1080P`（`size` 會轉大寫） |
| `ratio` | `string` | 否 | `16:9`、`9:16`、`1:1` 等 |
| `duration` | `integer` | 否 | 秒數，預設 `5` |

**媒體格式**（各 `images` / `videos` / `audios` / `files` / `links` 通用）：

- 純 URL 字串：`"https://..."`
- 帶 role 物件：`{ "url": "https://...", "role": "reference_image" }`（`role` 可寫為 `type`）

**`images` 數量預設 role**（未明確指定時）：

| 張數 | 預設 role |
| :--- | :--- |
| 1 | `first_frame` |
| 2 | `first_frame` + `last_frame` |
| 3+ | 全部 `reference_image` |

**全部 role 取值**：

| role | 來源欄位 | 說明 |
| :--- | :--- | :--- |
| `first_frame` | `images` / `image_urls` | 首影格 |
| `last_frame` | `images` / `image_urls` | 尾影格 |
| `reference_image` | `images` / `image_urls` | 參考圖 |
| `reference_video` | `videos` | 參考影片（萬相預設） |
| `reference_audio` | `audios` | 參考音訊 |
| `file` | `files` | 參考檔案 |
| `link` | `links` | 參考網頁 |

---

## 4. 阿里百煉原生路由（簡要）

| 能力 | 路徑 |
| :--- | :--- |
| 影片提交 | `POST /api/v1/services/aigc/video-generation/video-synthesis` |
| 任務查詢 | `GET /api/v1/tasks/{task_id}` |

已含官方 `input` / `parameters` 結構時可原樣透傳；非同步須 `X-DashScope-Async: enable`。

---

## 5. 回傳範例

**提交**：

```json
{
  "id": "video_task_001",
  "task_id": "video_task_001",
  "status": "pending"
}
```

**完成**：

```json
{
  "id": "video_task_001",
  "status": "completed",
  "data": [{ "url": "https://example.com/output/wan_video.mp4" }]
}
```

非同步輪詢 HTTP 200 時也可能出現 `"status": "failed"`，詳見 [閘道器常見錯誤碼與排解](/docs/zh-TW/errors/error-codes)。



### 5. 常見錯誤碼與問題排解

HTTP 狀態碼、錯誤主體格式及分情境排解已 **統一維護** 於專頁，避免與各呼叫範例重複導致不同步。

👉 **[閘道常見錯誤碼與排解](/docs/zh-TW/errors/error-codes)**

**速記**（詳情見專頁）：

| 碼 | 含義 |
| :---: | :--- |
| **400** | 參數無效 / 內容風控或版權攔截 |
| **402** | 帳戶餘額不足（**不是** 429） |
| **429** | 權杖 RPS/RPM 頻率限制，或低餘額在途任務過多 |
| **403** | 權杖/IP/模型權限（餘額見 402；內容攔截見 400） |
| **200 + `status: failed`** | 非同步影片等長任務的業務失敗（輪詢時常見） |

HTTP 回應碼與控制台呼叫日誌 `status_code` 一致，可依 `log_id` 追溯。

> **維護約定**：新增或調整錯誤說明時，請只更新專頁 `error-codes`；本附錄透過連結自動指向最新內容。
