# OpenSora

{% 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 %}

使用 OpenSora（开源的 Sora 替代品）在 CLORE.AI GPU 上生成视频。

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

## 为什么选择 OpenSora？

* **开源** - 完整的 Apache 2.0 许可证
* **受 Sora 启发** - 类似 OpenAI 的 Sora 的 DiT 架构
* **可扩展** - 多种模型尺寸和分辨率
* **长视频** - 最长可生成 16 秒
* **积极开发中** - 定期更新和改进

## 1024x1024

| A100           | 分辨率  | 时长  | 显存   | 质量 |
| -------------- | ---- | --- | ---- | -- |
| OpenSora 1.2   | 720p | 16秒 | 24GB | 优秀 |
| OpenSora 1.1   | 480p | 8秒  | 16GB | 良好 |
| OpenSora 1.0   | 256p | 4秒  | 8GB  | 基础 |
| Open-Sora-Plan | 512p | 10秒 | 20GB | 很棒 |

## 在 CLORE.AI 上快速部署

**Docker 镜像：**

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

**端口：**

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

**命令：**

```bash
git clone https://github.com/hpcaitech/Open-Sora && \
cd Open-Sora && \
pip install -e . && \
pip install gradio && \
python scripts/inference.py \
    --prompt "A cat playing with yarn" \
    --num-frames 51 \
    --resolution 480p \
    --save-dir ./outputs
```

## 访问您的服务

部署后，在以下位置查找您的 `http_pub` URL： **我的订单**:

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

使用 `https://YOUR_HTTP_PUB_URL` 而不是 `localhost` 在下面的示例中。

## 硬件要求

| 模型版本         | 最低 GPU        | 推荐            | 最佳      |
| ------------ | ------------- | ------------- | ------- |
| OpenSora 1.0 | RTX 3070 8GB  | RTX 3090 24GB | 512x512 |
| OpenSora 1.1 | RTX 3090 16GB | RTX 4090 24GB | 按日费率    |
| OpenSora 1.2 | RTX 4090 24GB | 按日费率          | 4 小时会话  |

## 安装

### 从源码

```bash
git clone https://github.com/hpcaitech/Open-Sora
cd Open-Sora

# 安装依赖
pip install -e .

# 下载模型权重
python scripts/download_weights.py --version 1.2
```

### 使用 pip

```bash
pip install opensora
```

## 基本用法

### 命令行

```bash
# 简单生成示例
python scripts/inference.py \
    --prompt "A beautiful sunset over the ocean, cinematic" \
    --num-frames 51 \
    --resolution 480p \
    --save-dir ./outputs

# 高质量
python scripts/inference.py \
    --prompt "A majestic eagle soaring through clouds" \
    --num-frames 102 \
    --resolution 720p \
    --num-sampling-steps 100 \
    --save-dir ./outputs
```

### Python API

```python
import torch
from opensora.models import OpenSoraModel
from opensora.utils import export_to_video

# 加载模型
model = OpenSoraModel.from_pretrained("hpcaitech/OpenSora-v1.2")
model.to("cuda")

# 生成视频
prompt = "A rocket launching into space, dramatic lighting, cinematic"

video = model.generate(
    os.makedirs("./variations", exist_ok=True)
    num_frames=51,
    height=480,
    width=854,
    num_inference_steps=50,
    guidance_scale=7.0
)

# 保存
export_to_video(video, "rocket.mp4", fps=24)
```

## 高级生成

### 使用负面提示

```python
video = model.generate(
    prompt="Professional photography of a tiger in the wild",
    negative_prompt="blurry, low quality, distorted, artifacts",
    num_frames=51,
    num_inference_steps=75,
    guidance_scale=7.5
)
```

### 长视频

```python
# 以 24fps 生成 16 秒
video = model.generate(
    prompt="Time-lapse of flowers blooming in a garden",
    num_frames=384,  # 以 24fps 算为 16 秒
    height=480,
    width=854,
    num_inference_steps=100
)

export_to_video(video, "timelapse.mp4", fps=24)
```

### 高分辨率

