> ## Documentation Index
> Fetch the complete documentation index at: https://docs.apimart.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# MAGI-2 视频生成

> 支持文生视频、首尾帧图生视频和图片、视频、音频参考生成。输出固定 1080p，时长 4–30 秒，默认生成声音。

```bash cURL theme={null}
curl --request POST \
  --url https://api.apimart.ai/v1/videos/generations \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --data '{
  "model": "magi-2",
  "prompt": "雨夜街道上一只红色纸船顺着水沟漂流，微距镜头，雨声淅沥",
  "duration": 5,
  "aspect_ratio": "9:16",
  "generate_audio": true,
  "nsfw_check": true
}'
```

```python Python theme={null}
import requests

payload = {
  "model": "magi-2",
  "prompt": "雨夜街道上一只红色纸船顺着水沟漂流，微距镜头，雨声淅沥",
  "duration": 5,
  "aspect_ratio": "9:16",
  "generate_audio": True,
  "nsfw_check": True
}
response = requests.post(
    "https://api.apimart.ai/v1/videos/generations",
    headers={"Authorization": "Bearer <token>"},
    json=payload,
    timeout=60,
)
response.raise_for_status()
print(response.json())
```

```javascript JavaScript theme={null}
const payload = {
  "model": "magi-2",
  "prompt": "雨夜街道上一只红色纸船顺着水沟漂流，微距镜头，雨声淅沥",
  "duration": 5,
  "aspect_ratio": "9:16",
  "generate_audio": true,
  "nsfw_check": true
};
const response = await fetch("https://api.apimart.ai/v1/videos/generations", {
  method: "POST",
  headers: {
    "Authorization": "Bearer <token>",
    "Content-Type": "application/json"
  },
  body: JSON.stringify(payload)
});
if (!response.ok) throw new Error(await response.text());
console.log(await response.json());
```

```json 200 theme={null}
{
  "code": 200,
  "data": [
    {
      "status": "submitted",
      "task_id": "task_example_magi2"
    }
  ]
}
```

