> 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-chu-li/ffmpeg-nvenc.md).

# FFmpeg NVENC

在 Clore.ai 上使用 FFmpeg NVENC 进行 GPU 加速视频编码

使用 NVIDIA GPU 进行硬件加速视频编码。

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

## 在 CLORE.AI 上租用

1. 访问 [CLORE.AI 市场](https://clore.ai/marketplace)
2. 按 GPU 类型、VRAM 和价格筛选
3. 选择 **按需** （固定费率）或 **竞价** （出价）
4. 配置你的订单：
   * 选择 Docker 镜像
   * 设置端口（SSH 用 TCP，Web UI 用 HTTP）
   * 如有需要，添加环境变量
   * 输入启动命令
5. 选择支付方式： **CLORE**, **BTC**，或 **USDT/USDC**
6. 创建订单并等待部署

### 访问你的服务器

* 在以下位置查找连接信息 **我的订单**
* Web 界面：使用 HTTP 端口 URL
* SSH： `ssh -p <port> root@<proxy-address>`

## 什么是 NVENC？

NVENC（NVIDIA 视频编码器）提供：

* 比 CPU 快 5-10 倍的编码速度
* 支持 H.264、H.265/HEVC、AV1
* 实时 4K/8K 编码
* 低 GPU 计算占用

## 需求

| 编解码器  | 最低 GPU    | 推荐        |
| ----- | --------- | --------- |
| H.264 | GTX 600+  | RTX 3060+ |
| HEVC  | GTX 900+  | RTX 3070+ |
| AV1   | RTX 4000+ | RTX 4090  |

## 快速部署

**Docker 镜像：**

```
nvidia/cuda:12.8.1-devel-ubuntu22.04
```

**端口：**

```
22/tcp
```

**命令：**

```bash
apt-get update && \
apt-get install -y ffmpeg && \\
echo "FFmpeg with NVENC ready"
```

## 检查 NVENC 支持

```bash

# 检查可用的编码器
ffmpeg -encoders | grep nvenc

# 应显示：

# V....D h264_nvenc

# V....D hevc_nvenc

# V....D av1_nvenc (RTX 4000+)
```

## 基础编码

### H.264 编码

```bash
ffmpeg -i input.mp4 -c:v h264_nvenc -preset p7 -cq 23 output.mp4
```

### HEVC/H.265 编码

```bash
ffmpeg -i input.mp4 -c:v hevc_nvenc -preset p7 -cq 23 output.mp4
```

### AV1 编码（RTX 4000+）

```bash
ffmpeg -i input.mp4 -c:v av1_nvenc -preset p7 -cq 23 output.mp4
```

## 预设

| 预设    | 质量 | 速度 |
| ----- | -- | -- |
| p1    | 最低 | 最快 |
| p2-p3 | 低  | 快  |
| p4-p5 | 中等 | 平衡 |
| p6-p7 | 高  | 慢  |

```bash

# 编码最快
ffmpeg -i input.mp4 -c:v h264_nvenc -preset p1 output.mp4

# 最佳质量
ffmpeg -i input.mp4 -c:v h264_nvenc -preset p7 output.mp4
```

## 质量控制

### 恒定质量（CQ）

```bash

# 越低 = 质量越好，文件越大
ffmpeg -i input.mp4 -c:v h264_nvenc -cq 18 output.mp4

# 推荐值：18-28
```

### 恒定码率（CBR）

```bash
ffmpeg -i input.mp4 -c:v h264_nvenc -b:v 10M output.mp4
```

### 可变码率（VBR）

```bash
ffmpeg -i input.mp4 -c:v h264_nvenc -b:v 10M -maxrate 15M -bufsize 20M output.mp4
```

## 分辨率与缩放

### 调整视频大小

```bash

# 缩放到 1080p
ffmpeg -i input.mp4 -vf "scale=1920:1080" -c:v h264_nvenc output.mp4

# 缩放到 4K
ffmpeg -i input.mp4 -vf "scale=3840:2160" -c:v hevc_nvenc output.mp4

# 保持宽高比
ffmpeg -i input.mp4 -vf "scale=-1:1080" -c:v h264_nvenc output.mp4
```

### GPU 缩放（更快）

```bash
ffmpeg -hwaccel cuda -hwaccel_output_format cuda \\
    -i input.mp4 \\
    -vf "scale_cuda=1920:1080" \\
    -c:v h264_nvenc output.mp4
```

## 硬件解码 + 编码

完整 GPU 流水线：

```bash
ffmpeg \\
    -hwaccel cuda \\
    -hwaccel_output_format cuda \\
    -i input.mp4 \\
    -c:v h264_nvenc \\
    -preset p4 \\
    output.mp4
```

## 批量转换

### Shell 脚本

```bash
#!/bin/bash
INPUT_DIR=$1
OUTPUT_DIR=$2

mkdir -p "$OUTPUT_DIR"

for file in "$INPUT_DIR"/*.{mp4,mkv,avi,mov}; do
    if [ -f "$file" ]; then
        filename=$(basename "$file")
        name="${filename%.*}"

        ffmpeg -hwaccel cuda -i "$file" \\
            -c:v h264_nvenc -preset p5 -cq 23 \\
            -c:a aac -b:a 192k \\
            "$OUTPUT_DIR/${name}.mp4"

        echo "已转换：$filename"
    fi
done
```

### Python 批处理

```python
import subprocess
import os
from concurrent.futures import ThreadPoolExecutor

def convert_video(input_path, output_path):
    cmd = [
        'ffmpeg', '-y',
        '-hwaccel', 'cuda',
        '-i', input_path,
        '-c:v', 'h264_nvenc',
        '-preset', 'p5',
        '-cq', '23',
        '-c:a', 'aac',
        '-b:a', '192k',
        output_path
    ]
    subprocess.run(cmd, check=True)

input_dir = './videos'
output_dir = './converted'
os.makedirs(output_dir, exist_ok=True)

files = [f for f in os.listdir(input_dir) if f.endswith(('.mp4', '.mkv', '.avi'))]

# 并行处理（如果有多 GPU 或资源充足）
for f in files:
    input_path = os.path.join(input_dir, f)
    output_path = os.path.join(output_dir, f.rsplit('.', 1)[0] + '.mp4')
    convert_video(input_path, output_path)
    print(f"已转换：{f}")
```

## 常见任务

### 转换为 Web 优化的 MP4

```bash
ffmpeg -i input.mp4 \\
    -c:v h264_nvenc -preset p5 -cq 23 \\
    -c:a aac -b:a 128k \\
    -movflags +faststart \\
    web_video.mp4
```

### 提取音频

```bash
ffmpeg -i video.mp4 -vn -c:a copy audio.aac
ffmpeg -i video.mp4 -vn -c:a libmp3lame -b:a 320k audio.mp3
```

### 添加字幕

```bash

# 将字幕烧录到视频中
ffmpeg -i input.mp4 -vf "subtitles=subs.srt" -c:v h264_nvenc output.mp4

# 作为软字幕嵌入
ffmpeg -i input.mp4 -i subs.srt -c:v copy -c:a copy -c:s mov_text output.mp4
```

### 裁剪视频

```bash

# 从 00:01:00 开始，持续 30 秒
ffmpeg -ss 00:01:00 -i input.mp4 -t 30 -c:v h264_nvenc output.mp4

# 从起始到结束时间戳
ffmpeg -i input.mp4 -ss 00:00:30 -to 00:02:00 -c:v h264_nvenc output.mp4
```

### 拼接视频

```bash

# 创建文件列表
echo "file 'video1.mp4'" > list.txt
echo "file 'video2.mp4'" >> list.txt
echo "file 'video3.mp4'" >> list.txt

# 拼接
ffmpeg -f concat -safe 0 -i list.txt -c:v h264_nvenc output.mp4
```

### 创建 GIF

```bash
ffmpeg -i input.mp4 -vf "fps=10,scale=480:-1:flags=lanczos" output.gif
```

### 提取帧

```bash

# 每一帧
ffmpeg -i input.mp4 frames/frame_%04d.png

# 每 1 秒
ffmpeg -i input.mp4 -vf "fps=1" frames/frame_%04d.png
```

### 帧转视频

```bash
ffmpeg -framerate 30 -i frames/frame_%04d.png -c:v h264_nvenc output.mp4
```

## 流式输出

### RTMP 流

```bash
ffmpeg -re -i input.mp4 \\
    -c:v h264_nvenc -preset p4 -b:v 4M \\
    -c:a aac -b:a 128k \\
    -f flv rtmp://server/live/stream
```

### HLS 输出

```bash
ffmpeg -i input.mp4 \\
    -c:v h264_nvenc -preset p5 \\
    -c:a aac \\
    -f hls -hls_time 10 -hls_list_size 0 \\
    output.m3u8
```

## 性能对比

### 编码速度（4K 视频）

| 编码器         | GPU/CPU  | 速度        |
| ----------- | -------- | --------- |
| libx264     | CPU（8 核） | \~30 fps  |
| h264\_nvenc | RTX 3090 | \~300 fps |
| h264\_nvenc | RTX 4090 | \~450 fps |
| hevc\_nvenc | RTX 3090 | \~200 fps |
| hevc\_nvenc | RTX 4090 | \~350 fps |

## 高级选项

### 两遍编码

```bash

# 第 1 遍
ffmpeg -i input.mp4 -c:v h264_nvenc -preset p7 -b:v 10M -pass 1 -f null /dev/null

# 第 2 遍
ffmpeg -i input.mp4 -c:v h264_nvenc -preset p7 -b:v 10M -pass 2 output.mp4
```

### B 帧与 GOP

```bash
ffmpeg -i input.mp4 \\
    -c:v h264_nvenc \\
    -bf 2 \           # B 帧
    -g 60 \           # GOP 大小
    -keyint_min 30 \  # 最小关键帧间隔
    output.mp4
```

### HDR 支持（HEVC）

```bash
ffmpeg -i hdr_input.mp4 \\
    -c:v hevc_nvenc \\
    -preset p5 \\
    -profile:v main10 \\
    -pix_fmt p010le \\
    hdr_output.mp4
```

## 多 GPU

```bash

# 使用指定 GPU
ffmpeg -hwaccel cuda -hwaccel_device 0 -i input.mp4 -c:v h264_nvenc output.mp4

# 在不同 GPU 上并行编码
ffmpeg -hwaccel cuda -hwaccel_device 0 -i video1.mp4 -c:v h264_nvenc out1.mp4 &
ffmpeg -hwaccel cuda -hwaccel_device 1 -i video2.mp4 -c:v h264_nvenc out2.mp4 &
wait
```

## 故障排查

### 未找到 NVENC

```bash

# 检查 NVIDIA 驱动
nvidia-smi

# 检查 FFmpeg 构建
ffmpeg -encoders | grep nvenc
```

### 编码失败

```bash

# 减少并发会话（NVENC 限制）

# 消费级 GPU：3-5 个会话

# 专业级 GPU：不限
```

### 质量较差

* 使用更高预设（p6、p7）
* 降低 CQ 值（18-20）
* 提高码率

## 成本估算

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) *以获取当前费率。*

**节省费用：**

* 使用 **竞价** 可中断工作市场——约三分之一的服务器将现货价格定得低于按需价格（中位数约优惠 13%），其余则与按需价格持平
* 使用 **CLORE** 代币支付
* 比较不同提供商的价格

## 下一步

* [AI 视频生成](/guides/guides_v2-zh/shi-pin-sheng-cheng/ai-video-generation.md)
* [RIFE 插帧](/guides/guides_v2-zh/shi-pin-chu-li/rife-interpolation.md)
* [Real-ESRGAN 超分辨率放大](/guides/guides_v2-zh/tu-xiang-chu-li/real-esrgan-upscaling.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-chu-li/ffmpeg-nvenc.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.
