问题描述
我需要从麦克风录制音频并将其转换为文本。我用从网上下载的几个音频片段尝试了这个转换过程,效果很好。但当我尝试转换从麦克风录制的音频剪辑时,出现以下错误。 回溯(最近一次调用): 文件"C:UsersHPAppDataLocalProgramsPythonPython37libsite-packagesspeech_recognition__init__.py",第203行,在中输入 Self.AudioReader=Wave.Open(self.filename_or_fileobject,"RB") 文件"C:UsersHPAppDataLocalProgramsPythonPython37libwave.py",第510行,打开 返回波_读取(F) 初始化中的文件"C:UsersHPAppDataLocalProgramsPythonPython37libwave.py",第164行 Self.initfp(F) 文件"C:UsersHPAppDataLocalProgramsPythonPython37libwave.py",第144行,在initfp中 SELF_READ_FMT_CHUNK(块) 文件块第269行,in_"C:UsersHPAppDataLocalProgramsPythonPython37libwave.py",_fmt_chunk 引发错误(‘未知格式:%r’%(wFormatTag,)) 波形。错误:未知格式:3
我尝试的代码如下所示。
import speech_recognition as sr
import sounddevice as sd
from scipy.io.wavfile import write
# recording from the microphone
fs = 44100 # Sample rate
seconds = 3 # Duration of recording
myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
sd.wait() # Wait until recording is finished
write('output.wav', fs, myrecording) # Save as WAV file
sound = "output.wav"
recognizer = sr.Recognizer()
with sr.AudioFile(sound) as source:
recognizer.adjust_for_ambient_noise(source)
print("Converting audio file to text...")
audio = recognizer.listen(source)
try:
text = recognizer.recognize_google(audio)
print("The converted text:" + text)
except Exception as e:
print(e)
我查看了回答的类似问题,他们说我们需要将其转换为不同的WAV格式。有没有人能给我提供一个代码或库,我可以用来进行这种转换?提前谢谢您。
推荐答案
您以浮点格式编写了文件:
soxi output.wav
Input File : 'output.wav'
Channels : 2
Sample Rate : 44100
Precision : 25-bit
Duration : 00:00:03.00 = 132300 samples = 225 CDDA sectors
File Size : 1.06M
Bit Rate : 2.82M
Sample Encoding: 32-bit Floating Point PCM
并且WAVE模块无法读取它。
要存储int16格式,请执行以下操作:
import numpy as np
myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
sd.wait() # Wait until recording is finished
write('output.wav', fs, myrecording.astype(np.int16)) # Save as WAV file in 16-bit format
这篇关于Wave.Error:UNKNOWN FORMAT:3尝试将WAV文件转换为Python中的文本时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!