MAGI-2 是 Sand AI 的视频生成模型。通过 `POST /v1/videos/generations` 提交任务，成功后从 `data[0].task_id` 获取任务 ID，再调用[任务查询接口](https://docs.apimart.ai/cn/api-reference/tasks/status.md)获取结果。

**Authorization** `string` *required*

使用 `Authorization: Bearer <token>`，其中 `<token>` 为你的 APIMart API Key。

## 生成方式

| 方式 | 如何提交 |
| - | - |
| 文生视频 | 提供 `prompt`，不传图片、视频或音频素材。 |
| 图生视频 | 使用 `image_urls` 传首帧和可选尾帧，或使用独立的首尾帧字段。 |
| 参考生视频 | 传 `video_urls`、`audio_urls` 或 `role: "reference_image"` 的图片会自动进入参考模式。只有 `image_urls` 时，显式传 `omni_reference_task_type: "reference"` 才会将其作为参考图。 |

模式先按传入的素材判断。自动进入参考模式与显式指定 `reference` 的行为一致；进入参考模式后，所有图片都按参考图处理，`image_urls` 与 `image_with_roles` 中 `role: "reference_image"` 的图片**合计最多 10 张**。

参考生成示例统一显式传 `omni_reference_task_type: "reference"`。在提示词中说明每份素材的用途；参考素材用于引导生成，不会直接拼接参考视频或原样保留参考音频。

## 请求参数

**model** `string` *required*

`magi-2`

**nsfw\_check** `boolean` (default: false)

是否在提交视频任务前执行内容审核。

* `true`：使用 `omni-moderation-latest` 审核提示词和输入图片
* `false` 或不传：不发起审核请求，不增加审核成本与延迟（默认）

**prompt** `string` *required*

三种方式都必填，最多 15,000 字符。描述画面、动作、镜头与声音，台词可放在引号中。

**duration** `integer` (default: 5)

整数秒 `4`–`30`，默认 `5`。显式传 `0`、小数或范围外的值会返回 HTTP 400；使用默认值时省略该字段。

**seconds** `integer`

`duration` 的别名。两者同时传入时必须相等。

**resolution** `string` (default: 1080p)

固定为 `1080p`，省略时同样输出 1080p；其他值返回 HTTP 400。

**aspect\_ratio** `string`

文生视频与参考生视频支持 `16:9`（默认）、`9:16`、`1:1`、`4:3`、`3:4`、`21:9`。图生视频支持前五种比例，不支持 `21:9`；省略或传 `auto` 时跟随首帧比例。跟随比例不会改变固定 1080p 的输出规格。

**generate\_audio** `boolean` (default: true)

是否生成声音，默认 `true`；设为 `false` 时不生成音轨。与 `audio` 同时传入时必须一致。

**audio** `boolean`

`generate_audio` 的别名，不能与其取值冲突。参考音频应通过 `audio_urls` 传入。

**seed** `integer`

支持整数 `0`–`2147483647`；省略或传 `-1` 时随机。固定种子有助于让相同参数的结果更接近，不保证完全一致。

**omni\_reference\_task\_type** `string` (default: auto)

支持 `auto`（默认）和 `reference`。将多张 `image_urls` 图片用作参考时，请显式传 `reference`；不支持 `edit` 或 `extend`。

**image\_urls** `string[]`

每项均为公网 HTTP(S) 图片 URL。只有按图生视频处理时才最多 2 张：第 1 张为首帧，第 2 张为可选尾帧。图生条件是仅传图片、未传参考视频或音频、未指定 `reference`，且未使用 `reference_image` 角色。进入参考模式后，本数组全部作为参考图，与 `image_with_roles` 中 `reference_image` 图片合计最多 10 张；自动参考与显式 `reference` 使用同一上限。

**first\_frame\_image** `string`

首帧图片的公网 HTTP(S) URL。独立首尾帧字段与 `image_urls` 二选一。

**last\_frame\_image** `string`

可选尾帧图片的公网 HTTP(S) URL，需与首帧配合使用。独立首尾帧字段与 `image_urls` 二选一。

**image\_with\_roles** `object[]`

每项包含 `url` 和 `role`。`url` 为公网 HTTP(S) 图片 URL；`role` 支持 `first_frame`、`last_frame`、`reference_image`。图生模式的首帧和尾帧各最多 1 张。`reference_image` 会触发参考模式，其图片与 `image_urls` 中的图片合计最多 10 张。

**video\_urls** `string[]`

最多 5 段参考视频，每段 2–30 秒，总时长应不超过 30 秒；超出的部分可能被上游截断。使用公网 HTTP(S) URL 数组，不支持单独的 `video_url` 字段。

**audio\_urls** `string[]`

最多 5 段参考音频，每段 2–30 秒，总时长应不超过 30 秒；超出的部分可能被上游截断。每项为公网 HTTP(S) URL。

## 素材要求

所有素材必须是公网可直接访问的 HTTP(S) 直链。需要登录、带防盗链或无法下载的地址可能导致任务失败。本地文件可先通过[图片上传](https://docs.apimart.ai/cn/api-reference/uploads/images.md)、[视频上传](https://docs.apimart.ai/cn/api-reference/uploads/videos.md)或[音频上传](https://docs.apimart.ai/cn/api-reference/uploads/audios.md)获取 URL。

## 请求示例

### 首帧与尾帧

```json theme={null}
{
  "model": "magi-2",
  "prompt": "女孩慢慢转身，平滑过渡到尾帧的姿态，柔光",
  "image_urls": [
    "https://example.com/start.jpg",
    "https://example.com/end.jpg"
  ],
  "duration": 5
}
```

只传 1 张图片时使用首帧生成；不传 `aspect_ratio` 时跟随首帧比例。也可用 `first_frame_image` 与 `last_frame_image` 代替示例中的 `image_urls`。

### 仅图片参考

```json theme={null}
{
  "model": "magi-2",
  "prompt": "让两张参考图中的角色在同一座花园里相遇，保持各自的外观特征",
  "image_urls": [
    "https://example.com/reference-1.jpg"
  ],
  "image_with_roles": [
    {
      "url": "https://example.com/reference-2.jpg",
      "role": "reference_image"
    }
  ],
  "omni_reference_task_type": "reference",
  "aspect_ratio": "16:9",
  "duration": 5
}
```

### 图片、视频与音频混合参考

```json theme={null}
{
  "model": "magi-2",
  "prompt": "参考视频的运镜、参考图里女孩的样貌和参考音频的音乐风格，生成她在日落海边散步的画面",
  "image_urls": [
    "https://example.com/girl.jpg"
  ],
  "video_urls": [
    "https://example.com/camera.mp4"
  ],
  "audio_urls": [
    "https://example.com/bgm.mp3"
  ],
  "omni_reference_task_type": "reference",
  "aspect_ratio": "16:9",
  "duration": 8
}
```

## 查询任务与结果

通过[任务查询接口](https://docs.apimart.ai/cn/api-reference/tasks/status.md)轮询至 `completed` 或 `failed`。生成通常需要几分钟，服务繁忙时可能更久；不要把某个固定等待时间当作任务已经失败。停止查询不会取消任务。

```bash theme={null}
curl --request GET \
  --url 'https://api.apimart.ai/v1/tasks/task_example_magi2' \
  --header 'Authorization: Bearer <token>'
```

| 字段 | 说明 |
| - | - |
| `data.status` | `pending` / `processing` 表示处理中；`completed` 表示成功；`failed` 表示失败。 |
| `data.progress` | 进度，范围 0–100。 |
| `data.result.videos[0].url` | 成片下载地址，已转存至平台存储；完整结构见任务查询文档。 |
| `data.error.message` | 任务失败时的错误说明。 |

## 不支持的功能

以下内容会在提交时返回 HTTP 400：`mode`、`negative_prompt`、`watermark: true`、`camera_control`、`multi_shot` / `multi_prompt`、`element_list`、`video_list`、`voice_list`、`audio_setting`、中间帧 `keyframe`、`omni_reference_task_type: "edit"` / `"extend"`、单独的 `video_url`，以及 `1080p` 以外的 `resolution`。本模型不提供视频编辑或续写。

## 常见错误

提交失败时读取 `error.message`。任务已创建后失败时，读取任务查询结果中的 `data.error.message`。

| 错误信息或场景 | 处理方式 |
| - | - |
| `duration must be between 4 and 30 whole seconds` | 使用 4–30 的整数秒；省略时默认为 5。 |
| `duration (5) and seconds (6) are aliases and must match` | 两个时长字段保持相同，或只传一个。 |
| `requires prompt` | 三种生成方式都必须填写提示词。 |
| `image_urls takes at most 2 images` | 图生最多传首尾帧两张；仅图片参考时显式传 `reference`。 |
| `image-to-video only supports aspect_ratio` | 图生移除 `21:9`；省略比例或传 `auto` 可跟随首帧。 |
| `accepts at most 10 reference images` / `5 videos` / `5 audios` | 参考图片最多 10 张、视频最多 5 段、音频最多 5 段。 |
| `has no video edit / extend` / `does not support keyframes` | 移除编辑、续写和中间帧参数；参考视频使用 `video_urls`。 |
| `always outputs 1080p` | 只传 `1080p`，或省略 `resolution`。 |
| `audio (true) and generate_audio (false) are aliases and must match` | 两个声音开关保持一致，或只传一个。 |
| `Could not download the input...` | 检查素材 URL 是否公开且可直接下载，再重新提交。 |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.