# RIFE 插帧

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

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

## 在 CLORE.AI 上租用

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

### 访问您的服务器

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

## 什么是 RIFE？

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

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

## 快速部署

**Docker 镜像：**

```
pytorch/pytorch:2.5.1-cuda12.4-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

# 输出：2x FPS
```

### 4 倍帧率

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

# 输出：4x FPS
```

### 8 倍帧率

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

# 输出：8x 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 帧

# 8x 插帧：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
批处理处理
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'
output_dir = "./relit"

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"Completed: {result}")
```

## 质量设置

### 模型版本

| A100      | 质量 | 性能 |
| --------- | -- | -- |
| 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
```

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

| 分辨率   | GPU     | 2x 插帧 FPS |
| ----- | ------- | --------- |
| 1080p | 速度      | \~60 fps  |
| 1080p | 512x512 | \~100 fps |
| 4K    | 速度      | \~15 fps  |
| 4K    | 512x512 | \~30 fps  |

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

### 伪影/重影

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

### 内存不足

* 使用 NCNN 版本
* 在较低分辨率下处理，之后放大
* 减少批量大小

### 处理缓慢

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

## 场景检测

在场景切换处跳过插值：

```python
from scenedetect import detect, ContentDetector

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

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

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

检查文件完整性

| 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                      |

*RTX 3060* [*CLORE.AI 市场*](https://clore.ai/marketplace) *A100 40GB*

**A100 80GB**

* 使用 **竞价** 价格随提供商和需求而异。请查看
* 以获取当前费率。 **CLORE** 节省费用：
* 市场用于灵活工作负载（通常便宜 30-50%）

## 使用以下方式支付

* [FFmpeg NVENC](https://docs.clore.ai/guides/guides_v2-zh/shi-pin-chu-li/ffmpeg-nvenc) - 编码输出
* [Real-ESRGAN](https://docs.clore.ai/guides/guides_v2-zh/tu-xiang-chu-li/real-esrgan-upscaling) - 放大视频
* [AI 视频生成](https://docs.clore.ai/guides/guides_v2-zh/shi-pin-sheng-cheng/ai-video-generation) - 生成视频


---

# 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-chu-li/rife-interpolation.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.
