# foamzou/bilibili-downloader architecture

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

- Interactive diagram: https://gitdiagram.com/foamzou/bilibili-downloader
- Repository: https://github.com/foamzou/bilibili-downloader

## Overview

1. **Project shape**
   - This repo is a two-part system: a browser userscript (`index.user.js`) plus a local Node.js backend (`server.js`).
   - The userscript runs on Bilibili video pages, while the backend runs on the user’s machine, usually via Docker (`Dockerfile`, `docker-compose.yml`).

2. **Frontend boundary: Tampermonkey userscript**
   - `index.user.js` is the only browser-side component.
   - It injects UI into Bilibili pages, reads page/video metadata, and drives the user workflow: open download panel, choose options, start jobs, and show progress.
   - It also provides the “copy command” path for advanced users who want the raw `curl` + `ffmpeg` flow.

3. **Backend boundary: local HTTP service**
   - `server.js` exposes a local service on port `5577` as described in the README.
   - The frontend talks to this service over HTTP on `localhost` by default, with the backend address configurable in the script UI.
   - This is the key trust boundary: browser UI stays in the page, while all heavy work happens in the local service.

4. **Download/processing pipeline**
   - The backend accepts a download request, places it into an asynchronous task queue, and processes it in the background.
   - The work is split into stages visible to the UI: downloading, merging, and completion.
   - Video jobs use `ffmpeg` for media handling/merging; downloading uses `curl` according to the README.

5. **Data flow from page to file**
   - The userscript captures Bilibili video identity and user-selected options such as audio/video choice, time range, output naming, and subdirectory.
   - Those parameters are sent to the backend, which performs the actual network fetch and writes files into the mounted download directory.
   - Output is stored locally, not in a remote service, which is central to the repo’s architecture.

6. **State and feedback loop**
   - Progress is not just fire-and-forget: the frontend repeatedly polls or otherwise checks job status from the backend and updates the UI in real time.
   - The UI also performs a file-existence check before starting a download to avoid duplicates, which means the backend likely exposes filesystem-aware status queries.
   - The recent download history shown in the UI appears to be a client-facing feature, likely managed by the script rather than a database.

7. **Storage model**
   - There is no obvious database layer in the repo.
   - The persistent data model is the filesystem: downloaded media files under a local `downloaded` directory, optionally organized into user-defined subfolders and filenames.
   - Docker Compose is intended to mount that directory from the host into the container, preserving files outside the container lifecycle.

8. **Configuration and customization**
   - The system is highly parameterized from the userscript: backend URL, filename template, storage subdirectory, and trimming start/end times.
   - Filename placeholders like `{title}`, `{vid}`, `{year}`, `{month}`, and `{day}` are part of the app’s file-path generation contract between frontend intent and backend execution.
   - This makes naming and organization part of the architecture, not just presentation.

9. **Deployment topology**
   - `docker-compose.yml` is the recommended deployment path and likely wires together the Node service, filesystem mount, and container runtime settings.
   - `Dockerfile` packages the backend environment so the user does not need to manually install Node, `ffmpeg`, or other native tooling.
   - A non-Docker path is also supported: install dependencies with `npm install` and run `node server.js` directly.

10. **External dependencies and runtime assumptions**
   - The backend depends on Node.js plus system tools `ffmpeg` and `curl`; those are the operational dependencies that materially shape the architecture.
   - The frontend depends on a Tampermonkey-compatible browser extension and Bilibili’s page structure.
   - No cloud service is required in the current design; the repo is explicitly organized around local, user-owned execution.

11. **Operational characteristics**
   - The service is meant to stay running in the background while the browser UI comes and goes.
   - Since downloads and transcoding are local, throughput and bandwidth are bounded by the user’s machine and network, not a shared server.
   - The architecture therefore optimizes for privacy, control, and stability rather than multi-user scalability.

