# krmanik/yt-sub-ocr architecture

> Architecture diagram of the GitHub repository krmanik/yt-sub-ocr, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/krmanik/yt-sub-ocr
- Repository: https://github.com/krmanik/yt-sub-ocr

## Overview

1. **Repository purpose**
   - This is a **browser userscript** for **YouTube** that extracts **hardcoded/burned-in subtitles** by running OCR on the video image itself, not on YouTube caption tracks.
   - There is **no backend service** or server-side component; everything runs inside the user’s browser.

2. **Top-level architecture**
   - The repo is very small and effectively split into:
     - `yt-sub-ocr.user.js`: the **userscript entrypoint** injected into YouTube pages.
     - `paddleocr.js`: the **OCR integration/runtime module** used by the userscript.
   - The README describes the behavior and the external libraries it relies on.

3. **Execution boundary**
   - The script runs in the **userscript manager runtime** (e.g. Tampermonkey/Greasemonkey) and attaches itself to the **YouTube page DOM**.
   - It operates as an **in-page augmentation**, meaning it adds UI and behavior without replacing YouTube’s app.

4. **UI subsystem**
   - The userscript adds controls below the video area:
     - a **Crop** action to define the region of interest,
     - an **OCR** action to start recognition.
   - It also renders the **recognized subtitle text** back into the page below the video canvas.

5. **Crop/region-selection subsystem**
   - Cropping is used to limit OCR to the area where subtitles appear.
   - The README explicitly states this is powered by **cropperjs**, so the crop flow is a first-class part of the UI/interaction model rather than a simple fixed screenshot.

6. **Video/frame capture and control**
   - The OCR pipeline works on the **current video frame** or its rendered canvas representation.
   - The script **automatically pauses playback** during OCR to reduce motion blur and improve recognition accuracy.
   - This creates a dependency on YouTube’s player state and DOM structure.

7. **OCR subsystem**
   - OCR is implemented via **paddlejs-ocr** (mentioned in the README), with `paddleocr.js` likely acting as the local wrapper or integration layer.
   - The OCR engine consumes the selected cropped image region and returns text for display.

8. **Data flow**
   - The flow is:
     - YouTube video renders in the browser,
     - the user selects a crop region,
     - the script pauses playback,
     - the selected image region is sent to OCR,
     - recognized text is inserted back into the page.
   - The main data artifact is thus a **cropped video frame image**, transformed into **subtitle text**.

9. **External dependencies and their roles**
   - **YouTube**: the host application and DOM surface being augmented.
   - **Userscript manager**: the installation/runtime mechanism.
   - **cropperjs**: region selection UI.
   - **paddlejs-ocr**: text recognition engine.

10. **Operational characteristics**
   - The system is **interactive and client-side**, not batch or headless.
   - There is no persistence layer, queue, API, or database.
   - The architecture is centered on **DOM injection + frame capture + OCR + text display**.

11. **What an architecture diagram should show**
   - A single browser client boundary containing:
     - the YouTube page/player,
     - the injected userscript,
     - a crop-selection UI,
     - an OCR engine/module,
     - and a text output panel.
   - The only external dependencies worth showing are the userscript manager and the two libraries above.

Main entry points: [`yt-sub-ocr.user.js`](https://github.com/krmanik/yt-sub-ocr/blob/main/yt-sub-ocr.user.js) (Userscript), [`paddleocr.js`](https://github.com/krmanik/yt-sub-ocr/blob/main/paddleocr.js) (OCR module)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Browser client"]
  node_userscript["Userscript<br/>entrypoint<br/>[yt-sub-ocr.user.js]"]
  node_page["YouTube page<br/>host DOM"]
  node_ui["Video controls<br/>in-page UI"]
  node_cropper["Crop selector<br/>region picker"]
  node_capture["Frame capture<br/>frame control"]
  node_ocrmod["OCR module<br/>integration layer<br/>[paddleocr.js]"]
  node_output["Subtitle text<br/>rendered output"]
end

subgraph group_deps["External deps"]
  node_tm["Userscript manager<br/>runtime"]
  node_cropperjs["cropperjs"]
  node_paddlejs["paddlejs-ocr"]
end

node_tm -->|"injects"| node_userscript
node_userscript -->|"attaches to"| node_page
node_userscript -->|"adds"| node_ui
node_ui -->|"opens"| node_cropper
node_cropper -->|"uses"| node_cropperjs
node_ui -->|"triggers"| node_capture
node_capture -->|"reads frame"| node_page
node_capture -->|"sends image"| node_ocrmod
node_ocrmod -->|"runs on"| node_paddlejs
node_capture -->|"pauses playback"| node_page
node_ocrmod -->|"returns text"| node_output
node_userscript -->|"loads"| node_ocrmod

click node_userscript "https://github.com/krmanik/yt-sub-ocr/blob/main/yt-sub-ocr.user.js"
click node_ocrmod "https://github.com/krmanik/yt-sub-ocr/blob/main/paddleocr.js"

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_userscript,node_page,node_ui,node_cropper,node_capture,node_ocrmod,node_output toneBlue
class node_tm,node_cropperjs,node_paddlejs toneAmber
```

## Components

### Browser client

In-page YouTube augmentation running entirely in the browser

- [Userscript](https://github.com/krmanik/yt-sub-ocr/blob/main/yt-sub-ocr.user.js): entrypoint · Injected script that initializes the OCR workflow on YouTube pages · `yt-sub-ocr.user.js`
- **YouTube page**: host DOM · Host page and player surface that the script augments
- **Video controls**: in-page UI · Crop and OCR actions added below the video area
- **Crop selector**: region picker · Interactive crop flow used to define the subtitle region
- **Frame capture**: frame control · Pauses playback and captures the current video frame or canvas image
- [OCR module](https://github.com/krmanik/yt-sub-ocr/blob/main/paddleocr.js): integration layer · Local wrapper around the OCR engine used by the userscript · `paddleocr.js`
- **Subtitle text**: rendered output · Recognized text rendered back into the page below the video

### External deps

Runtime and library dependencies used by the userscript

- **Userscript manager**: runtime · Tampermonkey/Greasemonkey-style runtime that injects the script
- **cropperjs**: library · Library providing the crop region selection interaction
- **paddlejs-ocr**: library · OCR engine used to recognize burned-in subtitles from image regions

## Connections

- Userscript manager → Userscript: injects · Loads the userscript into YouTube pages
- Userscript → YouTube page: attaches to · Binds behavior and UI into the YouTube DOM
- Userscript → Video controls: adds · Creates the Crop and OCR controls below the video
- Video controls → Crop selector: opens · Starts crop-region selection for subtitle extraction
- Crop selector → cropperjs: uses · Region selection is powered by cropperjs
- Video controls → Frame capture: triggers · OCR action causes the current frame to be prepared for recognition
- Frame capture → YouTube page: reads frame · Uses the playing video element or its rendered canvas image
- Frame capture → OCR module: sends image · Passes the cropped frame region into OCR processing
- OCR module → paddlejs-ocr: runs on · Delegates recognition to paddlejs-ocr
- Frame capture → YouTube page: pauses playback · Stops video motion during OCR to improve accuracy
- OCR module → Subtitle text: returns text · Recognized subtitle text is rendered back into the page
- Userscript → OCR module: loads · Imports the OCR integration module