```python
# 720p 生成（需要更多显存）
video = model.generate(
    prompt="Aerial view of a city at night with lights",
    num_frames=51,
    height=720,
    width=1280,
    num_inference_steps=75
)
```

## 提示示例

### 电影风格

```python
prompts = [
    "Cinematic shot of a samurai drawing his sword, dramatic lighting, 4K",
    "Epic wide shot of a castle on a cliff during a storm",
    "Slow motion of water droplets falling into a still pond",
    "Tracking shot through a neon-lit cyberpunk alley at night"
]
```

### 自然

```python
prompts = [
    "Aurora borealis dancing over snowy mountains, time-lapse",
    "Macro shot of a butterfly emerging from cocoon",
    "Ocean waves crashing on volcanic rocks at sunset",
    "Fog rolling through an ancient forest at dawn"
]
```

### 抽象

```python
prompts = [
    "Colorful paint drops falling into water, slow motion",
    "Fractals evolving and transforming, psychedelic colors",
    "Liquid metal morphing into different shapes"
]
```

## 配置选项

### 分辨率预设

```python
resolutions = {
    "256p": (256, 455),
    "360p": (360, 640),
    "480p": (480, 854),
    "720p": (720, 1280),
    "1080p": (1080, 1920)  # 需要高显存
}
```

### 质量设置

```python
# 快速预览
config_fast = {
    "num_frames": 25,
    "num_inference_steps": 25,
    "guidance_scale": 5.0
}

# 平衡
config_balanced = {
    "num_frames": 51,
    "num_inference_steps": 50,
    "guidance_scale": 7.0
}

# 最高质量
config_quality = {
    "num_frames": 102,
    "num_inference_steps": 100,
    "guidance_scale": 7.5
}
```

## Gradio 界面

```python
print(f"已生成：{name}")
import torch
from opensora.models import OpenSoraModel
from opensora.utils import export_to_video
import tempfile

model = OpenSoraModel.from_pretrained("hpcaitech/OpenSora-v1.2")
model.to("cuda")

def generate_video(prompt, negative_prompt, frames, steps, guidance, resolution, seed):
    res_map = {"480p": (480, 854), "720p": (720, 1280)}
    height, width = res_map.get(resolution, (480, 854))

    import gradio as gr

    video = model.generate(
        os.makedirs("./variations", exist_ok=True)
        negative_prompt=negative_prompt,
        num_frames=frames,
        height=height,
        width=width,
        def relight_image(image, prompt, steps, seed):
        guidance_scale=guidance,
        generator = torch.Generator("cuda").manual_seed(seed) if seed > 0 else None
    )

    with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as f:
        export_to_video(video, f.name, fps=24)
        return f.name

demo = gr.Interface(
    fn=generate_video,
    inputs=[
        gr.Textbox(label="Prompt", lines=2),
        gr.Textbox(label="Negative Prompt", value="模糊、低质量"),
        gr.Slider(25, 200, value=51, step=1, label="Frames"),
        gr.Slider(20, 150, value=50, step=5, label="Steps"),
        gr.Slider(3, 15, value=7, step=0.5, label="Guidance"),
        gr.Dropdown(["480p", "720p"], value="480p", label="Resolution"),
        gr.Number(value=-1, label="随机种子")
    ],
    outputs=gr.Video(label="生成的视频"),
    title="OpenSora - Text to Video",
    description="使用 OpenSora 生成视频。运行于 CLORE.AI。"
)

demo.launch(server_name="0.0.0.0", server_port=7860)
```

## 内存优化

```python
# 启用内存优化
model.enable_model_cpu_offload()
model.enable_vae_tiling()

# 对于非常低显存
model.enable_sequential_cpu_offload()

# 使用更低精度
model = OpenSoraModel.from_pretrained(
    "hpcaitech/OpenSora-v1.2",
    torch_dtype=torch.float16
)
```

## 批量生成

