大多数语音转文字工具都是要收费的,免费的很少,而且质量也不太高,python有自己的语音识别库,因此尝试使用一下。
先安装库:
pip install pydub speechrecognition
pip install pydub
安装 ffmpeg
brew install ffmpeg
录音格式要转换成wav
# 定义音频文件路径
audio_file_path = "path_to_your_audio_file.m4a"
wav_file_path = "path_to_your_audio_file.wav"
# 将M4A文件转换为WAV格式
audio = pydub.AudioSegment.from_file('a1.m4a', format="m4a")
audio.export(wav_file_path, format="wav")
import pydub
import speech_recognition as sr
# 定义音频文件路径
audio_file_path = "path_to_your_audio_file.m4a"
wav_file_path = "path_to_your_audio_file.wav"
# 将M4A文件转换为WAV格式
audio = pydub.AudioSegment.from_file('a1.m4a', format="m4a")
audio.export(wav_file_path, format="wav")
# 初始化识别器
recognizer = sr.Recognizer()
# 使用识别器识别音频文件中的语音
with sr.AudioFile(wav_file_path) as source:
audio_data = recognizer.record(source)
try:
# 使用Google Web Speech API识别语音
text = recognizer.recognize_google(audio_data, language="zh-CN")
print("识别的文本:", text)
except sr.UnknownValueError:
print("无法识别音频中的语音")
except sr.RequestError as e:
print("无法处理音频请求; {0}".format(e))
运行......