# VMAF Optimiser

**Intelligent Video Library Optimization Pipeline**

Automatically optimizes your video library (Movies/TV) by finding the best compression (AV1/HEVC) that maintains a high visual quality target (VMAF 93+).

## Features

- **Hybrid Encoding:** 
  - Uses **Hardware Acceleration** (NVIDIA NVENC, AMD AMF, Intel QSV, Apple VideoToolbox) for fast encoding.
  - Uses **Software Encoding** (CPU) as a robust fallback.
- **VMAF Targeted:** Ensures visual quality matches the original (Target VMAF 93+).
- **Multi-Platform:** Runs on **Windows**, **Linux**, and **macOS**.
- **Distributed Processing:** Run multiple workers across multiple PCs sharing the same network library.
- **Safety First:**
  - Locks files to prevent double-processing.
  - Verifies output size and integrity before replacing.
  - "Smart Resume" - skips already processed files.

---

## Directory Structure

```
VMAFOptimiser/
├── bin/                 # Place ab-av1.exe here (or install to PATH)
├── logs/                # Local logs (processed, rejected, stats)
├── locks/               # Local lock files (if network not available)
├── src/
│   ├── smart_gpu_encoder.py   # Main encoder engine
│   ├── smart_monitor.py       # TUI Dashboard (Watchdog)
│   ├── vmaf_common.py         # Shared logic
│   └── smart_encoder.py       # Legacy CPU-only engine
├── run.ps1              # Windows One-Click Start
└── README.md
```

## Setup & Installation

### 1. Requirements

- **Python 3.10+**
- **FFmpeg** (with libsvtav1/libx265 and HW drivers)
  - Windows: `choco install ffmpeg`
  - macOS: `brew install ffmpeg`
  - Linux: `sudo apt install ffmpeg`
- **ab-av1** (VMAF calculator)
  - Download from [GitHub](https://github.com/alexheretic/ab-av1) and place in `bin/` OR install via `cargo install ab-av1`

### 2. Install Python Deps

```bash
pip install watchdog rich
```

### 3. Usage

#### Interactive Dashboard (Recommended)

Monitors directories and shows a TUI with progress bars.

```bash
python src/smart_monitor.py --tv-dir "Z:\tv" --content-dir "Z:\content" --jobs 2
```

#### Headless / Background (Cron/Task Scheduler)

Runs once through the library and exits.

```bash
python src/smart_gpu_encoder.py --tv-dir "Z:\tv" --content-dir "Z:\content" --jobs 4
```

---

## Multi-PC Setup

To speed up processing, you can run this script on multiple computers simultaneously pointing to the same NAS/Network Share.

1. **Map Network Drive:** Ensure `Z:\` (or whatever path) is mapped on ALL computers.
2. **Shared Locks:** The script automatically attempts to create a `.vmaf_locks` folder in the parent directory of your TV folder (e.g., `Z:\.vmaf_locks`).
3. **Run:** Start the script on PC 1, PC 2, etc. They will respect each other's locks and not process the same file.

**Note:** Logs are stored LOCALLY on each PC in the `logs/` folder to prevent network file contention.

---

## Advanced Options

| Flag | Description |
|------|-------------|
| `--jobs N` | Number of parallel encoders (Default: 2) |
| `--tv-only` | Scan only TV Shows |
| `--content-only` | Scan only Movies |
| `--skip-until "Name"` | Skip files until this keyword is found (good for resuming) |
| `--debug` | Enable verbose logging |

---

## Credits

- Powered by `ab-av1` for VMAF calculation.
- Uses `ffmpeg` for heavy lifting.
