B2A (Bilibili to Agents) — 让 AI Agent 能「观看」B站视频,现已原生支持 MCP 协议
Project description
B2A (Bilibili to Agents)
让 AI Agent 能「观看」B站视频的多模态内容提取工具。
解决什么问题
AI Agent(如 Claude Code、Cursor 等)可以访问网页,但无法真正「观看」视频。当用户想让 Agent 分析某个 B 站视频的内容时,Agent 无从下手。
B2A 提供了三条互补的信息获取轨道:
- CC 字幕 — 最快速,直接拉取视频自带的字幕文本(含时间戳)
- ASR 语音识别(声) — 通过豆包语音大模型将音频转为带时间戳的文字,解决 B 站大量视频无 CC 字幕的问题
- 视觉抽帧(形) — 下载视频并按间隔截取关键帧图片,让 Agent 能「看到」画面内容
三条轨道可任意组合,支持全视频或指定时间片段。
安装
1. 安装 ffmpeg(前置依赖)
--visual 抽帧和 --asr 切片功能依赖 ffmpeg,版本需 5.0 以上。
Windows:
# 方式一:winget(Windows 11 自带,推荐)
winget install Gyan.FFmpeg
# 方式二:scoop
scoop install ffmpeg
安装后打开新的终端窗口,验证:
ffmpeg -version
# 应显示 ffmpeg version 5.x 或更高
如果提示找不到命令,需要将 ffmpeg 的 bin 目录加入系统 PATH 环境变量。
macOS:
brew install ffmpeg
Linux(Debian/Ubuntu):
sudo apt update && sudo apt install ffmpeg
2. 安装 B2A
# 克隆仓库
git clone https://github.com/fanzhongli/B2A.git
cd B2A
# 安装
pip install -e .
安装完成后即可使用 b2a 命令。
快速上手
🔌 作为 MCP 插件使用 (v0.4.0+)
B2A 已经原生支持 Model Context Protocol (MCP)!
你可以将 B2A 挂载到主流的 AI IDE 中(如 Cursor, Windsurf, Claude Desktop)。挂载后,你可以直接在聊天框发送 B 站链接,Agent 会自动调用 B2A 读取字幕、甚至调取其自身的视觉能力(Vision)查看截取的关键帧来为你“看”视频。
挂载方法
由于在安装 b2a777 后系统会自动生成 b2a-mcp 命令,你只需要在你的 AI 客户端配置中添加如下选项即可:
🖥️ Claude Desktop / Windsurf
在你的 claude_desktop_config.json 或 mcp_config.json 中配置:
{
"mcpServers": {
"b2a-vision": {
"command": "b2a-mcp",
"args": []
}
}
}
🖱️ Cursor
- 打开 Cursor Settings -> Features -> MCP
- 点击 + Add New MCP Server
- Type 选择
command - Name 填
b2a-vision - Command 填
b2a-mcp(如果是虚拟环境,填入虚拟环境中的绝对路径即可)
🚀 使用示例
"帮我看看这个视频
BV1xx411c7mD第1分30秒到2分钟 画面里写了什么样的代码,顺便总结一下 up 主这段时间说了什么?" (Agent 将会自动调用bilibili_extract_frames和bilibili_extract_voice为你解答!)
基础用法:获取视频信息 + CC 字幕
b2a BV1xx411c7mD
语音识别(ASR)
# 全视频 ASR
b2a BV1xx411c7mD --asr
# 指定片段 ASR(3分20秒到4分50秒)
b2a BV1xx411c7mD --asr --start 03:20 --end 04:50
视觉抽帧
# 全视频抽帧(每10秒一张)
b2a BV1xx411c7mD --visual
# 指定片段抽帧
b2a BV1xx411c7mD --visual --start 01:00 --end 02:30
声形同时获取
b2a BV1xx411c7mD --asr --visual --start 03:20 --end 04:50
分P视频
# 指定第3个分P
b2a BV1xx411c7mD --page 3 --asr
多种输入格式
# 完整链接(自动提取 BV 号、分P、时间跳转)
b2a "https://www.bilibili.com/video/BV1xx411c7mD?p=2&t=180"
# b23.tv 短链
b2a "https://b23.tv/xxxxxx"
# AV 号
b2a av170001
JSON 结构化输出(供 Agent 消费)
b2a BV1xx411c7mD --asr --format json
ASR 配置
ASR 功能依赖豆包语音(火山引擎)API Key。不配置时,CC 字幕和视觉抽帧功能仍可正常使用。
- 复制配置模板:
cp .env.example .env
- 编辑
.env,填入你的 API Key:
VOLC_ENV=test
VOLC_TEST_API_KEY=你的测试环境API_Key
VOLC_PROD_API_KEY=你的生产环境API_Key
CLI 参数一览
b2a <url> [选项]
位置参数:
url B站视频URL、BV号或AV号
选项:
--asr 启用语音识别(提取音频轨并转文字)
--visual 启用视觉提取(抽取视频关键帧截图)
--start TIME 截取开始时间(如 01:30 或 1:03:20)
--end TIME 截取结束时间(如 02:40 或 1:05:00)
--page N 指定分P编号(从1开始)
--format {text,json} 输出格式(默认 text)
项目结构
B2A/
├── src/
│ ├── cli.py # CLI 入口
│ ├── core/
│ │ ├── api.py # B站 API(视频信息、字幕、分P)
│ │ └── asr.py # 火山引擎 ASR(含长音频自动分片)
│ ├── audio/
│ │ └── extractor.py # 音频提取(yt-dlp)
│ ├── visual/
│ │ └── extractor.py # 视觉提取(yt-dlp 下载 + ffmpeg 抽帧)
│ └── utils/
│ ├── config.py # 环境变量与鉴权
│ ├── url_parser.py # URL 解析(短链/BV/AV/分P/时间)
│ └── workspace.py # 工作区目录管理(按视频隔离)
├── tests/ # 测试用例(26个)
├── pyproject.toml # 打包配置
├── requirements.txt # 依赖清单
└── .env.example # 环境变量模板
运行测试
pip install -e ".[dev]"
pytest tests/ -v
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file b2a777-0.4.0.tar.gz.
File metadata
- Download URL: b2a777-0.4.0.tar.gz
- Upload date:
- Size: 21.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
da32085803e7ba7915834e03f09e78fa25162ca087f6bb2569a806401b94e24a
|
|
| MD5 |
c2a07ccdbd61800d4d6333aab2962b4f
|
|
| BLAKE2b-256 |
b66139f7fd8288a938538669e45ac54974080bec2bbbb51ba77b3e7b00652c2c
|
Provenance
The following attestation bundles were made for b2a777-0.4.0.tar.gz:
Publisher:
publish.yml on e8b298/B2A
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
b2a777-0.4.0.tar.gz -
Subject digest:
da32085803e7ba7915834e03f09e78fa25162ca087f6bb2569a806401b94e24a - Sigstore transparency entry: 1013080823
- Sigstore integration time:
-
Permalink:
e8b298/B2A@fcac31ef0e2b9474d4a4c0251a68d7bb56a1b8a8 -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/e8b298
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@fcac31ef0e2b9474d4a4c0251a68d7bb56a1b8a8 -
Trigger Event:
push
-
Statement type:
File details
Details for the file b2a777-0.4.0-py3-none-any.whl.
File metadata
- Download URL: b2a777-0.4.0-py3-none-any.whl
- Upload date:
- Size: 22.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
53e3d34ad1299119f8bba3805ab2377295de11440bbe680e4c7290ff5714b6df
|
|
| MD5 |
15300a3e089180be25b1663aa1b4f1cb
|
|
| BLAKE2b-256 |
02c570dee1e80c0873b9c84bc6c3583b0ea8f152e6b55c56c87a2d6bb5ab8e56
|
Provenance
The following attestation bundles were made for b2a777-0.4.0-py3-none-any.whl:
Publisher:
publish.yml on e8b298/B2A
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
b2a777-0.4.0-py3-none-any.whl -
Subject digest:
53e3d34ad1299119f8bba3805ab2377295de11440bbe680e4c7290ff5714b6df - Sigstore transparency entry: 1013080849
- Sigstore integration time:
-
Permalink:
e8b298/B2A@fcac31ef0e2b9474d4a4c0251a68d7bb56a1b8a8 -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/e8b298
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@fcac31ef0e2b9474d4a4c0251a68d7bb56a1b8a8 -
Trigger Event:
push
-
Statement type: