本报告详细记录了使用 AI 工具将喜马拉雅音频和 B 站视频音频本地化下载、转换并转写为文字的完整实操流程,涵盖工具安装、音频获取、格式转换、语音识别、繁简转换、多格式输出等全链路操作,以及过程中遇到的各类问题与解决方案,旨在帮助初学者快速上手本地化音频转文字工作流。
本项目旨在实现网页音频/视频的本地化转文字,全程在本地完成,无需云端上传,保障隐私安全。覆盖两个典型场景:
最终输出可生成 TXT、HTML、Word 等多种格式,满足不同使用需求。
| 工具 | 用途 | 安装命令 |
|---|---|---|
| imageio-ffmpeg | 提供 ffmpeg 二进制,用于音频格式转换 | pip install imageio-ffmpeg |
| faster-whisper | 本地语音转文字(基于 CTranslate2,比 openai-whisper 快 4 倍) | pip install faster-whisper |
| opencc-python-reimplemented | 繁体中文转简体中文 | pip install opencc-python-reimplemented |
| python-docx | 生成 Word 文档 | pip install python-docx |
推荐使用清华镜像源加速安装:
$ pip install -i https://pypi.tuna.tsinghua.edu.cn/simple imageio-ffmpeg faster-whisper opencc-python-reimplemented python-docx
ImportError: DLL load failed,请安装 VC++ 运行库:winget install Microsoft.VCRedist.2015+.x64<14,否则 faster-whisper 转写时会报 open() got an unexpected keyword argument 'metadata_errors'。安装命令:pip install "av<14"HF_ENDPOINT=https://hf-mirror.com喜马拉雅音频 URL 采用防盗链加密,需通过浏览器解析播放器 API 获取:
https://www.ximalaya.com/sound/763869193)getPlayAddressByTrackId 获取音频地址sign、timestamp、token 等参数注意:URL 有时效性,获取后需尽快下载。
使用 Python 的 urllib 下载,需设置 User-Agent:
import urllib.request
url = "https://audiopay.cos.tx.xmcdn.com/download/.../audio.m4a?sign=...×tamp=..."
req = urllib.request.Request(url, headers={
'User-Agent': 'Mozilla/5.0 ...'
})
with urllib.request.urlopen(req, timeout=30) as response:
with open("output.m4a", "wb") as f:
f.write(response.read())
用 imageio-ffmpeg 将 m4a 转为 16kHz 单声道 wav,再用 faster-whisper 转写:
import imageio_ffmpeg, subprocess
from faster_whisper import WhisperModel
ffmpeg = imageio_ffmpeg.get_ffmpeg_exe()
subprocess.run([ffmpeg, '-y', '-i', 'input.m4a', '-ar', '16000', '-ac', '1', 'output.wav'])
model = WhisperModel('small', device='cpu', compute_type='int8')
segments, info = model.transcribe('output.wav', beam_size=5, language='zh')
for seg in segments:
print(f"[{seg.start:.1f}s] {seg.text}")
B 站视频采用 DASH 格式,音视频分离。通过浏览器解析 window.__playinfo__ 获取音频流:
https://www.bilibili.com/video/BV1DVHq6yEm4/)window.__playinfo__.data.dash.audio 中提取音频流 baseUrlB 站音频流需要带 Referer 头才能下载:
req = urllib.request.Request(url, headers={
'Referer': 'https://www.bilibili.com',
'User-Agent': 'Mozilla/5.0 ...'
})
下载后的 m4s 文件可直接用 ffmpeg 转换为 wav。
faster-whisper 转写中文时可能输出繁体,需用 opencc 转换为简体:
from opencc import OpenCC
cc = OpenCC('t2s') # 繁体转简体
simplified_text = cc.convert(traditional_text)
根据转写内容进行语义分段,使用内联 CSS 生成可读性良好的 HTML:
使用 python-docx 生成 .docx 文档,保留字体、缩进、加粗高亮等样式:
from docx import Document
from docx.shared import Pt, Inches, RGBColor
from docx.enum.text import WD_ALIGN_PARAGRAPH
doc = Document()
# 设置中文字体为微软雅黑
style = doc.styles['Normal']
style.font.name = '微软雅黑'
style.element.rPr.rFonts.set(qn('w:eastAsia'), '微软雅黑')
# ... 添加段落并设置首行缩进、加粗高亮等
doc.save('output.docx')
| 问题 | 原因 | 解决方案 |
|---|---|---|
| pip install 下载缓慢/超时 | 默认 PyPI 源在国外 | 使用清华镜像:-i https://pypi.tuna.tsinghua.edu.cn/simple |
ImportError: DLL load failed(ctranslate2) |
缺少 VC++ 运行库 | winget install Microsoft.VCRedist.2015+.x64 |
open() got an unexpected keyword argument 'metadata_errors' |
av 库版本过高(≥14) | pip install "av<14" |
| faster-whisper 模型下载失败 | HuggingFace 国内访问受限 | 设置环境变量 HF_ENDPOINT=https://hf-mirror.com |
| B 站音频下载 403 | 缺少 Referer 头 | 请求头添加 Referer: https://www.bilibili.com |
| 转写结果为繁体字 | small 模型中文识别偏好 | 用 opencc t2s 转换为简体 |
目录:ximalaya_output\
【商鞅方升】时空之环维修指南06.m4a — 原始音频(548 KB)【商鞅方升】时空之环维修指南06.wav — 转换后音频(2.8 MB)【商鞅方升】时空之环维修指南06_转写文字.txt — 转写文字稿(含时间戳)目录:bilibili_output\
lidaokui_anchoring_effect.m4s — 原始音频流(2.1 MB)lidaokui_anchoring_effect.wav — 转换后音频(6.8 MB)lidaokui_anchoring_effect_transcription.txt — 繁体转写(含时间戳)lidaokui_anchoring_effect_transcription_simplified.txt — 简体转写(含时间戳)lidaokui_anchoring_effect_transcription_simplified.html — 分段 HTMLlidaokui_anchoring_effect_transcription_simplified.docx — Word 文档本方案实现了从网页音频/视频到文字稿的全本地化处理流程,核心优势:
对于初学者,建议先安装好所有依赖(特别是 VC++ 运行库和 av<14),然后从小模型(base/small)开始尝试,熟悉流程后再根据准确率需求选择更大的模型。