# MAGI-2影片生成

Sand.ai 音畫同出影片模型 `magi-2` 原生支援 **1080p 高畫質、4–30 秒整數時長與預設同步音訊**，涵蓋文生影片、首幀/首尾幀及圖/視/音多模態參考生成。

> [!NOTE]
> 範例中的模型 ID 僅供參考，實際呼叫請以 [模型廣場](/models) 對應的真實 ID 為準。

* **OpenAI 相容協定**：提交 `POST /v1/video/generations`，查詢 `GET /v1/video/generations/{task_id}`（或 `/v1/tasks/{task_id}`）
* **Sand.ai 官方原生協定**：提交 `POST /v2/generations`，查詢 `GET /v2/generations/{id}`
* **Sand.ai Ark 相容協定**：提交 `POST /compatible/ark/api/v3/contents/generations/tasks`，查詢 `GET /compatible/ark/api/v3/contents/generations/tasks/{id}`（原生與 Ark 範例見 **[MAGI-2 原生介面](/docs/zh-TW/ali-kling/magi-native)**）

> [!NOTE]
> * **規格預設值**：解析度固定 `1080p`；時長 `4`–`30` 秒整數（預設 `5`）；`generate_audio` / `generateAudio` 預設 `true`。
> * **素材要求**：參考圖/視/音須為公開 `https://` 直連網址（圖片 ≤ 10 張，影片 ≤ 5 段且總長 ≤ 30s，音訊 ≤ 5 段且總長 ≤ 30s）。
> * **控幀與多參區分**：傳入 `first_frame_url` / `last_frame_url`（或單張 `images`）按首幀/首尾幀生成；`images` 傳 2 張以上純 URL 預設按多圖參考處理。
> * **禁傳 `watermark`**：官方協定禁傳 `watermark` 欄位；查詢成功均回傳免驗證直連網址。

---

## 1. 提交影片生成任務 (POST)

* **請求路徑**: `https://www.roookey.com/v1/video/generations`
* **驗證標頭**: `Authorization: Bearer sk-your_token_here`

### A. 文生影片
```bash
curl -X POST https://www.roookey.com/v1/video/generations \
  -H "Authorization: Bearer sk-your_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "magi-2",
    "prompt": "雨夜賽博龐克街頭，霓虹倒映在潮濕路面，鏡頭緩推，伴隨細雨與遠處車流聲",
    "duration": 5,
    "ratio": "16:9",
    "resolution": "1080p",
    "generate_audio": true
  }'
```

### B. 首幀 / 首尾幀圖生影片
```bash
curl -X POST https://www.roookey.com/v1/video/generations \
  -H "Authorization: Bearer sk-your_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "magi-2",
    "prompt": "畫面從清晨雪山平滑過渡到金色日落雲海，鏡頭緩升，風聲漸起",
    "first_frame_url": "https://example.com/first_frame.png",
    "last_frame_url": "https://example.com/last_frame.png",
    "duration": 8,
    "ratio": "16:9"
  }'
```

### C. 多模態參考生影片（圖片 / 影片 / 音訊）
```bash
curl -X POST https://www.roookey.com/v1/video/generations \
  -H "Authorization: Bearer sk-your_token_here" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "magi-2",
    "prompt": "保持參考圖角色與參考影片運鏡，在海邊木棧道緩步前行，融合參考音訊旋律",
    "images": ["https://example.com/char.png", "https://example.com/scene.png"],
    "videos": ["https://example.com/motion.mp4"],
    "audios": ["https://example.com/bgm.mp3"],
    "duration": 10,
    "ratio": "16:9"
  }'
```

---

## 2. 輪詢任務結果 (GET)

```bash
curl -X GET https://www.roookey.com/v1/video/generations/task_01jxyz... \
  -H "Authorization: Bearer sk-your_token_here"
```

---

## 3. 參數字典說明

| OpenAI 相容參數 | Sand.ai 原生 (`/v2/generations`) | Ark 相容 (`/compatible/ark/...`) | 類型 | 必填 | 預設值 | 說明 |
| :--- | :--- | :--- | :--- | :--- | :--- | :--- |
| `model` | `model` | `model` | `string` | **是** | - | 模型 ID，如 `magi-2`。 |
| `prompt` | `parts[type="text"].text` | `content[type="text"].text` | `string` | **是** | - | 提示詞文字（≤ 15,000 字元）。 |
| `first_frame_url` / `last_frame_url` | - | `content[type="image_url"]` (`role: "first_frame" \| "last_frame"`) | `string` | 否 | - | 首幀 / 尾幀公開圖片 URL。 |
| `images` / `image_urls` | `parts[type="reference_image"].image.url` | `content[type="image_url"]` (`role: "reference_image"`) | `array` | 否 | - | 圖片 URL 列表（≤ 10 張；單張預設首幀，2+ 張預設參考）。 |
| `videos` / `video_urls` | `parts[type="reference_video"].video.url` | `content[type="video_url"].video_url.url` | `array` | 否 | - | 參考影片 URL 列表（≤ 5 段，總長 ≤ 30s）。 |
| `audios` / `audio_urls` | `parts[type="reference_audio"].audio.url` | `content[type="audio_url"].audio_url.url` | `array` | 否 | - | 參考音訊 URL 列表（≤ 5 段，總長 ≤ 30s）。 |
| `duration` | `duration` | `duration` | `integer` | 否 | `5` | 生成時長（秒），支援 `4`–`30` 整數。 |
| `ratio` / `aspect_ratio` | `aspectRatio` | `ratio` | `string` | 否 | `"16:9"` | 比例：`"21:9"`、`"16:9"`、`"4:3"`、`"1:1"`、`"3:4"`、`"9:16"`。 |
| `resolution` | `resolution` | `resolution` | `string` | 否 | `"1080p"` | 解析度，僅 `"1080p"`。 |
| `generate_audio` | `generateAudio` | `generate_audio` | `boolean` | 否 | `true` | 是否同步生成音訊。 |
| `enhance_prompt` | `enhancePrompt` | `enhance_prompt` | `boolean` | 否 | `false` | 是否開啟提示詞增強。 |
| `seed` | `seed` | `seed` | `integer` | 否 | `-1` | 隨機種子（`-1` 隨機，或 `0`–`4294967295`）。 |

---

## 4. 回傳結果範例

```json
{
  "id": "task_01jxyz1234567890",
  "object": "video.generation",
  "status": "completed",
  "created": 1796378400,
  "data": [{ "url": "https://cdn.example.com/videos/magi2_output.mp4" }]
}
```



### 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` 追溯。
