Python 语音转文字:用 FunASR 本地、免费地把音频转成文本
在 Python 里把音频转成文字,不一定要用云 API 或庞大的 Whisper。FunASR 几行代码就能在本地、免费地转写,还自带时间戳、说话人分离和批量处理。中文尤其强,同时支持 50+ 语言。下面每段代码都已实测。
安装
pip install -U torch torchaudio
pip install -U funasr
最简单:几行转写一个文件
from funasr import AutoModel
from funasr.utils.postprocess_utils import rich_transcription_postprocess
model = AutoModel(model="iic/SenseVoiceSmall", vad_model="fsmn-vad", device="cuda")
result = model.generate(input="audio.wav")
print(rich_transcription_postprocess(result[0]["text"]))
# -> 欢迎大家来体验达摩院推出的语音识别模型
默认用 SenseVoice(非自回归、极快)。rich_transcription_postprocess 用来清掉 SenseVoice 的 <|zh|> 等标签,得到干净文本——别漏掉这一步。
直接从 URL 转写
result = model.generate(
input="https://example.com/audio.wav" # 也支持本地路径、numpy、bytes
)
要时间戳和说话人
结果里的 sentence_info 每段都带 start/end(毫秒)、spk(说话人)、sentence:
model = AutoModel(model="iic/SenseVoiceSmall", vad_model="fsmn-vad", spk_model="cam++", device="cuda")
result = model.generate(input="audio.wav")
for seg in result[0]["sentence_info"]:
start = seg["start"] / 1000
text = rich_transcription_postprocess(seg["sentence"])
print("[%.1fs] Speaker %s: %s" % (start, seg["spk"], text))
# -> [0.6s] Speaker 0: 欢迎大家来体验达摩院推出的语音识别模型
批量转写多个文件
把文件列表传给 input,一次返回每个文件的结果:
results = model.generate(input=["a.wav", "b.wav", "c.wav"])
for r in results:
print(rich_transcription_postprocess(r["text"]))
选哪个模型
| model= | 适合 |
|---|---|
iic/SenseVoiceSmall | 默认,极快,50+ 语言,带情感/事件 |
paraformer-zh | 经典中文生产级,稳 |
FunAudioLLM/Fun-ASR-Nano-2512 | LLM 解码,精度最高,31 语言含方言 |
换模型只改 model= 即可,其余代码不变。
为什么用 FunASR
- 本地、免费、隐私:无需联网、无 API key、无时长费用。
- 快:SenseVoice 非自回归,远快于 Whisper(实测对比),CPU 也能实时。
- 中文更准 + 50+ 语言;时间戳、说话人、热词、批量开箱即用。
FunASR 是通义实验室开源的工业级语音识别工具包。
在 GitHub 上 Star FunASR ★