python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python aa-whisper包语法

Python中的aa-whisper包语法、参数和实际应用案例小结

作者:王国平

aa-whisper是基于Whisper的增强工具包,支持99种语言、实时语音处理、批量转换及多种输出格式,本文给大家介绍Python中的aa-whisper包语法、参数和实际应用案例小结,感兴趣的朋友一起看看吧

Python 软件包aa-whisper详细介绍

aa-whisper 是基于 OpenAI 的 Whisper 语音识别模型开发的增强型工具包,提供了更便捷的语音转文字功能,支持多语言识别、实时语音处理、批量音频文件转换等扩展功能。它在 Whisper 基础上优化了处理流程,增加了更多实用参数和接口,适合快速集成到语音处理项目中。

一、功能特点

  1. 多语言支持:支持 99 种语言的语音识别,包括中文、英文、日语等。
  2. 实时语音处理:可通过麦克风实时采集语音并转换为文本。
  3. 批量处理:支持批量转换多个音频文件(如 MP3、WAV 等格式)。
  4. 输出格式丰富:支持纯文本、SRT 字幕、JSON 等多种输出格式。
  5. 模型选择灵活:可根据需求选择不同大小的模型(tiny、base、small、medium、large),平衡速度与精度。
  6. 噪音抑制:内置基础噪音过滤功能,提升嘈杂环境下的识别效果。

二、安装方法

aa-whisper 依赖 Python 3.8+ 及 FFmpeg(用于音频处理),安装步骤如下:

  1. 安装 FFmpeg

    • Windows:从 FFmpeg 官网 下载,添加到系统环境变量。
    • macOS:brew install ffmpeg
    • Linux:sudo apt update && sudo apt install ffmpeg
  2. 安装 aa-whisper

    pip install aa-whisper

三、基本语法与参数

核心函数

aa-whisper 的核心函数是 transcribe(),用于语音转文字:

from aa_whisper import transcribe

result = transcribe(audio_path, model_name="base", language="zh")

主要参数

参数名类型说明
audio_pathstr音频文件路径(如 "./test.wav")或麦克风输入标识(如 "microphone")。
model_namestr模型名称,可选:"tiny""base""small""medium""large"
languagestr指定语言(如 "zh" 中文、"en" 英文),自动检测可设为 None
output_formatstr输出格式,可选:"text""srt""json",默认 "text"
output_dirstr输出文件保存路径,默认与音频文件同目录。
verbosebool是否打印处理过程日志,默认 False
temperaturefloat识别随机性(0~1),0 表示确定性输出,默认 0.0。

四、实际应用案例

案例 1:单文件语音转文字(中文)

将中文音频文件转换为文本:

from aa_whisper import transcribe
# 转换中文音频为文本
result = transcribe(
    audio_path="./chinese_speech.wav",
    model_name="base",
    language="zh",
    output_format="text"
)
print("识别结果:", result["text"])

案例 2:生成 SRT 字幕文件

为英文视频的音频轨道生成字幕:

from aa_whisper import transcribe
# 生成英文 SRT 字幕
transcribe(
    audio_path="./english_video.mp3",
    model_name="small",
    language="en",
    output_format="srt",
    output_dir="./subtitles"
)
# 输出文件:./subtitles/english_video.srt

案例 3:实时麦克风语音识别

实时监听麦克风输入并转换为文本:

from aa_whisper import transcribe
# 实时处理麦克风输入(按 Ctrl+C 停止)
try:
    transcribe(
        audio_path="microphone",
        model_name="tiny",  # 小模型适合实时场景
        language="zh",
        verbose=True
    )
except KeyboardInterrupt:
    print("已停止监听")

案例 4:批量处理多语言音频文件

批量转换文件夹中所有音频,自动识别语言:

import os
from aa_whisper import transcribe
audio_dir = "./multilingual_audios"
for filename in os.listdir(audio_dir):
    if filename.endswith((".wav", ".mp3")):
        audio_path = os.path.join(audio_dir, filename)
        transcribe(
            audio_path=audio_path,
            model_name="medium",
            language=None,  # 自动检测语言
            output_format="json"
        )

案例 5:低资源设备适配(tiny 模型)

在树莓派等低配置设备上使用轻量模型:

from aa_whisper import transcribe
# 低资源设备使用 tiny 模型
result = transcribe(
    audio_path="./short_audio.wav",
    model_name="tiny",  # 最小模型,速度最快
    temperature=0.2,    # 降低随机性,提升稳定性
    verbose=True
)

案例 6:提取长音频片段(按时间切片)

对长音频按时间段提取并识别:

from aa_whisper import transcribe
# 仅识别音频中 1分30秒 到 3分钟 的内容
result = transcribe(
    audio_path="./long_speech.mp3",
    model_name="large",
    language="zh",
    start_time=90,  # 开始时间(秒)
    end_time=180    # 结束时间(秒)
)
print("片段识别结果:", result["text"])

五、常见错误与解决方法

六、使用注意事项

  1. 模型选择:小模型(tinybase)速度快但精度低,适合实时场景;大模型(mediumlarge)精度高但耗资源,适合离线批量处理。
  2. 音频预处理:对噪音大、音量低的音频,建议先使用工具(如 Audacity)降噪、增益,再进行识别。
  3. 隐私保护aa-whisper 本地运行,无需上传音频到云端,适合处理敏感内容。
  4. 缓存管理:首次运行会下载模型(约 1GB~3GB),默认缓存路径为 ~/.cache/aa-whisper,可手动清理旧模型释放空间。
  5. 多线程限制:实时麦克风处理不支持多线程,批量处理时可通过多进程提高效率。

通过 aa-whisper,开发者可以快速实现语音识别功能,适用于字幕生成、语音助手、会议记录等多种场景。根据实际需求调整模型和参数,可在速度与精度间取得平衡。

到此这篇关于Python中的aa-whisper包语法、参数和实际应用案例小结的文章就介绍到这了,更多相关Python aa-whisper包语法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文