# retrogtx/youtube-clipper architecture

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

- Interactive diagram: https://gitdiagram.com/retrogtx/youtube-clipper
- Repository: https://github.com/retrogtx/youtube-clipper

## Overview

##### Project type and purpose
- This is a full-stack SaaS web application (“YouTube Clipper”) that lets authenticated users submit YouTube URLs and time-ranges, clips the video on the server, and streams/downloads the result.
- Frontend: Next.js 13 “app” directory + TailwindCSS + Shadcn/UI, with Drizzle ORM for user/session data.
- Backend: Bun-powered Node.js/Express microservice handling the heavy lifting (yt-dlp + ffmpeg).

##### File structure
- Top-level “backend” and “frontend” directories clearly separate UI/API from processing logic.
- Frontend “app”, “components”, “lib” + Drizzle config indicate an MVC/BFF-style pattern:
- “app” holds pages + Next API routes (auth).
- “lib” holds client/server helpers (auth-client, DB, schema).
- “drizzle” contains DB migrations and metadata.
- Backend is a single Express service (`src/index.ts`) with no UI.
- Presence of bun.lock in both indicates Bun runtime for each.

##### README for additional insights
- Mentions next-auth (via `[...all]` route), Drizzle ORM for database, and external CLI tools (yt-dlp, ffmpeg).
- No cloud storage: processed clips are streamed back, downloaded client-side.

##### Main components and their interactions
a. Frontend (Next.js UI + API)
- UI Layer: React pages (login/page.tsx, page.tsx) + Shadcn/UI components.
- Auth API: `/api/auth/[...all]` for sign-in/sign-out, backed by Drizzle + database.
- DB Layer: Drizzle ORM connects to Postgres/SQLite via `lib/db.ts` + `lib/schema.ts`.
- Clip Request UI: form that POSTs to Backend service.

b. Backend (Express microservice on Bun)
- Clip API endpoint (`POST /clip`): parses URL + timestamps.
- Video Processor: spawns `yt-dlp` to fetch raw media, then invokes `ffmpeg` to trim/encode.
- Response: streams binary `clip.mp4` back to the frontend for client download.
- (Optional) Temporary storage: `/uploads` folder or in-memory streams.

c. External Services
- yt-dlp CLI for partial YouTube downloads.
- ffmpeg CLI for editing and re-encoding.

d. Database
- Stores user accounts, sessions, and maybe clipping history.

