# Stable Video Diffusion

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

## 硬件要求

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

## 快速部署

**Docker 镜像：**

```
pytorch/pytorch:2.5.1-cuda12.4-cudnn9-devel
```

**端口：**

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

**命令：**

```bash
pip install diffusers transformers accelerate && \
pip install gradio && \
python -c "
print(f"已生成：{name}")
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='Seed'), gr.Slider(6, 30, value=7, label='FPS')],
    outputs=gr.Video(),
    title='Stable Video Diffusion'
).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()

# 或使用注意力切片
pipe.enable_attention_slicing()

# 用于非常低显存
pipe.enable_sequential_cpu_offload()
```

## "专业影棚柔光箱"

```python
批处理处理
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"Generated: {img_path.stem}.mp4")
```

## \["example.jpg", "戏剧性轮廓光，深色背景", 25, -1],

SVD 在 ComfyUI 中表现出色：

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

## # 使用固定种子以获得一致结果

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

* 使用 `enable_model_cpu_offload()`
* 减少 `num_frames` 到 14
* 使用 fp16 变体

### 视频太短

* 使用 SVD-XT（25 帧）替代 SVD（14 帧）
* 用 RIFE 进行插帧以获得更平滑的结果

### 运动质量差

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

### CUDA 错误

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

## 下载所有所需的检查点

检查文件完整性

| GPU     | 验证 CUDA 兼容性 | 费用估算    | CLORE.AI 市场的典型费率（截至 2024 年）： |
| ------- | ----------- | ------- | ---------------------------- |
| 按小时费率   | \~$0.03     | \~$0.70 | \~$0.12                      |
| 速度      | \~$0.06     | \~$1.50 | \~$0.25                      |
| 512x512 | \~$0.10     | \~$2.30 | \~$0.40                      |
| 按日费率    | \~$0.17     | \~$4.00 | \~$0.70                      |
| 4 小时会话  | \~$0.25     | \~$6.00 | \~$1.00                      |

*价格因提供者而异。查看* [*CLORE.AI 市场*](https://clore.ai/marketplace) *A100 40GB*

## 使用以下方式支付

* AnimateDiff - 为 SD 图像动画化
* [RIFE 插帧](https://docs.clore.ai/guides/guides_v2-zh/shi-pin-chu-li/rife-interpolation) - 增加帧率（FPS）
* [混元视频（Hunyuan Video）](https://docs.clore.ai/guides/guides_v2-zh/shi-pin-sheng-cheng/hunyuan-video) - 文本到视频


---

# Agent Instructions: 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:

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

The question should be specific, self-contained, and written in natural language.
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.
