You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Whisper批量处理桌面Audio目录下的WAV音频文件?

修正Whisper批量转写WAV文件的脚本

错误根源:os.listdir('Audio')仅返回文件名,脚本运行时会在当前目录(桌面)查找文件,而非Audio子目录,导致找不到目标音频文件。

以下是修正后的脚本,同时增加了WAV文件过滤、结果保存等实用功能:

import whisper
import os

# 加载Whisper模型,指定fp16=False消除CPU运行时的警告
model = whisper.load_model("base", fp16=False)

# 定义桌面Audio目录的完整路径
audio_dir = os.path.expanduser("~/Desktop/Audio")

# 遍历目录下所有文件
for filename in os.listdir(audio_dir):
    # 仅处理WAV格式文件
    if filename.lower().endswith(".wav"):
        # 拼接完整文件路径
        full_path = os.path.join(audio_dir, filename)
        # 执行转写
        transcribe_result = model.transcribe(full_path)
        # 将转写结果保存为同名TXT文件
        output_txt = os.path.splitext(full_path)[0] + ".txt"
        with open(output_txt, "w", encoding="utf-8") as txt_file:
            txt_file.write(transcribe_result["text"])
        print(f"转写完成:{filename}")

关键修改说明

  • 完整路径拼接:用os.path.join组合目录和文件名,确保Whisper能定位到正确的音频文件
  • 格式过滤:通过.endswith(".wav")只处理目标格式文件,避免无效文件干扰
  • 消除警告:加载模型时添加fp16=False,解决CPU环境下的FP16不支持警告
  • 结果保存:自动将转写文本保存为对应音频文件的同名TXT,方便后续查看

内容的提问来源于stack exchange,提问作者Alligator

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 11:50:33