# FFmpeg NVENC

Hardware-accelerated video encoding with NVIDIA GPUs.

{% hint style="success" %}
All examples can be run on GPU servers rented through [CLORE.AI Marketplace](https://clore.ai/marketplace).
{% endhint %}

## Renting on CLORE.AI

1. Visit [CLORE.AI Marketplace](https://clore.ai/marketplace)
2. Filter by GPU type, VRAM, and price
3. Choose **On-Demand** (fixed rate) or **Spot** (bid price)
4. Configure your order:
   * Select Docker image
   * Set ports (TCP for SSH, HTTP for web UIs)
   * Add environment variables if needed
   * Enter startup command
5. Select payment: **CLORE**, **BTC**, or **USDT/USDC**
6. Create order and wait for deployment

### Access Your Server

* Find connection details in **My Orders**
* Web interfaces: Use the HTTP port URL
* SSH: `ssh -p <port> root@<proxy-address>`

## What is NVENC?

NVENC (NVIDIA Video Encoder) provides:

* 5-10x faster encoding than CPU
* H.264, H.265/HEVC, AV1 support
* Real-time 4K/8K encoding
* Low GPU compute usage

## Requirements

| Codec | Min GPU   | Recommended |
| ----- | --------- | ----------- |
| H.264 | GTX 600+  | RTX 3060+   |
| HEVC  | GTX 900+  | RTX 3070+   |
| AV1   | RTX 4000+ | RTX 4090    |

## Quick Deploy

**Docker Image:**

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

**Ports:**

```
22/tcp
```

**Command:**

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

## Check NVENC Support

```bash

# Check available encoders
ffmpeg -encoders | grep nvenc

# Should show:

# V....D h264_nvenc

# V....D hevc_nvenc

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

## Basic Encoding

### H.264 Encoding

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

### HEVC/H.265 Encoding

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

### AV1 Encoding (RTX 4000+)

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

## Presets

| Preset | Quality | Speed    |
| ------ | ------- | -------- |
| p1     | Lowest  | Fastest  |
| p2-p3  | Low     | Fast     |
| p4-p5  | Medium  | Balanced |
| p6-p7  | High    | Slow     |

```bash

# Fastest encoding
ffmpeg -i input.mp4 -c:v h264_nvenc -preset p1 output.mp4

# Best quality
ffmpeg -i input.mp4 -c:v h264_nvenc -preset p7 output.mp4
```

## Quality Control

### Constant Quality (CQ)

```bash

# Lower = better quality, larger file
ffmpeg -i input.mp4 -c:v h264_nvenc -cq 18 output.mp4

# Recommended values: 18-28
```

### Constant Bitrate (CBR)

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

### Variable Bitrate (VBR)

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

## Resolution & Scaling

### Resize Video

```bash

# Scale to 1080p
ffmpeg -i input.mp4 -vf "scale=1920:1080" -c:v h264_nvenc output.mp4

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

# Maintain aspect ratio
ffmpeg -i input.mp4 -vf "scale=-1:1080" -c:v h264_nvenc output.mp4
```

### GPU Scaling (Faster)

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

## Hardware Decoding + Encoding

Full GPU pipeline:

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

## Batch Conversion

### Shell Script

```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 "Converted: $filename"
    fi
done
```

### Python Batch

```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'))]

# Process in parallel (if multi-GPU or enough resources)
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"Converted: {f}")
```

## Common Tasks

### Convert to Web-Optimized 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
```

### Extract Audio

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

### Add Subtitles

```bash

# Burn subtitles into video
ffmpeg -i input.mp4 -vf "subtitles=subs.srt" -c:v h264_nvenc output.mp4

# Embed as soft subtitles
ffmpeg -i input.mp4 -i subs.srt -c:v copy -c:a copy -c:s mov_text output.mp4
```

### Trim Video

```bash

# From 00:01:00 for 30 seconds
ffmpeg -ss 00:01:00 -i input.mp4 -t 30 -c:v h264_nvenc output.mp4

# From start to end timestamp
ffmpeg -i input.mp4 -ss 00:00:30 -to 00:02:00 -c:v h264_nvenc output.mp4
```

### Concatenate Videos

```bash

# Create file list
echo "file 'video1.mp4'" > list.txt
echo "file 'video2.mp4'" >> list.txt
echo "file 'video3.mp4'" >> list.txt

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

### Create GIF

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

### Extract Frames

```bash

# Every frame
ffmpeg -i input.mp4 frames/frame_%04d.png

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

### Frames to Video

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

## Streaming

### RTMP Stream

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

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

## Performance Comparison

### Encoding Speed (4K Video)

| Encoder     | GPU/CPU      | Speed     |
| ----------- | ------------ | --------- |
| libx264     | CPU (8 core) | \~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 |

## Advanced Options

### Two-Pass Encoding

```bash

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

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

### B-Frames & GOP

```bash
ffmpeg -i input.mp4 \
    -c:v h264_nvenc \
    -bf 2 \           # B-frames
    -g 60 \           # GOP size
    -keyint_min 30 \  # Min keyframe interval
    output.mp4
```

### HDR Support (HEVC)

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

## Multi-GPU

```bash

# Use specific GPU
ffmpeg -hwaccel cuda -hwaccel_device 0 -i input.mp4 -c:v h264_nvenc output.mp4

# Parallel encoding on different GPUs
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
```

## Troubleshooting

### NVENC Not Found

```bash

# Check NVIDIA driver
nvidia-smi

# Check FFmpeg build
ffmpeg -encoders | grep nvenc
```

### Encoding Failed

```bash

# Reduce concurrent sessions (NVENC limit)

# Consumer GPUs: 3-5 sessions

# Professional GPUs: unlimited
```

### Poor Quality

* Use higher preset (p6, p7)
* Lower CQ value (18-20)
* Increase bitrate

## Cost Estimate

Typical CLORE.AI marketplace rates (as of 2024):

| GPU       | Hourly Rate | Daily Rate | 4-Hour Session |
| --------- | ----------- | ---------- | -------------- |
| 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        |

*Prices vary by provider and demand. Check* [*CLORE.AI Marketplace*](https://clore.ai/marketplace) *for current rates.*

**Save money:**

* Use **Spot** market for flexible workloads (often 30-50% cheaper)
* Pay with **CLORE** tokens
* Compare prices across different providers

## Next Steps

* [AI Video Generation](https://docs.clore.ai/guides/video-generation/ai-video-generation)
* [RIFE Interpolation](https://docs.clore.ai/guides/video-processing/rife-interpolation)
* [Real-ESRGAN Upscaling](https://docs.clore.ai/guides/image-processing/real-esrgan-upscaling)


---

# 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/video-processing/ffmpeg-nvenc.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.
