4.2 KiB
4.2 KiB
🧠 AI Video Upscaler - Project Context & Reference
Welcome! This document acts as a memory reference folder for AI agents working on this project. It details the system architecture, code organization, pipeline stages, and custom feature implementations (Resumption & Queue Management).
🏗️ System Architecture
The AI Video Upscaler is a full-stack, single-user application designed to upscale videos locally using Vulkan GPU-accelerated AI models.
Technology Stack
- FastAPI (Python Backend): Handles API endpoints, serving static assets, WebSockets, background threads, and orchestrates subprocess execution.
- Real-ESRGAN ncnn-vulkan (AI Engine): A compiled C++ Vulkan binary located in
realesrgan-bin/realesrgan-ncnn-vulkan. - FFmpeg & FFprobe (Multimedia Toolkit): Splitting videos into high-quality JPEG frames, querying metadata, extracting audio streams, and re-muxing audio/subtitles back into the upscaled output.
- Vanilla HTML/CSS/JS (Frontend): Cyberpunk-themed web dashboard with real-time progress logging, side-by-side comparative preview, and queue management.
📁 Key File Map
app/main.py: API route definitions, WebSocket orchestrator, custom thread-safe job queue, and JSON database state preservation (jobs.json).app/upscaler.py: Core pipeline orchestrator (run_upscale_pipeline). Executes external FFmpeg and Real-ESRGAN commands. Contains the resume logic.static/index.html,static/styles.css,static/app.js: Frontend interface, WebSocket handlers, interactive comparison slider, zoom/pan tool, and queue reordering actions.jobs.json: Persisted file storing details of all jobs (automatically generated at root).
🔄 Upscale Pipeline Lifecycle
A typical upscale job follows these sequential steps:
- Queued (
queued): Added to the FIFO queue. - Analyzing (
analyzing): GPU lock acquired. Querying stream specs withffprobe. - Extracting (
extracting): FFmpeg extracts video frames intotemp/<job_id>/input_frames/frame_%08d.jpg. - Upscaling (
upscaling): Real-ESRGAN binary upscales images totemp/<job_id>/output_frames/frame_%08d.jpg. - Assembling (
assembling): FFmpeg merges upscaled frames with original audio/subtitles. - Completed (
completed) / Failed (failed) / Cancelled (cancelled) / Interrupted (interrupted).
⚡ Custom Enhancements
1. Job Resumption (interrupted status)
- Persistency: The status of all jobs is stored in
jobs.jsonat the root. On application restart, any incomplete/active job is automatically loaded in theinterruptedstatus. - Skip Extraction: On resume, if
temp/<job_id>/input_framescontains frames, the extraction step is bypassed. - Incremental Upscaling: The upscaler inspects
output_frames/and checks for already upscaled frames. It removes corresponding files frominput_frames/, meaning the AI model only processes the remaining un-upscaled frames. - Fast Assembly: If all frames are already upscaled, it skips the upscaling step entirely and directly runs the FFmpeg reassembly.
2. Queue Management (Cancel & Reorder)
- Custom Queue (
CustomJobQueue): A thread-safe, list-backed FIFO queue replacing the standardqueue.Queue. It allows:- Querying the active queue list (
GET /api/queue). - Swapping queued jobs and re-ordering (
POST /api/queue/reorder). - Graceful removal upon cancellation.
- Querying the active queue list (
- Interactive UI Arrows: Arrows are displayed next to queued items in the dashboard to move jobs up and down, triggering the API to swap their order on-the-fly.
3. Automatic Virtual Environment Setup (start.py)
- Self-Sufficiency: Running
python3 start.pyautomatically checks for a local virtual environment (venv/or.venv/). - Auto-Provisioning: If no virtual environment is found,
start.pywill initialize one invenv/, upgradepip, install all dependencies listed inrequirements.txt, mark the Real-ESRGAN binary as executable (chmod +x), and create necessary folders (uploads/,outputs/,temp/). - Seamless Launch: It then automatically launches the server process using the newly created environment interpreter.