使用openai-whisper实现语音转文字

使用openai-whisper实现语音转文字

1 安装依赖

1.1 Windows下安装ffmpeg

FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。采用LGPL或GPL许可证。它提供了录制、转换以及流化音视频的完整解决方案。

复制代码
# ffmpeg官网
https://ffmpeg.org/

# ffmpeg下载地址
https://ffmpeg.org/download.html

# 点击下载后会进入github,地址如下
https://github.com/BtbN/FFmpeg-Builds/releases

在官网上选择windows版本

在GitHub上可以选择最新版本,选择ffmpeg-master-latest-win64-gpl.zip

如果python程序出现"FileNotFoundError: [WinError 2] 系统找不到指定的文件。"错误时,可能是ffmpeg版本的问题。

ffmpeg-master-latest-win64-gpl.zip 解压到D盘,名字修改为ffmpeg, 将目录 D:\ffmpeg\bin 添加到环境变量中。

在dos页面查看版本号中输入:ffmpeg.exe -version,出现下面的信息表示安装成功。

1.2 安装openai-whispe

复制代码
# 安装openai-whisper
pip install openai-whisper -i https://pypi.tuna.tsinghua.edu.cn/simple

# 安装pydub切割音频,防止音频太长
pip install pydub -i https://pypi.tuna.tsinghua.edu.cn/simple

下载语音,可以直接在浏览器中打开,再下载

复制代码
wget -c https://paddlespeech.bj.bcebos.com/PaddleAudio/zh.wav
wget -c https://paddlespeech.bj.bcebos.com/PaddleAudio/en.wav

2 使用openai-whispe

2.1 工程目录

2.2 main.py

python 复制代码
import whisper
model = whisper.load_model(name="tiny", download_root="./model")

# load audio and pad/trim it to fit 30 seconds
audio = whisper.load_audio(".//data//zh.wav")
audio = whisper.pad_or_trim(audio)

# make log-Mel spectrogram and move to the same device as the model
mel = whisper.log_mel_spectrogram(audio).to(model.device)

# detect the spoken language
_, probs = model.detect_language(mel)
print(f"Detected language: {max(probs, key=probs.get)}")

# decode the audio
options = whisper.DecodingOptions()
result = whisper.decode(model, mel, options)

# print the recognized text
print(result.text)

输出结果:

复制代码
Detected language: zh
我認為跑步最重要的就是給我帶來了身體健康
相关推荐
数据饕餮7 天前
Faster-Whisper命令和意图识别程序设计调优:上下文感知和领域词汇增强
whisper
落淼喵_G8 天前
ubuntu部署whisper+speaker_large+qwen【一】
ubuntu·whisper
兔兔爱学习兔兔爱学习9 天前
浏览器端实时语音采集 + WebSocket 传输 + 后端 Whisper + GPT 翻译 + 实时字幕返回
gpt·websocket·whisper
兔兔爱学习兔兔爱学习9 天前
一个可本地运行的实时字幕翻译 Demo(Whisper + GPT + Streamlit),可以边说边出中英文字幕
gpt·whisper
数据饕餮10 天前
Faster-Whisper唤醒词检测程序设计实战1
whisper
说话的鲸鱼10 天前
‌Whisper模型在RTranslator中的实时语音识别优化:动态资源分配与负载均衡
whisper·负载均衡·语音识别
猫头虎18 天前
DeepSeek刚刚开源了一个3B的 OCR模型:什么是DeepSeek-OCR?单张A100-40G每天可以处理20万+页文档
人工智能·开源·whisper·prompt·aigc·ocr·gpu算力
星野云联AIoT技术洞察1 个月前
2025年语音识别(ASR)与语音合成(TTS)技术趋势分析对比
whisper·语音识别·模型部署·tts·asr·嵌入式ai·naturalspeech3
共绩算力1 个月前
OpenAI Whisper 语音识别模型:技术与应用全面分析
人工智能·whisper·语音识别·共绩算力
人工智能技术派1 个月前
Whisper推理源码解读
人工智能·语言模型·whisper·语音识别