> 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/rife-interpolation.md).

# RIFE 插帧

使用 RIFE AI 插帧提升视频帧率

使用 RIFE AI 插帧提升视频帧率。

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

## 什么是 RIFE？

RIFE（实时中间光流估计）可以：

* 提高 FPS（24→60，30→120）
* 创建平滑的慢动作
* 修复卡顿画面
* 实时处理

## 快速部署

**Docker 镜像：**

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

**端口：**

```
22/tcp
```

**命令：**

```bash
pip install torch torchvision && \\
git clone https://github.com/megvii-research/ECCV2022-RIFE.git && \\
cd ECCV2022-RIFE && \\
pip install -r requirements.txt
```

## 安装

### 选项 1：Python 包

```bash
pip install rife-ncnn-vulkan-python
```

### 选项 2：从源码安装

```bash
git clone https://github.com/megvii-research/ECCV2022-RIFE.git
cd ECCV2022-RIFE
pip install -r requirements.txt
```

## 基础用法

### 帧率翻倍

```bash
python inference_video.py --exp=1 --video=input.mp4

# 输出：2 倍 FPS
```

### 4 倍帧率

```bash
python inference_video.py --exp=2 --video=input.mp4

# 输出：4 倍 FPS
```

### 8 倍帧率

```bash
python inference_video.py --exp=3 --video=input.mp4

# 输出：8 倍 FPS
```

## Python API

### 加载模型

```python
import torch
from model.RIFE import Model

device = torch.device("cuda")
model = Model()
model.load_model('./train_log', -1)
model.eval()
model.device()
```

### 插值单帧

```python
import cv2
import numpy as np
import torch

def interpolate_frames(frame1, frame2, model, num_frames=1):
    """在两张图像之间插值帧"""
    # 准备张量
    img0 = torch.from_numpy(frame1).permute(2, 0, 1).float() / 255.0
    img1 = torch.from_numpy(frame2).permute(2, 0, 1).float() / 255.0

    img0 = img0.unsqueeze(0).cuda()
    img1 = img1.unsqueeze(0).cuda()

    # 插帧
    with torch.no_grad():
        middle = model.inference(img0, img1)

    # 转回
    middle = (middle[0] * 255).byte().cpu().numpy().transpose(1, 2, 0)
    return middle

# 加载帧
frame1 = cv2.imread('frame1.png')
frame2 = cv2.imread('frame2.png')

# 获取插值帧
middle_frame = interpolate_frames(frame1, frame2, model)
cv2.imwrite('interpolated.png', middle_frame)
```

### 处理视频

```python
import cv2
import torch
from model.RIFE import Model

model = Model()
model.load_model('./train_log', -1)
model.eval()
model.device()

def process_video(input_path, output_path, multiplier=2):
    cap = cv2.VideoCapture(input_path)
    fps = cap.get(cv2.CAP_PROP_FPS)
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

    out = cv2.VideoWriter(
        output_path,
        cv2.VideoWriter_fourcc(*'mp4v'),
        fps * multiplier,
        (width, height)
    )

    ret, prev_frame = cap.read()
    if not ret:
        return

    out.write(prev_frame)

    while True:
        ret, curr_frame = cap.read()
        if not ret:
            break

        # 准备张量
        img0 = torch.from_numpy(prev_frame).permute(2, 0, 1).float() / 255.0
        img1 = torch.from_numpy(curr_frame).permute(2, 0, 1).float() / 255.0

        img0 = img0.unsqueeze(0).cuda()
        img1 = img1.unsqueeze(0).cuda()

        # 生成中间帧
        for i in range(multiplier - 1):
            t = (i + 1) / multiplier
            with torch.no_grad():
                middle = model.inference(img0, img1, timestep=t)
            middle = (middle[0] * 255).byte().cpu().numpy().transpose(1, 2, 0)
            out.write(middle)

        out.write(curr_frame)
        prev_frame = curr_frame

    cap.release()
    out.release()

process_video('input.mp4', 'output_60fps.mp4', multiplier=2)
```

## 使用 rife-ncnn-vulkan

更快的 NCNN 实现：

```python
from rife_ncnn_vulkan import Rife

rife = Rife(gpu_id=0)

# 插帧
frame1 = Image.open('frame1.png')
frame2 = Image.open('frame2.png')
middle = rife.process(frame1, frame2)
middle.save('interpolated.png')
```

### 视频处理