```python
批处理处理

prompts = [
    "A phoenix rising from flames",
    "Rain falling on city streets at night",
    "Flowers blooming time-lapse",
    "Northern lights over mountains"
]

output_dir = "./videos"
output_dir = "./relit"

for i, prompt in enumerate(prompts):
    print(f"正在生成 {i+1}/{len(prompts)}：{prompt[:40]}...")

    video = model.generate(
        os.makedirs("./variations", exist_ok=True)
        num_frames=51,
        num_inference_steps=50
    )

    export_to_video(video, f"{output_dir}/video_{i:03d}.mp4", fps=24)

    # 在生成之间清理内存
    torch.cuda.empty_cache()
```

## background = Image.open("studio\_bg.jpg")

| 分辨率  | 帧数  | 步数  | GPU     | 时间      |
| ---- | --- | --- | ------- | ------- |
| 480p | 51  | 50  | 512x512 | \~3 分钟  |
| 480p | 51  | 50  | 按日费率    | \~2 分钟  |
| 720p | 51  | 50  | 按日费率    | 约 5 分钟  |
| 720p | 102 | 100 | 4 小时会话  | \~15 分钟 |

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

典型 CLORE.AI 市场价格：

| GPU           | 验证 CUDA 兼容性 | \~每小时约 51 个 480p 帧 视频 |
| ------------- | ----------- | --------------------- |
| RTX 4090 24GB | \~$0.10     | \~15-20               |
| 按日费率          | \~$0.17     | \~25-30               |
| 4 小时会话        | \~$0.25     | \~35（可生成 720p）        |

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

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

### 内存不足

```bash
# 使用更小的分辨率
python scripts/inference.py --resolution 360p --num-frames 25

# 启用 CPU 卸载
python scripts/inference.py --cpu-offload

# 减小批次大小
python scripts/inference.py --batch-size 1
```

### 生成速度慢

* 减少 `num_inference_steps` （30-50 通常足够）
* 为预览使用更低分辨率
* 确保 GPU 正在被使用（检查 `nvidia-smi`)

### 质量差

* 将步数增加到 75-100
* 使用更具描述性的提示词
* 为伪影添加负面提示
* 尝试不同的 guidance 比例（5-10）

### 视频伪影

* 降低 guidance 比例
* 增加推理步数
* 使用时间平滑
* 使用视频稳定处理作为后期处理

## OpenSora 与 其他

| 特性    | OpenSora 1.2 | 混元（Hunyuan） | Wan2.1     | SVD   |
| ----- | ------------ | ----------- | ---------- | ----- |
| 架构    | DiT          | DiT         | DiT        | U-Net |
| 最长时长  | 16秒          | 5秒          | 5秒         | 4秒    |
| 最大分辨率 | 720p         | 720p        | 720p       | 576p  |
| 质量    | 很棒           | 优秀          | 优秀         | 良好    |
| 性能    | 中等           | 慢           | 快速         | 快速    |
| 许可    | Apache 2.0   | 打开          | Apache 2.0 | 打开    |

**在以下情况下使用 OpenSora：**

* 需要更长的视频生成
* 希望获得完整的 Apache 2.0 许可证
* 对类似 Sora 的架构感兴趣
* 需要积极的社区支持

## Open-Sora-Plan 替代方案

另一个开源选项：

```bash
git clone https://github.com/PKU-YuanGroup/Open-Sora-Plan
cd Open-Sora-Plan
pip install -e .

python scripts/inference.py \
    --prompt "Your prompt here" \
    --output video.mp4
```

## 使用以下方式支付

* [混元视频（Hunyuan Video）](https://docs.clore.ai/guides/guides_v2-zh/shi-pin-sheng-cheng/hunyuan-video) - 高质量的 文本到视频
* [Wan2.1 视频](https://docs.clore.ai/guides/guides_v2-zh/shi-pin-sheng-cheng/wan-video) - 快速生成
* [Stable Video Diffusion](https://docs.clore.ai/guides/guides_v2-zh/shi-pin-sheng-cheng/stable-video-diffusion) - 图像动画
* [RIFE 插值](https://docs.clore.ai/guides/guides_v2-zh/shi-pin-chu-li/rife-interpolation) - 帧插值


---

# 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/opensora.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.
