bilibili-video-helper
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseBilibili Video Helper
Bilibili Video Helper
本技能通过 B 站公开 API 实现视频搜索、信息挖掘、字幕/评论/弹幕提取。
This skill implements video search, information mining, and subtitle/comment/barrage extraction through Bilibili's public APIs.
API Cookie 需求一览
API Cookie Requirements Overview
| 功能 | API | 需要 Cookie |
|---|---|---|
| 搜索视频 | | ❌ |
| 视频详情 | | ❌ |
| 评论列表 | | ❌ |
| 字幕列表 | | ✅ |
| 弹幕 | | ✅ |
说明:搜索、视频详情、评论这三个功能不需要 Cookie,可以直接调用。字幕和弹幕需要 Cookie。
| Function | API | Cookie Required |
|---|---|---|
| Video Search | | ❌ |
| Video Details | | ❌ |
| Comment List | | ❌ |
| Subtitle List | | ✅ |
| Barrage | | ✅ |
Note: The three functions of search, video details, and comments do not require a Cookie and can be called directly. Subtitles and barrage require a Cookie.
前置准备:Cookie(仅字幕和弹幕需要)
Prerequisites: Cookie (Only required for subtitles and barrage)
如果需要获取字幕或弹幕,引导用户提供 Cookie:
请登录电脑浏览器 B 站,按 F12 打开开发者工具,切换到 网络 (Network) 标签,刷新页面后点击任意请求,在右侧 请求标头 (Request Headers) 中复制 cookie: ... 整段内容发给我。保存为环境变量:
bash
COOKIE='用户提供的完整cookie内容'If you need to obtain subtitles or barrage, guide users to provide a Cookie:
Please log in to Bilibili on your desktop browser, press F12 to open Developer Tools, switch to the Network tab, refresh the page and click any request, then copy the entire cookie: ... content from the Request Headers on the right and send it to me.Save as an environment variable:
bash
COOKIE='The complete cookie content provided by the user'核心工作流
Core Workflow
1. 搜索视频(无需 Cookie)
1. Video Search (No Cookie Required)
API:
GET /x/web-interface/search/typebash
curl -s "https://api.bilibili.com/x/web-interface/search/type?keyword=AI&page=1&page_size=10&search_type=video" | \
jq '.data.result[] | {title: .title, author: .author, play: .play, duration: .duration, bvid: .bvid}'参数说明:
- : 搜索关键词
keyword - : 页码(从 1 开始)
page - : 每页数量(最大 50)
page_size - : 搜索类型(视频)
search_type=video
返回字段:
- : 标题(含
title高亮标签)<em class="keyword"> - : UP 主
author - : 播放量
play - : 时长(如 "13:28")
duration - : 视频 BV 号
bvid
API:
GET /x/web-interface/search/typebash
curl -s "https://api.bilibili.com/x/web-interface/search/type?keyword=AI&page=1&page_size=10&search_type=video" | \
jq '.data.result[] | {title: .title, author: .author, play: .play, duration: .duration, bvid: .bvid}'Parameter Explanation:
- : Search keyword
keyword - : Page number (starts from 1)
page - : Number of items per page (max 50)
page_size - : Search type (video)
search_type=video
Return Fields:
- : Title (contains
titlehighlight tags)<em class="keyword"> - : Uploader
author - : View count
play - : Duration (e.g., "13:28")
duration - : Video BVID
bvid
2. 获取视频详情(无需 Cookie)
2. Get Video Details (No Cookie Required)
API:
GET /x/web-interface/viewbash
BVID="BV1hNFdz4EZp"
curl -s "https://api.bilibili.com/x/web-interface/view?bvid=$BVID" | \
jq '.data | {title, bvid, aid, cid, owner: .owner.name, desc, duration, stat}'返回字段:
| 字段 | 说明 |
|---|---|
| 标题 |
| BV 号 |
| AV 号 |
| 视频分 P 的 CID |
| UP 主 |
| 简介 |
| 时长(秒) |
| 播放量 |
| 点赞数 |
| 投币数 |
| 收藏数 |
| 分享数 |
| 弹幕数 |
| 评论数 |
注意:以上所有元数据(播放量、点赞、投币、收藏、分享、弹幕、评论)均无需 Cookie 即可获取。
API:
GET /x/web-interface/viewbash
BVID="BV1hNFdz4EZp"
curl -s "https://api.bilibili.com/x/web-interface/view?bvid=$BVID" | \
jq '.data | {title, bvid, aid, cid, owner: .owner.name, desc, duration, stat}'Return Fields:
| Field | Explanation |
|---|---|
| Title |
| BVID |
| AID |
| CID of the video segment |
| Uploader |
| Description |
| Duration (seconds) |
| View count |
| Like count |
| Coin count |
| Favorite count |
| Share count |
| Barrage count |
| Comment count |
Note: All the above metadata (view count, like, coin, favorite, share, barrage, comment) can be obtained without a Cookie.
3. 获取评论(无需 Cookie)
3. Get Comments (No Cookie Required)
API:
GET /x/v2/replyAPI:
GET /x/v2/reply步骤 1: 获取 AID
Step 1: Get AID
bash
AID=$(curl -s "https://api.bilibili.com/x/web-interface/view?bvid=$BVID" | jq -r '.data.aid')bash
AID=$(curl -s "https://api.bilibili.com/x/web-interface/view?bvid=$BVID" | jq -r '.data.aid')步骤 2: 获取评论列表
Step 2: Get Comment List
bash
curl -s "https://api.bilibili.com/x/v2/reply?type=1&oid=$AID&sort=1&ps=30" | \
jq '.data.page, (.data.replies | length)'提取热门评论:
bash
curl -s "https://api.bilibili.com/x/v2/reply?type=1&oid=$AID&sort=1&ps=20" | \
jq -r '.data.replies | sort_by(-.like) | .[] | "【\(.member.uname)】\(.content.message[:100])... [👍\(.like)]"'参数说明:
- : 视频类型
type=1 - : 视频 AID
oid - : 按时间排序;
sort=0: 按热度排序;sort=1: 按回复数排序sort=2 - : 每页数量(最大 49)
ps - : 页码
pn
bash
curl -s "https://api.bilibili.com/x/v2/reply?type=1&oid=$AID&sort=1&ps=30" | \
jq '.data.page, (.data.replies | length)'Extract Popular Comments:
bash
curl -s "https://api.bilibili.com/x/v2/reply?type=1&oid=$AID&sort=1&ps=20" | \
jq -r '.data.replies | sort_by(-.like) | .[] | "【\(.member.uname)】\(.content.message[:100])... [👍\(.like)]"'Parameter Explanation:
- : Video type
type=1 - : Video AID
oid - : Sort by time;
sort=0: Sort by popularity;sort=1: Sort by reply countsort=2 - : Number of items per page (max 49)
ps - : Page number
pn
4. 获取字幕(需要 Cookie)
4. Get Subtitles (Cookie Required)
API:
GET /x/player/v2API:
GET /x/player/v2步骤 1: 获取 CID
Step 1: Get CID
bash
CID=$(curl -s "https://api.bilibili.com/x/web-interface/view?bvid=$BVID" | jq -r '.data.cid')bash
CID=$(curl -s "https://api.bilibili.com/x/web-interface/view?bvid=$BVID" | jq -r '.data.cid')步骤 2: 获取字幕列表
Step 2: Get Subtitle List
bash
curl -s "https://api.bilibili.com/x/player/v2?bvid=$BVID&cid=$CID" \
-H "Cookie: $COOKIE" \
-H "Referer: https://www.bilibili.com/video/$BVID" | \
jq '.data.subtitle.subtitles[] | {lan: .lan, lan_doc: .lan_doc, url: .subtitle_url}'返回示例:
json
{"lan": "ai-zh", "lan_doc": "中文", "url": "//aisubtitle.hdslb.com/..."}
{"lan": "ai-en", "lan_doc": "English", "url": "//aisubtitle.hdslb.com/..."}bash
curl -s "https://api.bilibili.com/x/player/v2?bvid=$BVID&cid=$CID" \
-H "Cookie: $COOKIE" \
-H "Referer: https://www.bilibili.com/video/$BVID" | \
jq '.data.subtitle.subtitles[] | {lan: .lan, lan_doc: .lan_doc, url: .subtitle_url}'Return Example:
json
{"lan": "ai-zh", "lan_doc": "中文", "url": "//aisubtitle.hdslb.com/..."}
{"lan": "ai-en", "lan_doc": "English", "url": "//aisubtitle.hdslb.com/..."}步骤 3: 下载字幕内容(无需 Cookie)
Step 3: Download Subtitle Content (No Cookie Required)
bash
SUBTITLE_URL="https://aisubtitle.hdslb.com/bfs/ai_subtitle/prod/xxx"
curl -s "$SUBTITLE_URL" | jq -r '.body[].content'字幕语言代码:
- : 中文(AI 生成)
ai-zh - : English(AI 生成)
ai-en - : 日本語(AI 生成)
ai-ja - : Español(AI 生成)
ai-es - : Português(AI 生成)
ai-pt - : العربية(AI 生成)
ai-ar
提示:B 站会自动为视频生成 AI 字幕,即使 UP 主没有上传字幕,也可能有 AI 生成的字幕可用。
bash
SUBTITLE_URL="https://aisubtitle.hdslb.com/bfs/ai_subtitle/prod/xxx"
curl -s "$SUBTITLE_URL" | jq -r '.body[].content'Subtitle Language Codes:
- : Chinese (AI-generated)
ai-zh - : English (AI-generated)
ai-en - : Japanese (AI-generated)
ai-ja - : Spanish (AI-generated)
ai-es - : Portuguese (AI-generated)
ai-pt - : Arabic (AI-generated)
ai-ar
Tip: Bilibili automatically generates AI subtitles for videos. Even if the uploader hasn't uploaded subtitles, AI-generated subtitles may be available.
5. 获取弹幕(需要 Cookie)
5. Get Barrage (Cookie Required)
弹幕 API 返回 protobuf 格式,解析复杂。推荐使用 :
yt-dlpbash
yt-dlp --add-header "Cookie:$COOKIE" --write-sub --sub-lang danmaku --skip-download -o "/tmp/%(id)s.%(ext)s" "https://www.bilibili.com/video/$BVID"弹幕保存为 XML 格式,提取纯文本:
bash
cat /tmp/${BVID}.danmaku.xml | sed 's/<d /\n<d /g' | grep -oP '>[^<]+<' | tr -d '<>' | grep -v '^$'The barrage API returns data in protobuf format, which is complex to parse. It is recommended to use :
yt-dlpbash
yt-dlp --add-header "Cookie:$COOKIE" --write-sub --sub-lang danmaku --skip-download -o "/tmp/%(id)s.%(ext)s" "https://www.bilibili.com/video/$BVID"Barrage is saved in XML format. Extract plain text:
bash
cat /tmp/${BVID}.danmaku.xml | sed 's/<d /\n<d /g' | grep -oP '>[^<]+<' | tr -d '<>' | grep -v '^$'访问受限处理 (412 Precondition Failed)
Access Restriction Handling (412 Precondition Failed)
如果遇到 B 站 412 错误,说明当前环境被风控。需要用户重新获取 Cookie。
If you encounter a Bilibili 412 error, it means the current environment is under risk control. You need to ask the user to re-obtain the Cookie.
6. 获取 UP 主视频列表(无需 Cookie)
6. Get Uploader's Video List (No Cookie Required)
API:
GET /x/space/wbi/arc/searchbash
UID="12345678"
curl -s "https://api.bilibili.com/x/space/arc/search?mid=$UID&ps=30&pn=1&order=pubdate" \
-H "User-Agent: Mozilla/5.0" | \
jq '.data.list.vlist[] | {bvid: .bvid, title: .title, play: .play, created: .created, length: .length}'参数说明:
- : UP 主 UID
mid - : 每页数量(最大 50)
ps - : 页码(从 1 开始)
pn - : 排序方式,
order(最新)或pubdate(最多播放)click
批量翻页示例:
bash
for pn in 1 2 3; do
curl -s "https://api.bilibili.com/x/space/arc/search?mid=$UID&ps=30&pn=$pn&order=pubdate" \
-H "User-Agent: Mozilla/5.0" | \
jq -r '.data.list.vlist[].bvid'
sleep 1.5
doneAPI:
GET /x/space/wbi/arc/searchbash
UID="12345678"
curl -s "https://api.bilibili.com/x/space/arc/search?mid=$UID&ps=30&pn=1&order=pubdate" \
-H "User-Agent: Mozilla/5.0" | \
jq '.data.list.vlist[] | {bvid: .bvid, title: .title, play: .play, created: .created, length: .length}'Parameter Explanation:
- : Uploader's UID
mid - : Number of items per page (max 50)
ps - : Page number (starts from 1)
pn - : Sorting method,
order(latest) orpubdate(most views)click
Batch Pagination Example:
bash
for pn in 1 2 3; do
curl -s "https://api.bilibili.com/x/space/arc/search?mid=$UID&ps=30&pn=$pn&order=pubdate" \
-H "User-Agent: Mozilla/5.0" | \
jq -r '.data.list.vlist[].bvid'
sleep 1.5
done7. 无字幕时 ASR 兜底转录(需要 Cookie + SiliconFlow API Key)
7. ASR Fallback Transcription When No Subtitles Are Available (Requires Cookie + SiliconFlow API Key)
当视频没有 AI 字幕时,可下载音频后调用 ASR 获取文本。
When there are no AI subtitles for a video, you can download the audio and call ASR to get the text.
步骤 1: 用 yt-dlp 下载音频
Step 1: Download Audio with yt-dlp
bash
BVID="BV1xx411c7mD"
yt-dlp --add-header "Cookie:$COOKIE" \
-f bestaudio \
--extract-audio --audio-format mp3 \
-o "/tmp/%(id)s.%(ext)s" \
"https://www.bilibili.com/video/$BVID"bash
BVID="BV1xx411c7mD"
yt-dlp --add-header "Cookie:$COOKIE" \
-f bestaudio \
--extract-audio --audio-format mp3 \
-o "/tmp/%(id)s.%(ext)s" \
"https://www.bilibili.com/video/$BVID"步骤 2: 调用 SiliconFlow ASR 转录
Step 2: Call SiliconFlow ASR for Transcription
bash
AUDIO_FILE="/tmp/${BVID}.mp3"
SILICONFLOW_API_KEY="your_api_key"
curl -s "https://api.siliconflow.cn/v1/audio/transcriptions" \
-H "Authorization: Bearer $SILICONFLOW_API_KEY" \
-F "file=@${AUDIO_FILE};type=audio/mpeg" \
-F "model=FunAudioLLM/SenseVoiceSmall" | \
jq -r '.text'决策优先级:
- 优先尝试获取 B 站 AI 字幕(免费、快)
- 字幕不存在时,才走 ASR 兜底(消耗 API 额度)
bash
AUDIO_FILE="/tmp/${BVID}.mp3"
SILICONFLOW_API_KEY="your_api_key"
curl -s "https://api.siliconflow.cn/v1/audio/transcriptions" \
-H "Authorization: Bearer $SILICONFLOW_API_KEY" \
-F "file=@${AUDIO_FILE};type=audio/mpeg" \
-F "model=FunAudioLLM/SenseVoiceSmall" | \
jq -r '.text'Decision Priority:
1.优先尝试获取 B 站 AI 字幕(免费、快)
2.字幕不存在时,才走 ASR 兜底(消耗 API 额度)
8. 可选数据持久化
8. Optional Data Persistence
提取到的内容(字幕/评论/元数据)由 AI 判断用户是否需要存储。表结构由用户自行维护,此处仅提供参考模板。
The extracted content (subtitles/comments/metadata) is determined by AI whether the user needs to store it. The table structure is maintained by the user, and only a reference template is provided here.
参考表结构
Reference Table Structure
sql
CREATE TABLE IF NOT EXISTS bili_videos (
bvid VARCHAR(20) PRIMARY KEY,
title TEXT,
up_name VARCHAR(255),
up_mid BIGINT,
duration INTEGER, -- 秒
view_count INTEGER,
like_count INTEGER,
coin_count INTEGER,
pub_time TIMESTAMP,
content TEXT, -- 字幕或 ASR 转录文本
source VARCHAR(20), -- 'subtitle' 或 'asr'
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);sql
CREATE TABLE IF NOT EXISTS bili_videos (
bvid VARCHAR(20) PRIMARY KEY,
title TEXT,
up_name VARCHAR(255),
up_mid BIGINT,
duration INTEGER, -- seconds
view_count INTEGER,
like_count INTEGER,
coin_count INTEGER,
pub_time TIMESTAMP,
content TEXT, -- Subtitle or ASR transcribed text
source VARCHAR(20), -- 'subtitle' or 'asr'
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);存储示例
Storage Example
bash
psql $DATABASE_URL -c "
INSERT INTO bili_videos (bvid, title, up_name, up_mid, pub_time, content, source)
VALUES ('BV1xx411c7mD', '视频标题', 'UP主名', 12345678, '2024-01-01', '字幕文本...', 'subtitle')
ON CONFLICT (bvid) DO UPDATE SET content = EXCLUDED.content;
"存储决策原则:
- 用户明确说"保存"、"存库"、"记录下来"时才执行写入
- 默认只展示内容,不自动写库
- 数据库连接串从用户环境变量 读取
DATABASE_URL
bash
psql $DATABASE_URL -c "
INSERT INTO bili_videos (bvid, title, up_name, up_mid, pub_time, content, source)
VALUES ('BV1xx411c7mD', 'Video Title', 'Uploader Name', 12345678, '2024-01-01', 'Subtitle text...', 'subtitle')
ON CONFLICT (bvid) DO UPDATE SET content = EXCLUDED.content;
"Storage Decision Principles:
- Only perform writing when the user explicitly says "save", "store in database", or "record it"
- By default, only display content and do not automatically write to the database
- The database connection string is read from the user's environment variable
DATABASE_URL
访问受限处理 (412 Precondition Failed)
Access Restriction Handling (412 Precondition Failed)
如果遇到 B 站 412 错误,说明当前环境被风控。需要用户重新获取 Cookie。
If you encounter a Bilibili 412 error, it means the current environment is under risk control. You need to ask the user to re-obtain the Cookie.
注意事项
Notes
- 默认不下载视频:仅提取信息。
- 依赖检查:确保环境已安装 和
jq。yt-dlp - Cookie 需求:只有字幕、弹幕、ASR 音频下载需要 Cookie,搜索、视频详情、评论均无需 Cookie。
- API 限制:搜索 API 有频率限制,避免短时间内大量请求;批量翻页时每页间隔 1.5s 以上。
- 字幕优先:B 站会自动生成 AI 字幕,优先检查是否有可用字幕,无字幕再走 ASR。
- 存储解耦:持久化是可选动作,不与任何固定表结构绑定,用户自己管理数据库。
- No video download by default: Only extract information.
- Dependency Check: Ensure and
jqare installed in the environment.yt-dlp - Cookie Requirements: Only subtitles, barrage, and ASR audio download require a Cookie. Search, video details, and comments do not require a Cookie.
- API Limits: The search API has frequency limits. Avoid a large number of requests in a short time; interval of more than 1.5s between pages when batch paginating.
- Subtitle Priority: Bilibili automatically generates AI subtitles. Prioritize checking for available subtitles, and use ASR as a fallback only when no subtitles exist.
- Storage Decoupling: Persistence is an optional action, not bound to any fixed table structure. Users manage their own databases.