How foamzou/bilibili-downloader works
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 Download UI · server.js Local API · docker-compose.yml Compose · Dockerfile Docker image
Components 15
Browser
Bilibili page and Tampermonkey-side workflow
- Bilibili page · Video page where the script runs and reads metadata
- Download UI · Injects controls, gathers options, and starts jobs
index.user.js - Copy command · 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 · Receives download requests on localhost:5577
server.js - Task queue · Buffers and schedules download jobs in the background
server.js - Status loop · Tracks progress for polling and UI updates
server.js - Downloader · Fetches media segments or streams with curl
server.js - Merger · Combines media outputs with ffmpeg
server.js - File writer · Writes finished media into the target directory
server.js - curl · Backend download transport used by the pipeline
- ffmpeg · Backend media processing and merging tool
Local storage
Host-mounted download output
- Downloads · Mounted local directory that stores completed files
Deployment
Packaging and local runtime setup
- Compose · Recommended container orchestration for the backend
docker-compose.yml - Docker image · Packages Node and native tools into a runnable backend image
Dockerfile - Package config · Declares Node dependencies and scripts
package.json
Connections 17
- 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