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

# 首帧生视频

> 用 Viggle H3 让静态图动起来。图片作为开场帧，提示词描述接下来发生的事。

首帧生视频以你提供的图片作为片段的开场，并从那里开始动起来。开场帧保留图片的主体、画面风格和构图，提示词决定动作和运镜。输出为 5–15 秒、带原生音频的 MP4。

可以用它让产品图、插画或生成的图片动起来。如果你也知道镜头应该如何结束，请使用[首尾帧生视频](https://docs.viggle.ai/zh/v1/video/first-last-frame-to-video.md)。

## 请求内容

以 `multipart/form-data` 调用 `POST /v1/videos`。发送首帧且不发送尾帧即选择此模式。

| 参数 | 必填 | 说明 |
| - | - | - |
| `prompt` | 是 | 开场帧之后应发生的内容。带图片时仍然必填 |
| `quality` | 是 | `low`（更快）或 `high`（保真度更高），价格相同。也可以用 [`ultrafast`](https://docs.viggle.ai/zh/v1/video/generate-video#ultrafast)，最快的档位，单独定价 |
| `first_frame_image` | 二选一 | 以文件上传的图片 |
| `first_frame_image_url` | 二选一 | 公开的 `http(s)://` URL，或 base64 `data:` URI |
| `aspect_ratio` | 否 | 默认 `16:9`。请设置为与图片一致 |
| `duration_s` | 否 | 5–15 秒，默认 `5` |

帧画面以文件或 URL 发送，不能同时发送。`resolution`、`enhance`、`seed` 和 `watermark` 的用法与其他模式相同，见[通用参数](https://docs.viggle.ai/zh/v1/video/generate-video#common-parameters)。

### 图片限制

| | 限制 |
| - | - |
| 上传大小 | 最大 20 MiB |
| URL 或 `data:` URI 大小 | 最大 30 MiB |
| 格式 | JPEG、PNG、WebP、GIF、BMP、TIFF、HEIC |
| 超大图片 | 长边达到 3840 px 及以上，或短边达到 2160 px 及以上的图片，会按原比例缩小到 1920×1080 以内（竖图为 1080×1920） |

首帧不能与参考图或参考视频同时使用，后者属于[参考生视频](https://docs.viggle.ai/zh/v1/video/reference-to-video.md)。

## 价格

与所有 Viggle H3 模式相同：按秒数和 `resolution` 计费，开启 `enhance` 时每秒另加 $0.01。图片免费。下面的示例（5 秒，768p，开启 enhance）消耗 10 积分（$0.10）。见[单价表](https://docs.viggle.ai/zh/v1/video/generate-video#price)。

## 生成视频

导出 `VIGGLE_API_KEY`，然后直接运行以下任一脚本。脚本使用 Viggle 托管的示例角色图；准备好后换成你自己的 URL 即可。每个脚本每 5 秒轮询一次，并打印视频 URL。cURL 版本需要 `jq`。

```bash cURL theme={null}
#!/usr/bin/env bash
set -euo pipefail
API="https://apis.viggle.ai/v1"

video_id=$(curl -sS --fail-with-body "$API/videos" \
  -H "Authorization: Bearer $VIGGLE_API_KEY" \
  -F prompt="the character waves at the camera and starts to dance" \
  -F quality="high" \
  -F first_frame_image_url="https://assets.viggle.ai/pub/www/developers/threed-source-image-3f2f6787.webp" | jq -er '.id')
echo "Submitted $video_id"

for _ in $(seq 1 1440); do
  video=$(curl -sS --fail-with-body "$API/videos/$video_id" \
    -H "Authorization: Bearer $VIGGLE_API_KEY")
  case $(echo "$video" | jq -r '.status') in
    ready) echo "$video" | jq -r '.video_url'; exit 0 ;;
    failed|cancelled) echo "$video" | jq -c '.error' >&2; exit 1 ;;
  esac
  sleep 5
done
echo "Timed out waiting for $video_id" >&2; exit 1
```

```javascript Node.js theme={null}
// Node.js 20+。保存为 first-frame.mjs，然后运行：node first-frame.mjs
const API = "https://apis.viggle.ai/v1";
const headers = { Authorization: `Bearer ${process.env.VIGGLE_API_KEY}` };

async function call(path, init = {}) {
  const res = await fetch(`${API}${path}`, { ...init, headers });
  const body = await res.json();
  if (!res.ok) throw new Error(`HTTP ${res.status}: ${JSON.stringify(body.error)}`);
  return body;
}

const form = new FormData();
form.append("prompt", "the character waves at the camera and starts to dance");
form.append("quality", "high");
form.append("first_frame_image_url", "https://assets.viggle.ai/pub/www/developers/threed-source-image-3f2f6787.webp");
const { id } = await call("/videos", { method: "POST", body: form });
console.log(`Submitted ${id}`);

for (let i = 0; i < 1440; i++) {
  const video = await call(`/videos/${id}`);
  if (video.status === "ready") {
    console.log(video.video_url);
    process.exit(0);
  }
  if (video.status === "failed" || video.status === "cancelled") {
    throw new Error(`${video.status}: ${JSON.stringify(video.error)}`);
  }
  await new Promise((resolve) => setTimeout(resolve, 5000));
}
throw new Error(`Timed out waiting for ${id}`);
```

```python Python theme={null}
import os
import time

import requests

API = "https://apis.viggle.ai/v1"
HEADERS = {"Authorization": f"Bearer {os.environ['VIGGLE_API_KEY']}"}


def check(resp):
    if not resp.ok:
        raise RuntimeError(f"HTTP {resp.status_code}: {resp.text}")
    return resp.json()


# 通过 files= 传字段，requests 会以 multipart/form-data 发送。
video = check(requests.post(f"{API}/videos", headers=HEADERS, timeout=300, files={
    "prompt": (None, "the character waves at the camera and starts to dance"),
    "quality": (None, "high"),
    "first_frame_image_url": (None, "https://assets.viggle.ai/pub/www/developers/threed-source-image-3f2f6787.webp"),
}))
print("Submitted", video["id"])

for _ in range(1440):
    video = check(requests.get(f"{API}/videos/{video['id']}", headers=HEADERS))
    if video["status"] == "ready":
        print(video["video_url"])
        break
    if video["status"] in ("failed", "cancelled"):
        raise RuntimeError(f"{video['status']}: {video['error']}")
    time.sleep(5)
else:
    raise TimeoutError(f"Timed out waiting for {video['id']}")
```

要上传本地文件，把 URL 字段换成 `first_frame_image`：

```bash theme={null}
curl https://apis.viggle.ai/v1/videos \
  -H "Authorization: Bearer $VIGGLE_API_KEY" \
  -F prompt="the character waves at the camera and starts to dance" \
  -F quality=high \
  -F first_frame_image=@first-frame.png
```

> [!WARNING]
>
> 如果余额不足以支付该任务，请求仍会被接受，随后任务以 `failed` 结束，`error.code: "INSUFFICIENT_CREDITS"`。大批量提交前请先调用 `GET /v1/credits` 查看余额。

## 获取结果

创建请求返回一个 `vid_` ID 和 `status: "queued"`。轮询 `GET /v1/videos/{video_id}`，直到 `status` 为 `ready`，然后下载 `video_url`。

* 链接有效期为一小时，每次读取都会返回新链接。
* 视频在任务结束 24 小时后删除，请复制到你自己的存储中。
* 只有创建该任务的 API 密钥才能读取它。

有问题的图片（无法读取、过大，或同时发送了文件和 URL）会在创建时被拒绝，返回 `400 INVALID_REQUEST`，不会产生任何费用。见[任务生命周期](https://docs.viggle.ai/zh/v1/concepts/task-lifecycle.md)和[错误](https://docs.viggle.ai/zh/v1/production/errors.md)。

## 提示

* 描述动作，而不是画面。图片已经定好了场景，提示词应着重写什么在动、镜头如何运动。
* 将 `aspect_ratio` 设置为与图片一致。无论图片是什么比例，它都默认为 `16:9`。

## 后续步骤

> [card] [首尾帧生视频](https://docs.viggle.ai/zh/v1/video/first-last-frame-to-video.md)
>
> 控制镜头的起点和终点。

> [card] [参考生视频](https://docs.viggle.ai/zh/v1/video/reference-to-video.md)
>
> 保留主体或风格，而不固定开场帧。

> [card] [Viggle H3 概览](https://docs.viggle.ai/zh/v1/video/generate-video)
>
> 四种模式、通用参数和价格。

> [card] [创建视频（API 参考）](https://docs.viggle.ai/zh/v1/api-reference/videos/create-a-video.md)
>
> `POST /v1/videos` 的全部字段、限制和错误。


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