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

如何用OpenAI Whisper在Python中成功转录音频?遇报错求助

问题解决:Whisper模型运行时的文件找不到错误与FP16警告

一、文件找不到错误排查与修复

你打印的路径C:\Users\rene.pessoto\Desktop\Audio\audio.wav看起来格式正确,但仍需确认以下几点:

  • 该路径下确实存在audio.wav文件,注意检查文件名拼写(包括大小写,Windows虽不严格区分,但仍需匹配)
  • 文件未被其他程序占用(比如音频播放器打开状态会导致无法读取)
  • 代码中可提前校验文件存在性,避免进入转录逻辑后才报错

二、FP16不支持警告的消除

由于你使用CPU运行,Whisper的large模型默认尝试使用FP16精度,但CPU不支持该精度,因此会自动降级为FP32并触发警告。可通过显式指定参数消除警告:在load_model时添加device="cpu"和fp16=False。

三、修改后的完整代码

import whisper
import os

# 获取音频文件绝对路径
current_dir = os.path.dirname(os.path.abspath(__file__))
file_name = "audio.wav"
file_path = os.path.join(current_dir, file_name)
print(f"音频文件路径: {file_path}")

# 提前校验文件是否存在
if not os.path.exists(file_path):
    print(f"错误:文件 {file_path} 不存在")
    exit(1)

def transcribe():
    # 显式指定CPU运行与FP32精度,消除警告
    model = whisper.load_model("large", device="cpu", fp16=False)
    result = model.transcribe(file_path)
    print(result["text"])
    return result

try:
    result = transcribe()
    # 用绝对路径保存转录结果,避免路径歧义
    save_path = os.path.join(current_dir, "transcript.txt")
    with open(save_path, "w", encoding="utf-8") as file:
        file.write(result["text"])
    print("转录文件已保存")
except PermissionError:
    print("无权限保存文件,请检查目录读写权限")
except Exception as e:
    print(f"发生未知错误: {str(e)}")
finally:
    print("退出程序")

额外说明

  • 39分钟、214MB的音频用large模型在CPU上运行会非常耗时,若有GPU建议切换到GPU环境;若必须用CPU,可改用base/small模型平衡速度与精度
  • 保存文件时指定encoding="utf-8",可避免中文等特殊字符出现乱码

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 14:22:44