##### Architectural patterns and design principles
- Clear separation of concerns:
- Frontend handles user interaction, authentication, and display.
- Backend focuses on CPU-heavy I/O tasks (video processing).
- Database isolates state.
- BFF pattern: Frontend’s Next API routes act as a middle layer for auth/DB.
- Microservice for video processing: can be scaled or containerized independently.
- Use of CLI tools via child processes for specialized workloads.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Browser
    Browser["Client Browser"]:::external

    %% Frontend Layer
    subgraph "Frontend (Next.js App - port 3000)"
        direction TB
        UI_Pages["UI Pages<br/>(page.tsx, login/page.tsx)"]:::frontend
        Auth_API["Auth API<br/>(NextAuth Route)"]:::frontend
        UI_Components["UI Components<br/>(Shadcn/UI & custom)"]:::frontend
        Styling["Styling & Layout<br/>(globals.css, layout.tsx)"]:::frontend
        DB_Layer["DB Layer<br/>(Drizzle ORM & Config)"]:::frontend
        Auth_Helpers["Auth Helpers<br/>(auth-client.ts, auth.ts)"]:::frontend
        Utils["Utility Functions<br/>(utils.ts)"]:::frontend
        Configs["Build & Config Files"]:::frontend
    end

    %% Backend Layer
    subgraph "Backend (Express Clip Service - port 3001)"
        direction TB
        Clip_Service["Clip Service<br/>(index.ts)"]:::backend
        Video_Processor["Video Processor<br/>(yt-dlp + ffmpeg)"]:::backend
        Temp_Storage["Temp Storage<br/>(uploads or streams)"]:::backend
    end

    %% Database
    Database["Database<br/>(Postgres/SQLite)"]:::db

    %% External Tools
    subgraph "External Tools"
        direction TB
        YTDLP["yt-dlp CLI"]:::external
        FFMPEG["ffmpeg CLI"]:::external
    end

    %% Connections
    Browser -->|"HTTPS Requests"| UI_Pages
    UI_Pages -->|"calls Auth API"| Auth_API
    UI_Pages -->|"POST /clip JSON"| Clip_Service
    Auth_API -->|"SQL Queries"| Database
    DB_Layer -->|"ORM Queries"| Database
    Clip_Service -->|"spawn process"| Video_Processor
    Video_Processor -->|"invoke"| YTDLP
    Video_Processor -->|"invoke"| FFMPEG
    Clip_Service -->|"binary stream"| Browser
    Clip_Service -->|"writes temp files"| Temp_Storage

    %% Click Events - Frontend
    click UI_Pages "https://github.com/retrogtx/youtube-clipper/tree/master/frontend/app"
    click Auth_API "https://github.com/retrogtx/youtube-clipper/blob/master/frontend/app/api/auth/[...all]/route.ts"
    click UI_Components "https://github.com/retrogtx/youtube-clipper/tree/master/frontend/components/ui"
    click Styling "https://github.com/retrogtx/youtube-clipper/blob/master/frontend/app/globals.css"
    click Styling "https://github.com/retrogtx/youtube-clipper/blob/master/frontend/app/layout.tsx"
    click DB_Layer "https://github.com/retrogtx/youtube-clipper/blob/master/frontend/drizzle.config.ts"
    click DB_Layer "https://github.com/retrogtx/youtube-clipper/tree/master/frontend/drizzle"
    click DB_Layer "https://github.com/retrogtx/youtube-clipper/blob/master/frontend/lib/db.ts"
    click DB_Layer "https://github.com/retrogtx/youtube-clipper/blob/master/frontend/lib/schema.ts"
    click Auth_Helpers "https://github.com/retrogtx/youtube-clipper/blob/master/frontend/lib/auth-client.ts"
    click Auth_Helpers "https://github.com/retrogtx/youtube-clipper/blob/master/frontend/lib/auth.ts"
    click Utils "https://github.com/retrogtx/youtube-clipper/blob/master/frontend/lib/utils.ts"
    click Configs "https://github.com/retrogtx/youtube-clipper/blob/master/next.config.ts"
    click Configs "https://github.com/retrogtx/youtube-clipper/blob/master/tsconfig.json"
    click Configs "https://github.com/retrogtx/youtube-clipper/blob/master/postcss.config.mjs"
    click Configs "https://github.com/retrogtx/youtube-clipper/blob/master/eslint.config.mjs"
    click Configs "https://github.com/retrogtx/youtube-clipper/blob/master/package.json"
    click Configs "https://github.com/retrogtx/youtube-clipper/blob/master/bun.lock"

    %% Click Events - Backend
    click Clip_Service "https://github.com/retrogtx/youtube-clipper/blob/master/backend/src/index.ts"
    click Clip_Service "https://github.com/retrogtx/youtube-clipper/blob/master/backend/package.json"
    click Clip_Service "https://github.com/retrogtx/youtube-clipper/blob/master/backend/tsconfig.json"
    click Clip_Service "https://github.com/retrogtx/youtube-clipper/blob/master/backend/bun.lock"

    %% Styles
    classDef frontend fill:#CCE8FF,stroke:#3399FF,color:#000
    classDef backend fill:#CCFFCC,stroke:#33AA33,color:#000
    classDef db fill:#E5CCFF,stroke:#7755CC,color:#000
    classDef external fill:#E0E0E0,stroke:#999999,color:#000
```
