Metadata-Version: 2.4
Name: bilibili-watch-mcp
Version: 0.1.0
Summary: MCP server that turns Bilibili videos into agent-readable materials: subtitle text, danmaku, keyframe contact sheets, hi-res frames
License: MIT
Keywords: mcp,bilibili,yt-dlp,video-understanding,agent
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: requests>=2.31
Requires-Dist: yt-dlp>=2026.7.4
Requires-Dist: imageio-ffmpeg>=0.5
Requires-Dist: mcp>=2.0
Requires-Dist: websockets>=13
Dynamic: license-file

<!-- mcp-name: io.github.Liber1917/bilibili-watch-mcp -->

# bilibili-watch-mcp

An MCP server that turns Bilibili videos into **agent-readable materials**:
subtitle text, danmaku (bullet comments), sprite keyframe contact sheets, and
optional hi-res frames. Built for tool-using agents (Claude Code, Cline, etc.)
that need to *understand* a video, not download it.

**Bilibili 视频 → agent 可读材料** 的 MCP server：字幕全文、弹幕、进度条关键帧拼图、可选高清抽帧。给 Claude Code 这类工具型 agent 提供"看懂视频"的能力。

## Why this exists

- Most Bilibili MCP servers only expose search/metadata. The ones that expose
  subtitles depend on `bilibili-api-python`, which was taken down by a legal
  notice in 2026 and is unmaintained. This project's fetching is built on
  **yt-dlp** (actively maintained, quick to follow Bilibili's anti-bot changes)
  plus two signature-free endpoints for danmaku and sprite sheets — no dead
  dependencies.
- The sprite contact sheet trick: one request returns Bilibili's own progress-bar
  preview thumbnails covering the whole video — no login, no video download.
  Good for coarse "where am I in this video" localization. Hi-res frames (854px)
  make on-screen code / slide text readable.

## Tools

| Tool | What it does |
|---|---|
| `search_videos(keyword, limit)` | Login-free site search. Returns BV id, title, uploader, duration, plays. |
| `watch_video(video, with_frames, max_frames, subtitle_max_chars)` | Fetch one video: metadata + subtitle text inline + danmaku + sprite sheets. `with_frames=True` additionally downloads a low-quality stream and extracts uniform frames + scene-cut frames (854px), then deletes the video. |

`video` accepts a BV id or a full URL. AI-generated subtitles require the
account cookie (see `BILI_SESSDATA` below); CC subtitles and everything else
work login-free.

## Install

```bash
pip install bilibili-watch-mcp        # or: uvx bilibili-watch-mcp
```

Runtime dependencies (`requests`, `yt-dlp`, `imageio-ffmpeg`, `mcp`) are pulled
in automatically. No ffmpeg installation needed — `imageio-ffmpeg` ships a
static binary.

## Configure your client

Claude Code:

```bash
claude mcp add bilibili-watch -- bilibili-watch-mcp
```

Or `.mcp.json` / `claude_desktop_config.json`:

```json
{
  "mcpServers": {
    "bilibili-watch": {
      "command": "bilibili-watch-mcp",
      "env": {
        "BILI_SESSDATA": "",
        "BILI_WATCH_OUT": "./bili_watch_out"
      }
    }
  }
}
```

## Authentication (AI subtitles & better search)

Two ways to provide the Bilibili session cookie (`SESSDATA`), tried in order:

1. **Automatic — via a debug-enabled browser (recommended).** Since Chrome 136,
   the default profile cannot be opened with a debugging port, so use a
   dedicated profile. Start Chrome once, log in to Bilibili in that window;
   the session persists in the profile:

   ```
   chrome.exe --user-data-dir="%USERPROFILE%\bili-watch-auth" --remote-debugging-port=9222 https://www.bilibili.com
   ```

   The server auto-detects `127.0.0.1:9222` and pulls `SESSDATA` over CDP on
   demand. Cookie values live in memory only — never written to disk, never
   echoed. If the debug browser is closed, tools fall back to login-free mode
   (danmaku/sprites/≤1080p still work; AI subtitles don't).

2. **Manual — environment variable.** Set `BILI_SESSDATA` to the cookie value
   from your browser's DevTools.

Note: yt-dlp cannot fetch Bilibili's AI-generated subtitles even when
authenticated, so this server additionally pulls them from the player API
directly (requires login state) and converts them to `.srt`.

Environment variables:

| Var | Meaning |
|---|---|
| `BILI_SESSDATA` | Optional. Your Bilibili session cookie value (browser DevTools → Cookies). Unlocks AI-generated subtitles. Give it only if you accept the account implications. |
| `BILI_WATCH_OUT` | Output directory for fetched materials. Default `./bili_watch_out`. |

## How an agent should use the output

`watch_video` returns a text report with:

1. **subtitle text inline** — for lecture/slide videos, this alone is usually
   90% of the content. Read it first.
2. **sprite contact sheet paths** — one or a few JPGs, each a 10x10 grid of
   thumbnails evenly covering the whole video. Cell *k* ≈ *k × duration / total
   cells*. Use for coarse time localization.
3. **frames paths (if `with_frames`)** — `uniform_*.jpg` with a timestamp table,
   and `scene_*.jpg` at detected scene cuts. 854px wide; on-screen text
   (slides, code) is readable. Read these with your file/image tool.

Typical flows:

- Lecture / tutorial → `search_videos` → `watch_video(bvid)` → read subtitle text.
- Hands-on coding demo → `watch_video(bvid, with_frames=true)` → locate the
  segment on sprite sheets → read nearby `frames/*.jpg`.

## Legal & etiquette

- Personal, low-volume use only. This project talks to Bilibili as a guest
  client; abusive request rates will get your IP rate-limited (-352 / HTTP 412)
  and may put your account at risk if you attach `BILI_SESSDATA`.
- Not affiliated with or endorsed by Bilibili. You are responsible for
  complying with their terms of service and local law. Bilibili has been
  actively sending legal notices to third-party API projects since 2026 —
  this project deliberately keeps its footprint minimal (yt-dlp + two
  signature-free endpoints) but cannot promise that never changes.

## 中文说明（简要）

给 agent 用的 B站视频理解 MCP。`search_videos` 免登录搜视频，`watch_video` 一键取字幕全文（直接返回文本）+ 弹幕 + 进度条关键帧拼图（粗定位画面）+ 可选高清抽帧（854px，PPT/代码可读，实操演示类用 `with_frames=True`）。取料底座是 yt-dlp（活跃维护、跟进B站风控），弹幕和拼图走两个免签名接口，不依赖任何已关停的第三方 SDK。

登录态（AI字幕/提高搜索成功率）推荐自动方式：开一个带调试端口的专用 Chrome 窗口并登录一次（`chrome.exe --user-data-dir="%USERPROFILE%\bili-watch-auth" --remote-debugging-port=9222 https://www.bilibili.com`），server 会自动经 CDP 取 SESSDATA（只在内存、不落盘）；调试窗口关掉则自动降级为免登录模式。也可手动设 `BILI_SESSDATA` 环境变量。注意 yt-dlp 拿不到 B站 AI 字幕，本工具会登录态直连播放器接口补拉并转成 srt。仅供个人低频使用，请尊重平台条款。

## License

MIT. See [LICENSE](LICENSE).