12. **What to draw in an architecture diagram**
   - Draw three major zones: Bilibili web page + Tampermonkey UI, local Node backend, and local filesystem/downloaded output.
   - Show HTTP requests from userscript to backend on `localhost:5577`.
   - Show backend invoking `curl`/`ffmpeg`, then writing to the mounted `downloaded` directory.
   - Optionally annotate Docker as the packaging/runtime layer around the backend, not a separate application tier.

Main entry points: [`index.user.js`](https://github.com/foamzou/bilibili-downloader/blob/main/index.user.js) (Download UI), [`server.js`](https://github.com/foamzou/bilibili-downloader/blob/main/server.js) (Local API), [`docker-compose.yml`](https://github.com/foamzou/bilibili-downloader/blob/main/docker-compose.yml) (Compose), [`Dockerfile`](https://github.com/foamzou/bilibili-downloader/tree/main/Dockerfile) (Docker image)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_browser["Browser"]
  node_bilibili_page["Bilibili page<br/>web page"]
  node_userscript_ui["Download UI<br/>userscript UI<br/>[index.user.js]"]
  node_command_path["Copy command<br/>userscript flow<br/>[index.user.js]"]
end

subgraph group_backend["Local backend"]
  node_local_api["Local API<br/>HTTP service<br/>[server.js]"]
  node_job_queue["Task queue<br/>async queue<br/>[server.js]"]
  node_status_loop["Status loop<br/>job state<br/>[server.js]"]
  node_download_worker["Downloader<br/>fetch worker<br/>[server.js]"]
  node_merge_worker["Merger<br/>media merge<br/>[server.js]"]
  node_file_writer["File writer<br/>output writer<br/>[server.js]"]
  node_curl_tool{{"curl<br/>cli tool"}}
  node_ffmpeg_tool{{"ffmpeg<br/>media tool"}}
end

subgraph group_storage["Local storage"]
  node_download_dir[("Downloads<br/>filesystem")]
end

subgraph group_deploy["Deployment"]
  node_docker_compose["Compose<br/>deployment<br/>[docker-compose.yml]"]
  node_dockerfile["Docker image<br/>container build"]
  node_package_manifest["Package config<br/>node manifest<br/>[package.json]"]
end

node_bilibili_page -->|"page data"| node_userscript_ui
node_userscript_ui -->|"HTTP request"| node_local_api
node_command_path -.->|"raw job"| node_local_api
node_local_api -->|"enqueue"| node_job_queue
node_job_queue -->|"job state"| node_status_loop
node_status_loop -->|"poll/update"| node_userscript_ui
node_job_queue -->|"start"| node_download_worker
node_download_worker -->|"invoke"| node_curl_tool
node_download_worker -->|"handoff"| node_merge_worker
node_merge_worker -->|"invoke"| node_ffmpeg_tool
node_merge_worker -->|"write output"| node_file_writer
node_file_writer -->|"store"| node_download_dir
node_userscript_ui -.->|"exists check"| node_download_dir
node_docker_compose -->|"build/run"| node_dockerfile
node_docker_compose -->|"mount"| node_download_dir
node_dockerfile -->|"deps"| node_package_manifest
node_package_manifest -.->|"start service"| node_local_api

click node_userscript_ui "https://github.com/foamzou/bilibili-downloader/blob/main/index.user.js"
click node_command_path "https://github.com/foamzou/bilibili-downloader/blob/main/index.user.js"
click node_local_api "https://github.com/foamzou/bilibili-downloader/blob/main/server.js"
click node_job_queue "https://github.com/foamzou/bilibili-downloader/blob/main/server.js"
click node_status_loop "https://github.com/foamzou/bilibili-downloader/blob/main/server.js"
click node_download_worker "https://github.com/foamzou/bilibili-downloader/blob/main/server.js"
click node_merge_worker "https://github.com/foamzou/bilibili-downloader/blob/main/server.js"
click node_file_writer "https://github.com/foamzou/bilibili-downloader/blob/main/server.js"
click node_docker_compose "https://github.com/foamzou/bilibili-downloader/blob/main/docker-compose.yml"
click node_dockerfile "https://github.com/foamzou/bilibili-downloader/tree/main/Dockerfile"
click node_package_manifest "https://github.com/foamzou/bilibili-downloader/blob/main/package.json"

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_bilibili_page,node_userscript_ui,node_command_path toneBlue
class node_local_api,node_job_queue,node_status_loop,node_download_worker,node_merge_worker,node_file_writer,node_curl_tool,node_ffmpeg_tool toneAmber
class node_download_dir toneMint
class node_docker_compose,node_dockerfile,node_package_manifest toneRose
```

## Components

### Browser

Bilibili page and Tampermonkey-side workflow

- **Bilibili page**: web page · Video page where the script runs and reads metadata
- [Download UI](https://github.com/foamzou/bilibili-downloader/blob/main/index.user.js): userscript UI · Injects controls, gathers options, and starts jobs · `index.user.js`
- [Copy command](https://github.com/foamzou/bilibili-downloader/blob/main/index.user.js): userscript flow · Exports the raw curl and ffmpeg workflow for advanced users · `index.user.js`

### Local backend

Node service that accepts jobs and processes media

- [Local API](https://github.com/foamzou/bilibili-downloader/blob/main/server.js): HTTP service · Receives download requests on localhost:5577 · `server.js`
- [Task queue](https://github.com/foamzou/bilibili-downloader/blob/main/server.js): async queue · Buffers and schedules download jobs in the background · `server.js`
- [Status loop](https://github.com/foamzou/bilibili-downloader/blob/main/server.js): job state · Tracks progress for polling and UI updates · `server.js`
- [Downloader](https://github.com/foamzou/bilibili-downloader/blob/main/server.js): fetch worker · Fetches media segments or streams with curl · `server.js`
- [Merger](https://github.com/foamzou/bilibili-downloader/blob/main/server.js): media merge · Combines media outputs with ffmpeg · `server.js`
- [File writer](https://github.com/foamzou/bilibili-downloader/blob/main/server.js): output writer · Writes finished media into the target directory · `server.js`
- **curl**: cli tool · Backend download transport used by the pipeline
- **ffmpeg**: media tool · Backend media processing and merging tool

### Local storage

Host-mounted download output

- **Downloads**: filesystem · Mounted local directory that stores completed files

### Deployment

Packaging and local runtime setup

- [Compose](https://github.com/foamzou/bilibili-downloader/blob/main/docker-compose.yml): deployment · Recommended container orchestration for the backend · `docker-compose.yml`
- [Docker image](https://github.com/foamzou/bilibili-downloader/tree/main/Dockerfile): container build · Packages Node and native tools into a runnable backend image · `Dockerfile`
- [Package config](https://github.com/foamzou/bilibili-downloader/blob/main/package.json): node manifest · Declares Node dependencies and scripts · `package.json`

## Connections

- Bilibili page → Download UI: page data · The script reads video metadata and injects controls into the page
- Download UI → Local API: HTTP request · Sends user-selected download parameters to the local backend
- Copy command → Local API: raw job · Advanced users can generate a command-style download flow for the backend
- Local API → Task queue: enqueue · Accepted requests are queued for asynchronous processing
- Task queue → Status loop: job state · Queued work updates progress and completion state
- Status loop → Download UI: poll/update · The UI refreshes progress and results from backend status checks
- Task queue → Downloader: start · Downloader stage begins after a job is scheduled
- Downloader → curl: invoke · Network fetches are performed through curl
- Downloader → Merger: handoff · Downloaded media is passed to the merge stage when needed
- Merger → ffmpeg: invoke · Media assembly and trimming use ffmpeg
- Merger → File writer: write output · Finished media is written to the output path
- File writer → Downloads: store · Completed downloads land in the host-mounted directory
- Download UI → Downloads: exists check · The UI checks for existing files before starting a new job
- Compose → Docker image: build/run · Compose uses the image definition to run the backend container
- Compose → Downloads: mount · Host storage is mounted into the container for persistence
- Docker image → Package config: deps · The container build packages the Node service and its dependencies
- Package config → Local API: start service · Node package metadata supports running the backend service
