# nikopueringer/corridorkey architecture

> Architecture diagram of the GitHub repository nikopueringer/corridorkey, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nikopueringer/corridorkey
- Repository: https://github.com/nikopueringer/corridorkey

## Overview

1. **Overall shape**
   CorridorKey is a Python-based batch media processing tool, not a service or web app. Its architecture is centered on a CLI-driven workflow that takes shot folders or loose clips, generates/ingests an alpha hint, runs neural inference, and writes keying outputs back to disk.

2. **Primary entry points**
   The main user-facing orchestrators are `clip_manager.py` and `corridorkey_cli.py`. These expose commands like wizard/list/run_inference and are responsible for device/backend selection, clip discovery, and launching the processing pipeline. The shell/batch launchers and install scripts are just wrappers around these entry points.

3. **Workflow orchestration layer**
   The `backend/` package is the repository’s coordination layer. It handles clip state, job queuing, project structure, validation, frame I/O, ffmpeg integration, and error handling. This is where the “batch processor” behavior lives: organizing clips into shot folders, deciding what is ready to process, and tracking per-clip progress and outputs.

4. **Project and clip model**
   `backend/project.py`, `backend/clip_state.py`, and `backend/validators.py` define the repository’s notion of a valid shot. The expected folder structure is important: an input clip sequence plus hint folders such as `AlphaHint/` and optional `VideoMamaMaskHint/`. The orchestration layer uses this structure to pair footage with the correct hints and to decide whether a clip needs auto-organization.

5. **Frame and file boundary**
   `backend/frame_io.py` and `backend/ffmpeg_tools.py` form the boundary between filesystem/video assets and model-ready tensors. They are the bridge that reads image sequences or encoded media, converts frames into the formats the models expect, and writes output images/video-compatible artifacts back to disk.

6. **Core CorridorKey inference**
   `CorridorKeyModule/` contains the main unmixing model implementation. `wrapper.py` is the high-level model entry, while `inference_engine.py` and `core/model_transformer.py` represent the actual inference pipeline and tensor transformations. `core/color_utils.py` indicates that color-space handling is a first-class concern, which matches the repo’s emphasis on linear vs sRGB processing and preserving straight color plus alpha.

7. **Main model outputs**
   The core model is designed to produce two key signals per pixel: a clean linear alpha matte and the straight foreground color. The repo’s output structure reflects this separation: `/Matte` for alpha, `/FG` for foreground color, `/Processed` for premultiplied preview/compositing convenience, and `/Comp` for quick checkerboard previews.

8. **Alpha hint generation subsystem**
   The repository integrates two optional “hint generator” subsystems to supply coarse masks to the main keyer:
   - `gvm_core/` packages Generative Video Matting.
   - `VideoMaMaInferenceModule/` packages VideoMaMa.
   These are not required for the keyer itself, but they are important upstream dependencies in the full workflow because CorridorKey’s quality depends heavily on the hint quality.

9. **Optional hint backends**
   `gvm_core/gvm/...` contains the GVM model and pipeline code, and `VideoMaMaInferenceModule/pipeline.py` plus `inference.py` contain the VideoMaMa inference path. The orchestration layer can invoke these modules to generate alpha hints automatically when the user has not provided one manually.

10. **Lightweight hinting / auxiliary model support**
    `BiRefNetModule/` is a separate optional alpha-hint generator path. It is much lighter than GVM/VideoMaMa and functions as another upstream way to produce coarse subject masks for the main CorridorKey model.

11. **Backend and hardware selection boundary**
    `device_utils.py` and the README’s backend logic show that inference can run through multiple runtimes: Torch on CUDA/MPS/CPU, or MLX on Apple Silicon. This affects architecture because device/backend choice happens before model execution and determines which code path, weights format, and runtime constraints are used.

12. **Weights and checkpoint management**
    The repo separates code from large model assets. Each major subsystem has its own checkpoint/weights directory: `CorridorKeyModule/checkpoints`, `gvm_core/weights`, `VideoMaMaInferenceModule/checkpoints`, and `BiRefNetModule/checkpoints`. The application auto-downloads or expects these assets at runtime, so the architecture depends on external model repositories and local cache folders.

13. **Processing lifecycle**
    The end-to-end flow is: discover clips → validate/organize shot folders → acquire or generate alpha hints → select screen color/backend/device → run core unmixing inference → write EXR/preview outputs. `backend/job_queue.py` and `backend/service.py` suggest the run is managed as queued work rather than a one-shot script.

14. **Testing and support tooling**
    `tests/` covers the key architecture boundaries: backend behavior, CLI behavior, device selection, frame I/O, color math, and end-to-end workflow. `scripts/convert_pth_to_safetensors.py` and the Docker/compose files support deployment and model packaging, but they are ancillary to the runtime architecture.

15. **What to draw in an architecture diagram**
    The diagram should emphasize three tiers:
    - **CLI/orchestration**: user commands, clip manager, backend services, project validation.
    - **Inference engines**: CorridorKey core model plus optional hint generators (GVM, VideoMaMa, BiRefNet).
    - **Asset/runtime boundaries**: clip folders, hint folders, checkpoint/weight stores, ffmpeg/frame I/O, and device/backend selection.
    The main dataflow is from input footage and coarse hint to separated foreground/alpha outputs, with optional auto-generated hints feeding the same inference path.

Main entry points: [`backend/service.py`](https://github.com/nikopueringer/corridorkey/blob/main/backend/service.py) (Service), [`CorridorKeyModule/backend.py`](https://github.com/nikopueringer/corridorkey/blob/main/CorridorKeyModule/backend.py) (Core wrapper), [`CorridorKeyModule/inference_engine.py`](https://github.com/nikopueringer/corridorkey/blob/main/CorridorKeyModule/inference_engine.py) (Inference engine), [`backend/project.py`](https://github.com/nikopueringer/corridorkey/blob/main/backend/project.py) (Project)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_orchestration["CLI & backend"]
  node_clip_manager["Clip manager<br/>CLI orchestrator<br/>[clip_manager.py]"]
  node_corridorkey_cli["CLI<br/>command entry<br/>[corridorkey_cli.py]"]
  node_backend_service["Service<br/>batch coordinator<br/>[service.py]"]
  node_job_queue["Job queue<br/>work scheduler<br/>[job_queue.py]"]
  node_project_model["Project<br/>shot model<br/>[project.py]"]
  node_clip_state["Clip state<br/>state model<br/>[clip_state.py]"]
  node_validators["Validators<br/>layout rules<br/>[validators.py]"]
end

subgraph group_inference["Model engines"]
  node_core_wrapper["Core wrapper<br/>model entry<br/>[backend.py]"]
  node_inference_engine["Inference engine<br/>tensor pipeline"]
  node_model_transformer["Transformer<br/>tensor transform"]
  node_color_utils["Color utils<br/>color science<br/>[color_utils.py]"]
  node_gvm_wrapper["GVM wrapper<br/>hint generator<br/>[wrapper.py]"]
  node_videomama["VideoMaMa<br/>hint generator<br/>[pipeline.py]"]
  node_birefnet["BiRefNet<br/>hint generator<br/>[wrapper.py]"]
end

subgraph group_runtime["Assets & runtime"]
  node_frame_io["Frame I/O<br/>media boundary<br/>[frame_io.py]"]
  node_ffmpeg_tools["FFmpeg<br/>media tools<br/>[ffmpeg_tools.py]"]
  node_device_utils["Device utils<br/>runtime selection<br/>[device_utils.py]"]
  node_corridor_weights[("CK weights<br/>checkpoint store")]
  node_gvm_weights[("GVM weights<br/>checkpoint store")]
  node_videomama_ckpts[("VM checkpoints<br/>checkpoint store")]
  node_birefnet_ckpts[("BiRefNet ckpts<br/>checkpoint store")]
end

node_clip_manager -->|"launches"| node_backend_service
node_corridorkey_cli -->|"dispatches"| node_backend_service
node_backend_service -->|"queues"| node_job_queue
node_backend_service -->|"loads"| node_project_model
node_backend_service -->|"validates"| node_validators
node_project_model -->|"tracks"| node_clip_state
node_validators -->|"gates"| node_project_model
node_job_queue -->|"selects runtime"| node_device_utils
node_backend_service -->|"reads frames"| node_frame_io
node_backend_service -->|"writes media"| node_ffmpeg_tools
node_frame_io -->|"feeds frames"| node_core_wrapper
node_core_wrapper -->|"runs"| node_inference_engine
node_inference_engine -->|"prepares tensors"| node_model_transformer
node_inference_engine -->|"applies color"| node_color_utils
node_gvm_wrapper -->|"loads"| node_gvm_weights
node_videomama -->|"loads"| node_videomama_ckpts
node_birefnet -->|"loads"| node_birefnet_ckpts
node_gvm_wrapper -.->|"supplies hints"| node_backend_service
node_videomama -.->|"supplies hints"| node_backend_service
node_birefnet -.->|"supplies hints"| node_backend_service
node_backend_service -->|"invokes core"| node_core_wrapper
node_core_wrapper -->|"loads"| node_corridor_weights

click node_clip_manager "https://github.com/nikopueringer/corridorkey/blob/main/clip_manager.py"
click node_corridorkey_cli "https://github.com/nikopueringer/corridorkey/blob/main/corridorkey_cli.py"
click node_backend_service "https://github.com/nikopueringer/corridorkey/blob/main/backend/service.py"
click node_job_queue "https://github.com/nikopueringer/corridorkey/blob/main/backend/job_queue.py"
click node_project_model "https://github.com/nikopueringer/corridorkey/blob/main/backend/project.py"
click node_clip_state "https://github.com/nikopueringer/corridorkey/blob/main/backend/clip_state.py"
click node_validators "https://github.com/nikopueringer/corridorkey/blob/main/backend/validators.py"
click node_frame_io "https://github.com/nikopueringer/corridorkey/blob/main/backend/frame_io.py"
click node_ffmpeg_tools "https://github.com/nikopueringer/corridorkey/blob/main/backend/ffmpeg_tools.py"
click node_device_utils "https://github.com/nikopueringer/corridorkey/blob/main/device_utils.py"
click node_core_wrapper "https://github.com/nikopueringer/corridorkey/blob/main/CorridorKeyModule/backend.py"
click node_inference_engine "https://github.com/nikopueringer/corridorkey/blob/main/CorridorKeyModule/inference_engine.py"
click node_model_transformer "https://github.com/nikopueringer/corridorkey/blob/main/CorridorKeyModule/core/model_transformer.py"
click node_color_utils "https://github.com/nikopueringer/corridorkey/blob/main/CorridorKeyModule/core/color_utils.py"
click node_gvm_wrapper "https://github.com/nikopueringer/corridorkey/blob/main/gvm_core/wrapper.py"
click node_videomama "https://github.com/nikopueringer/corridorkey/blob/main/VideoMaMaInferenceModule/pipeline.py"
click node_birefnet "https://github.com/nikopueringer/corridorkey/blob/main/BiRefNetModule/wrapper.py"
click node_corridor_weights "https://github.com/nikopueringer/corridorkey/tree/main/CorridorKeyModule/checkpoints"
click node_gvm_weights "https://github.com/nikopueringer/corridorkey/tree/main/gvm_core/weights"
click node_videomama_ckpts "https://github.com/nikopueringer/corridorkey/tree/main/VideoMaMaInferenceModule/checkpoints"
click node_birefnet_ckpts "https://github.com/nikopueringer/corridorkey/tree/main/BiRefNetModule/checkpoints"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_clip_manager,node_corridorkey_cli,node_backend_service,node_job_queue,node_project_model,node_clip_state,node_validators toneBlue
class node_core_wrapper,node_inference_engine,node_model_transformer,node_color_utils,node_gvm_wrapper,node_videomama,node_birefnet toneAmber
class node_frame_io,node_ffmpeg_tools,node_device_utils,node_corridor_weights,node_gvm_weights,node_videomama_ckpts,node_birefnet_ckpts toneMint
```

## Components

### CLI & backend

User entry points and batch workflow coordination

- [Clip manager](https://github.com/nikopueringer/corridorkey/blob/main/clip_manager.py): CLI orchestrator · Interactive clip discovery, backend selection, and workflow launch · `clip_manager.py`
- [CLI](https://github.com/nikopueringer/corridorkey/blob/main/corridorkey_cli.py): command entry · Command-line interface for running keying workflows · `corridorkey_cli.py`
- [Service](https://github.com/nikopueringer/corridorkey/blob/main/backend/service.py): batch coordinator · Top-level orchestration of queued clip processing · `backend/service.py`
- [Job queue](https://github.com/nikopueringer/corridorkey/blob/main/backend/job_queue.py): work scheduler · Queues and tracks per-clip processing work · `backend/job_queue.py`
- [Project](https://github.com/nikopueringer/corridorkey/blob/main/backend/project.py): shot model · Represents valid shot folders and their processing state · `backend/project.py`
- [Clip state](https://github.com/nikopueringer/corridorkey/blob/main/backend/clip_state.py): state model · Per-clip progress, readiness, and output status · `backend/clip_state.py`
- [Validators](https://github.com/nikopueringer/corridorkey/blob/main/backend/validators.py): layout rules · Checks folder structure, hints, and clip readiness · `backend/validators.py`

### Model engines

Core keying model and optional hint generators

- [Core wrapper](https://github.com/nikopueringer/corridorkey/blob/main/CorridorKeyModule/backend.py): model entry · High-level entry into CorridorKey inference · `CorridorKeyModule/backend.py`
- [Inference engine](https://github.com/nikopueringer/corridorkey/blob/main/CorridorKeyModule/inference_engine.py): tensor pipeline · Runs the main unmixing model pipeline · `CorridorKeyModule/inference_engine.py`
- [Transformer](https://github.com/nikopueringer/corridorkey/blob/main/CorridorKeyModule/core/model_transformer.py): tensor transform · Model-side tensor reshaping and inference preparation · `CorridorKeyModule/core/model_transformer.py`
- [Color utils](https://github.com/nikopueringer/corridorkey/blob/main/CorridorKeyModule/core/color_utils.py): color science · Linear and sRGB color handling for keying output · `CorridorKeyModule/core/color_utils.py`
- [GVM wrapper](https://github.com/nikopueringer/corridorkey/blob/main/gvm_core/wrapper.py): hint generator · Generative Video Matting hint generation entry point · `gvm_core/wrapper.py`
- [VideoMaMa](https://github.com/nikopueringer/corridorkey/blob/main/VideoMaMaInferenceModule/pipeline.py): hint generator · Optional video matting pipeline for alpha hints · `VideoMaMaInferenceModule/pipeline.py`
- [BiRefNet](https://github.com/nikopueringer/corridorkey/blob/main/BiRefNetModule/wrapper.py): hint generator · Lightweight optional mask generator for coarse hints · `BiRefNetModule/wrapper.py`

### Assets & runtime

Filesystem boundaries, checkpoints, and media I/O

- [Frame I/O](https://github.com/nikopueringer/corridorkey/blob/main/backend/frame_io.py): media boundary · Reads image sequences and prepares frame data · `backend/frame_io.py`
- [FFmpeg](https://github.com/nikopueringer/corridorkey/blob/main/backend/ffmpeg_tools.py): media tools · Video encoding and decoding helpers at the file boundary · `backend/ffmpeg_tools.py`
- [Device utils](https://github.com/nikopueringer/corridorkey/blob/main/device_utils.py): runtime selection · Selects Torch, MLX, CUDA, MPS, or CPU execution paths · `device_utils.py`
- [CK weights](https://github.com/nikopueringer/corridorkey/tree/main/CorridorKeyModule/checkpoints): checkpoint store · CorridorKey model checkpoints and local asset cache · `CorridorKeyModule/checkpoints`
- [GVM weights](https://github.com/nikopueringer/corridorkey/tree/main/gvm_core/weights): checkpoint store · Generative Video Matting model assets · `gvm_core/weights`
- [VM checkpoints](https://github.com/nikopueringer/corridorkey/tree/main/VideoMaMaInferenceModule/checkpoints): checkpoint store · VideoMaMa model checkpoints · `VideoMaMaInferenceModule/checkpoints`
- [BiRefNet ckpts](https://github.com/nikopueringer/corridorkey/tree/main/BiRefNetModule/checkpoints): checkpoint store · BiRefNet model checkpoints · `BiRefNetModule/checkpoints`

## Connections

- Clip manager → Service: launches · Starts batch processing and workflow actions
- CLI → Service: dispatches · Runs commands that reach the orchestration layer
- Service → Job queue: queues · Schedules clips for processing
- Service → Project: loads · Builds project and shot structure
- Service → Validators: validates · Checks clip folders, hints, and readiness
- Project → Clip state: tracks · Persists per-clip state and outputs
- Validators → Project: gates · Accepts or rejects shot organization
- Job queue → Device utils: selects runtime · Chooses backend and hardware before inference
- Service → Frame I/O: reads frames · Bridges media files into model-ready data
- Service → FFmpeg: writes media · Encodes outputs and handles video boundary
- Frame I/O → Core wrapper: feeds frames · Provides input frames to the model entry
- Core wrapper → Inference engine: runs · Invokes the core unmixing pipeline
- Inference engine → Transformer: prepares tensors · Transforms inputs for the model
- Inference engine → Color utils: applies color · Handles linear and sRGB conversions
- GVM wrapper → GVM weights: loads · Uses GVM checkpoints for hint generation
- VideoMaMa → VM checkpoints: loads · Uses VideoMaMa checkpoints for hints
- BiRefNet → BiRefNet ckpts: loads · Uses BiRefNet checkpoints for masks
- GVM wrapper → Service: supplies hints · Can generate alpha hints before main inference
- VideoMaMa → Service: supplies hints · Can generate alpha hints before main inference
- BiRefNet → Service: supplies hints · Can generate alpha hints before main inference
- Service → Core wrapper: invokes core · Hands validated clips and hints to CorridorKey
- Core wrapper → CK weights: loads · Reads CorridorKey checkpoints at runtime
