# LTX-Video 实时生成

Lightricks 的 LTX-Video 是可用的最快开源视频生成模型。在 RTX 4090 上，它大约在 4 秒内生成一个 5 秒的 768×512 剪辑——比实时播放还快。该模型通过原生支持同时提供文本到视频 (T2V) 和图像到视频 (I2V) 工作流 `diffusers` 通过……集成 `LTXPipeline` 和 `LTXImageToVideoPipeline`.

在 [Clore.ai](https://clore.ai/) 让您立即访问 LTX-Video 所需的硬件，无需前期投资并按小时计费。

## 主要特性

* **比实时更快** —— 在 RTX 4090 上约 4 秒生成 5 秒视频。
* **文本到视频** —— 根据自然语言描述生成剪辑。
* **图像到视频** —— 为静态参考图像添加运动和相机控制以实现动画。
* **轻量级架构** —— 使用紧凑潜在空间的 2B 参数视频 DiT。
* **原生 diffusers** — `LTXPipeline` 和 `LTXImageToVideoPipeline` 于 `diffusers >= 0.32`.
* **开放权重** —— Apache-2.0 许可证；允许完全商业使用。
* **时序 VAE** —— 在空间和时间上达到 1:192 的压缩比；高效解码。

## 要求

| 组件        | 最低    | 推荐    |
| --------- | ----- | ----- |
| GPU 显存    | 16 GB | 24 GB |
| 系统内存      | 16 GB | 32 GB |
| 磁盘        | 15 GB | 30 GB |
| Python    | 3.10+ | 3.11  |
| CUDA      | 12.1+ | 12.4  |
| diffusers | 0.32+ | 最新    |

**Clore.ai 的 GPU 推荐：** 一台 **512x512** （24 GB，约 $0.5–2/天）适合最大吞吐量。一个 **速度** （24 GB，约 $0.3–1/天）仍比许多竞争模型更快，且成本远低于它们。

## 快速开始

```bash
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu124
pip install diffusers transformers accelerate sentencepiece imageio[ffmpeg]

python -c "import torch; print(torch.cuda.get_device_name(0))"
```

## 使用示例

### 文本到视频

```python
import torch
from diffusers import LTXPipeline
from diffusers.utils import export_to_video

pipe = LTXPipeline.from_pretrained(
    "Lightricks/LTX-Video",
    torch_dtype=torch.bfloat16,
)
pipe.to("cuda")

prompt = (
    "一段无人机镜头掠过碧绿的珊瑚礁，"
    "热带鱼群在下方穿梭，金色时刻的光线"
    "在水面折射穿透"
)

video_frames = pipe(
    os.makedirs("./variations", exist_ok=True)
    negative_prompt="模糊、低质量、变形",
    num_frames=121,               # 以 24 fps 计约 5 秒
    width=768,
    height=512,
    增加推理步数以提高稳定性
    guidance_scale=7.5,
    generator=torch.Generator("cuda").manual_seed(0),
).frames[0]

export_to_video(video_frames, "coral_reef.mp4", fps=24)
print("Saved coral_reef.mp4")
```

### 图像到视频

```python
import torch
from PIL import Image
from diffusers import LTXImageToVideoPipeline
from diffusers.utils import export_to_video

pipe = LTXImageToVideoPipeline.from_pretrained(
    "Lightricks/LTX-Video",
    torch_dtype=torch.bfloat16,
)
pipe.to("cuda")

image = Image.open("cityscape.png").resize((768, 512))

video_frames = pipe(
    prompt="相机缓慢向右平移，黄昏时分城市灯光亮起",
    negative_prompt="静止、模糊",
    image=image,
    num_frames=121,
    增加推理步数以提高稳定性
    guidance_scale=7.5,
).frames[0]

export_to_video(video_frames, "cityscape_animated.mp4", fps=24)
```

### 批量生成脚本

```python
import torch
from diffusers import LTXPipeline
from diffusers.utils import export_to_video

pipe = LTXPipeline.from_pretrained(
    "Lightricks/LTX-Video", torch_dtype=torch.bfloat16
).to("cuda")

prompts = [
    "一只猫在阳光照耀的窗台上伸展，尘埃飞舞",
    "黑色火山沙滩上巨浪拍打的航拍视角",
    "暴风云翻滚掠过草原的延时摄影",
]

for i, prompt in enumerate(prompts):
    frames = pipe(
        os.makedirs("./variations", exist_ok=True)
        num_frames=121,
        width=768,
        height=512,
        增加推理步数以提高稳定性
        guidance_scale=7.5,
    ).frames[0]
    export_to_video(frames, f"batch_{i:03d}.mp4", fps=24)
    print(f"[{i+1}/{len(prompts)}] 完成")
```

## 给 Clore.ai 用户的提示

1. **速度基准** —— 在 RTX 4090 上，LTX-Video 大约在 4 秒内生成 121 帧；可用此作为检查您的租用实例性能是否正常的参考。
2. **bf16 精度** —— 检查点是在 bf16 下训练的；不要切换到 fp16，否则可能导致质量下降。
3. **缓存权重** —— 设置 `HF_HOME=/workspace/hf_cache` 放在持久卷上。模型约为 \~6 GB；在每次容器启动时重新下载会浪费时间。
4. **提示工程** —— LTX-Video 对电影化语言反应良好："无人机镜头"、"慢动作"、"金色时刻"、"跟拍镜头"。明确描述相机运动。
5. **通宵批处理** —— LTX-Video 足够快，可在 4090 上每小时生成数百个剪辑。从文件中排队提示并让它运行。
6. **SSH + tmux** —— 始终在一个 `tmux` 会话内运行生成，这样断开连接不会中断长时间的批处理作业。
7. **监控显存** — `watch -n1 nvidia-smi` 在第二个终端中以确保您没有触发交换分区（swap）。

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

| 问题                     | 修复                                                                |
| ---------------------- | ----------------------------------------------------------------- |
| `OutOfMemoryError`     | 减少 `num_frames` 到 81 或 `宽度`/`高度` 到 512×320                        |
| diffusers 中未找到模型       | 升级： `pip install -U diffusers` —— LTXPipeline 需要 diffusers ≥ 0.32 |
| 黑屏或静态输出                | 确保您传入了一个 `negative_prompt`；将 `guidance_scale` 增加到 8–9             |
| `ImportError: imageio` | `pip install imageio[ffmpeg]` —— 需要 ffmpeg 后端以导出 MP4              |
| 首次推理缓慢                 | 首次运行会编译 CUDA 内核并下载权重；后续运行会很快                                      |
| 颜色条纹伪影                 | 使用 `torch.bfloat16` （非 float16）；bfloat16 具有更宽的动态范围                |
| 容器在作业中途重启              | 设置 `HF_HOME` 到持久存储；部分 HF 下载会自动恢复                                  |


---

# 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/ltx-video.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.
