> For the complete documentation index, see [llms.txt](https://docs.clore.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.clore.ai/guides/guides_v2-zh/shi-pin-sheng-cheng/stable-video-diffusion.md).

# Stable Video Diffusion

在 Clore.ai 上使用 Stable Video Diffusion 从图像生成视频

{% hint style="info" %}
**有更新的替代方案可用！** 可考虑 [**FramePack**](/guides/guides_v2-zh/shi-pin-sheng-cheng/framepack.md) （仅需 6GB 显存！）， [**Wan2.1**](/guides/guides_v2-zh/shi-pin-sheng-cheng/wan-video.md) （更高质量），或 [**LTX-2**](/guides/guides_v2-zh/shi-pin-sheng-cheng/ltx-video-2.md) （带原生音频的视频）。
{% endhint %}

使用 Stability AI 的 SVD 模型从图像生成视频。

{% hint style="success" %}
所有示例都可以在通过以下方式租用的 GPU 服务器上运行 [CLORE.AI 市场](https://clore.ai/marketplace).
{% endhint %}

## 什么是 Stable Video Diffusion？

SVD（Stable Video Diffusion）可从单张图像生成短视频片段：

* 14 帧或 25 帧输出
* 576x1024 分辨率
* 平滑运动生成
* 开源权重

## 资源

* **HuggingFace：** [stabilityai/stable-video-diffusion-img2vid-xt](https://huggingface.co/stabilityai/stable-video-diffusion-img2vid-xt)
* **GitHub：** [Stability-AI/generative-models](https://github.com/Stability-AI/generative-models)
* **论文：** [SVD 论文](https://arxiv.org/abs/2311.15127)

## 硬件要求

| 模型           | 显存   | 推荐 GPU          |
| ------------ | ---- | --------------- |
| SVD（14 帧）    | 16GB | RTX 4090        |
| SVD-XT（25 帧） | 24GB | RTX 4090 / A100 |

## 快速部署

**Docker 镜像：**

```
pytorch/pytorch:2.11.0-cuda12.8-cudnn9-devel
```

**端口：**

```
22/tcp
7860/http
```

**命令：**

```bash
pip install diffusers transformers accelerate && \
pip install gradio && \
python -c "
import gradio as gr
from diffusers import StableVideoDiffusionPipeline
from diffusers.utils import export_to_video
import torch

pipe = StableVideoDiffusionPipeline.from_pretrained(
    'stabilityai/stable-video-diffusion-img2vid-xt',
    torch_dtype=torch.float16,
    variant='fp16'
).to('cuda')

def generate(image, seed, fps):
    generator = torch.manual_seed(seed)
    frames = pipe(image, num_frames=25, generator=generator).frames[0]
    export_to_video(frames, 'output.mp4', fps=fps)
    return 'output.mp4'

gr.Interface(
    fn=generate,
    inputs=[gr.Image(type='pil'), gr.Number(value=42, label='种子'), gr.Slider(6, 30, value=7, label='帧率')],
    outputs=gr.Video(),
    title='稳定视频扩散'
).launch(server_name='0.0.0.0', server_port=7860)
"
```

## 访问你的服务

部署后，找到你的 `http_pub` URL 在 **我的订单**:

1. 前往 **我的订单** 页面
2. 点击你的订单
3. 找到 `http_pub` URL（例如， `abc123.clorecloud.net`)

使用 `https://YOUR_HTTP_PUB_URL` 替代 `localhost` 在下面的示例中。

## 安装

```bash
pip install diffusers transformers accelerate torch

# 用于视频导出
pip install imageio[ffmpeg]
```

## 基础用法

```python
import torch
from diffusers import StableVideoDiffusionPipeline
from diffusers.utils import load_image, export_to_video

# 加载管道
pipe = StableVideoDiffusionPipeline.from_pretrained(
    "stabilityai/stable-video-diffusion-img2vid-xt",
    torch_dtype=torch.float16,
    variant="fp16"
)
pipe.to("cuda")

# 加载并调整图像大小
image = load_image("input.jpg")
image = image.resize((1024, 576))

# 生成视频
generator = torch.manual_seed(42)
frames = pipe(image, num_frames=25, generator=generator).frames[0]

# 保存视频
export_to_video(frames, "output.mp4", fps=7)
```

## SVD 与 SVD-XT

| 功能 | SVD   | SVD-XT  |
| -- | ----- | ------- |
| 帧数 | 14    | 25      |
| 时长 | \~2 秒 | \~3.5 秒 |
| 显存 | 16GB  | 24GB    |
| 质量 | 好     | 更好      |

## 内存优化

```python

# 启用内存高效注意力
pipe.enable_model_cpu_offload()

# 或使用 attention slicing
pipe.enable_attention_slicing()

# 适用于极低显存
pipe.enable_sequential_cpu_offload()
```

## 批量处理

```python
import os
from pathlib import Path

input_dir = Path("./images")
output_dir = Path("./videos")
output_dir.mkdir(exist_ok=True)

for img_path in input_dir.glob("*.jpg"):
    image = load_image(str(img_path)).resize((1024, 576))
    frames = pipe(image, num_frames=25).frames[0]
    export_to_video(frames, str(output_dir / f"{img_path.stem}.mp4"), fps=7)
    print(f"已生成：{img_path.stem}.mp4")
```

## ComfyUI 集成

SVD 在 ComfyUI 中表现很好：

1. 安装 ComfyUI
2. 将 SVD 模型下载到 `models/checkpoints/`
3. 在 img2vid 工作流中使用 SVD 节点

## 故障排查

{% hint style="danger" %}
**内存不足**
{% endhint %}

* 使用 `enable_model_cpu_offload()`
* 减少 `num_frames` 设为 14
* 使用 fp16 版本

### 视频太短

* 使用 SVD-XT（25 帧）而不是 SVD（14 帧）
* 使用 RIFE 插帧以获得更平滑的结果

### 运动质量较差

* 使用高质量输入图像
* 确保图像为 1024x576（或 576x1024）
* 尝试不同的种子

### CUDA 错误

* 更新 PyTorch 和 diffusers
* 检查 CUDA 版本兼容性

## 成本估算

CLORE.AI 市场常见费率（截至 2024 年）：

| GPU       | 小时费率    | 日费率     | 4 小时会话  |
| --------- | ------- | ------- | ------- |
| RTX 3060  | \~$0.03 | \~$0.70 | \~$0.12 |
| RTX 3090  | \~$0.06 | \~$1.50 | \~$0.25 |
| RTX 4090  | \~$0.10 | \~$2.30 | \~$0.40 |
| A100 40GB | \~$0.17 | \~$4.00 | \~$0.70 |
| A100 80GB | \~$0.25 | \~$6.00 | \~$1.00 |

*价格因提供商而异。查看* [*CLORE.AI 市场*](https://clore.ai/marketplace) *以获取当前费率。*

## 下一步

* AnimateDiff - 动画化 SD 图像
* [RIFE 插帧](/guides/guides_v2-zh/shi-pin-chu-li/rife-interpolation.md) - 提高 FPS
* [Hunyuan Video](/guides/guides_v2-zh/shi-pin-sheng-cheng/hunyuan-video.md) - 文本生成视频


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.clore.ai/guides/guides_v2-zh/shi-pin-sheng-cheng/stable-video-diffusion.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
