Subtitles first: First try to fetch official/automatic subtitles (seconds-level, no GPU, no funasr); if unavailable, download the audio and transcribe with SenseVoice-Small, saving as a Markdown file. Add --no-subtitle to force audio transcription.
Environment Requirements
# Python 3.9+
python -m venv .venv
source .venv/bin/activate
# 依赖
pip install funasr modelscope torch torchaudio
# 系统依赖
# macOS: brew install ffmpeg yt-dlp
# Ubuntu: sudo apt install ffmpeg && pip install yt-dlp
Usage
python scripts/transcribe.py "https://www.bilibili.com/video/BV1rrQGBeEen/"
python scripts/transcribe.py "BV1rrQGBeEen"
python scripts/transcribe.py "BV1rrQGBeEen" --no-subtitle # 强制音频转录
# 批量处理:每行一个 B站 URL 或 BV 号
python scripts/batch_transcribe.py urls.txt -o ./output
Workflow
Step 1: Download audio with yt-dlp
Bilibili has anti-scraping mechanisms, so User-Agent and Referer need to be added:
yt-dlp --extract-audio --audio-format mp3 --audio-quality 128K \
--user-agent "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) ..." \
--referer "https://www.bilibili.com" \
"https://www.bilibili.com/video/BV1rrQGBeEen/"
Step 2: Transcribe with SenseVoice-Small
from funasr import AutoModel
from funasr.utils.postprocess_utils import rich_transcription_postprocess
model = AutoModel(
model="iic/SenseVoiceSmall",
trust_remote_code=True,
vad_model="fsmn-vad",
vad_kwargs={"max_single_segment_time": 30000},
device="cpu",
)
result = model.generate(input=audio_path, language="zh", use_itn=True, batch_size_s=60)
Step 3: Generate Markdown
Automatically create a Markdown file with frontmatter.
Batch Mode
scripts/batch_transcribe.py calls the single-article transcription script one by one; by default it continues processing the next item after a single failure; add --stop-on-error。
Performance Data
| Video Duration | Transcription Time |
|---|---|
| 5 min | ~10s |
| 10 min | ~20s |
| 30 min | ~60s |
Known Limitations
- Bilibili 4K/1080P60 requires a Premium membership; regular users can download 720P and below
- The SenseVoice-Small model requires an initial download of about 893MB
- Speaker diarization is not supported
- For multi-part videos, only the first part is downloaded
Reference Projects
- yt-dlp/yt-dlp - Video download tool
- FunAudioLLM/SenseVoice - Speech recognition model
⚖️ Compliance Statement
Forpersonal learning and researchuse only. Please comply with the target platform's Terms of Service (ToS) and robots rules, control request frequency, and do not use it for bulk scraping, commercial crawling, or scenarios that infringe on others' rights. The copyright of downloaded content belongs to the original author.