No description
- -thumb downloads each part's first_frame cover as <name>.jpg - -subs fetches subtitle tracks via x/player/v2 (requires login cookie) and converts bilibili JSON subtitles to .srt - fixes use-after-free: SRT was written after the parsed tree was freed - backup-CDN fallback proven in the field (Akamai 403 -> COS succeeded) - ignore local test dirs and the work-engsub scratch folder |
||
|---|---|---|
| .gitignore | ||
| bilibili.c | ||
| Makefile | ||
| README.md | ||
bilibili-downloader (C)
Single-file C program that downloads a Bilibili video from a share link. Only dependency: libcurl. No other libraries, no JSON package, no build tools beyond a C compiler.
Requirements
- A C compiler (
gccorcc) - libcurl development files:
- Debian/Ubuntu:
sudo apt install libcurl4-openssl-dev - Fedora:
sudo dnf install libcurl-devel - Arch:
sudo pacman -S libcurl - macOS:
brew install curl(usually already present)
- Debian/Ubuntu:
ffmpegon PATH (recommended) — used to combine the video and audio streams into one.mp4. Without it the tool keeps the separate.video.m4s/.audio.m4sfiles and prints the combine command.
Build
make
# or directly:
gcc -O2 -Wall -Wextra bilibili.c -o bilibili $(pkg-config --cflags --libs libcurl)
Takes a few seconds.
Usage
./bilibili "<bilibili share link or BV id>" [output-dir] [-c cookies.txt] [-thumb] [-subs]
Example:
./bilibili "https://www.bilibili.com/video/BV1K9yazaE1H/?share_source=copy_web&vd_source=ab5a" downloads/ -c ~/Downloads/cookies.txt -thumb -subs
A bare BV id also works: ./bilibili BV1K9yazaE1H
| Flag | Effect |
|---|---|
-c FILE |
Use a Netscape cookie jar (logged-in SESSDATA unlocks 1080p/4K) |
-thumb |
Also download the cover thumbnail as <name>.jpg (per part) |
-subs |
Also download subtitles as <name>.<lang>.srt if the video has any |
Getting 1080p / 4K
Anonymous requests are capped at 480p. To unlock the highest quality (720p/1080p/4K), log in on bilibili.com and export your cookies — either:
- a Netscape-format cookie jar file (most browser cookie-export
extensions produce this; it contains a
# Netscape HTTP Cookie Fileheader) passed with-c, or - the env var form:
export BILIBILI_COOKIE="SESSDATA=...; bili_jct=..."
./bilibili "<url>"
The jar only needs SESSDATA (plus bili_jct); a full browser export is
fine.
Notes
- Multi-part videos (P1, P2, ...) are downloaded one part at a time, each
into its own
.mp4namedTitle - P# - part.mp4. - The tool always picks the highest-bandwidth stream available for the account/cookie you provide.