```python
from rife_ncnn_vulkan import Rife
import cv2
from PIL import Image

rife = Rife(gpu_id=0, num_threads=4)

def interpolate_video(input_path, output_path, factor=2):
    cap = cv2.VideoCapture(input_path)
    fps = cap.get(cv2.CAP_PROP_FPS)
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

    out = cv2.VideoWriter(
        output_path,
        cv2.VideoWriter_fourcc(*'mp4v'),
        fps * factor,
        (width, height)
    )

    ret, prev = cap.read()
    out.write(prev)

    while True:
        ret, curr = cap.read()
        if not ret:
            break

        # 转换为 PIL
        prev_pil = Image.fromarray(cv2.cvtColor(prev, cv2.COLOR_BGR2RGB))
        curr_pil = Image.fromarray(cv2.cvtColor(curr, cv2.COLOR_BGR2RGB))

        # 插帧
        for i in range(factor - 1):
            t = (i + 1) / factor
            mid = rife.process(prev_pil, curr_pil, timestep=t)
            mid_cv = cv2.cvtColor(np.array(mid), cv2.COLOR_RGB2BGR)
            out.write(mid_cv)

        out.write(curr)
        prev = curr

    cap.release()
    out.release()
```

## 慢动作

创建平滑慢动作：

```python

# 原始：30 fps，10 秒 = 300 帧

# 8 倍插帧：2400 帧

# 以 30 fps 播放：80 秒（慢 8 倍）

python inference_video.py --exp=3 --video=input.mp4

# 这会创建 8 倍帧数，以原始 FPS 播放即可实现慢动作
```

### 慢动作脚本

```python
def create_slow_motion(input_path, output_path, slowdown_factor=4):
    """创建慢动作视频"""
    cap = cv2.VideoCapture(input_path)
    original_fps = cap.get(cv2.CAP_PROP_FPS)

    # 插值以获取更多帧
    exp = int(np.log2(slowdown_factor))
    interpolate_video(input_path, 'temp_interpolated.mp4', factor=slowdown_factor)

    # 以原始 FPS 重新编码
    cap2 = cv2.VideoCapture('temp_interpolated.mp4')
    width = int(cap2.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap2.get(cv2.CAP_PROP_FRAME_HEIGHT))

    out = cv2.VideoWriter(
        output_path,
        cv2.VideoWriter_fourcc(*'mp4v'),
        original_fps,  # 保持原始 FPS
        (width, height)
    )

    while True:
        ret, frame = cap2.read()
        if not ret:
            break
        out.write(frame)

    cap.release()
    cap2.release()
    out.release()
```

## 批量处理

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

def process_single(input_path, output_dir, factor=2):
    filename = os.path.basename(input_path)
    output_path = os.path.join(output_dir, f"interpolated_{filename}")
    interpolate_video(input_path, output_path, factor)
    return output_path

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

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

for video in videos:
    result = process_single(video, output_dir, factor=2)
    print(f"完成：{result}")
```

## 质量设置

### 模型版本

| 模型        | 质量 | 速度 |
| --------- | -- | -- |
| RIFE v4.6 | 最佳 | 慢  |
| RIFE v4.0 | 很高 | 中等 |
| RIFE-NCNN | 好  | 最快 |

### UHD 模式

适用于 4K+ 视频：

```bash
python inference_video.py --exp=1 --video=input.mp4 --UHD
```

## 内存优化

### 适用于有限显存

```python

# 分块处理
from model.RIFE import Model

model = Model()
model.load_model('./train_log', -1)
model.eval()

# 为大帧设置块大小

# model.inference 在内部处理分块
```

### 减少内存占用

```bash

# 使用 NCNN 版本（更节省内存）
pip install rife-ncnn-vulkan-python
```

## 性能

| 分辨率   | GPU      | 2 倍插值 FPS |
| ----- | -------- | --------- |
| 1080p | RTX 3090 | \~60 fps  |
| 1080p | RTX 4090 | \~100 fps |
| 4K    | RTX 3090 | \~15 fps  |
| 4K    | RTX 4090 | \~30 fps  |

## 故障排查

### 伪影/重影

* 使用场景检测跳过切换
* 降低插值倍率
* 检查快速运动

### 内存不足

* 使用 NCNN 版本
* 先以较低分辨率处理，再放大
* 减小批量大小

### 处理缓慢

* 使用 NCNN-Vulkan 版本
* 启用 GPU 加速
* 使用更小的模型

## 场景检测

跳过场景切换处的插值：

```python
from scenedetect import detect, ContentDetector

scenes = detect('input.mp4', ContentDetector())

# 不要在场景之间插值
for scene in scenes:
    print(f"场景：{scene[0].get_frames()} - {scene[1].get_frames()}")
```

## 成本估算

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** 代币支付
* 比较不同提供商的价格

## 下一步

* [FFmpeg NVENC](/guides/guides_v2-zh/shi-pin-chu-li/ffmpeg-nvenc.md) - 编码输出
* [Real-ESRGAN](/guides/guides_v2-zh/tu-xiang-chu-li/real-esrgan-upscaling.md) - 视频放大
* [AI 视频生成](/guides/guides_v2-zh/shi-pin-sheng-cheng/ai-video-generation.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/rife-interpolation.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.
