gpt4 book ai didi

python - wave.Error : unknown format: 3 arises when trying to convert a wav file into text in Python

转载 作者:行者123 更新时间:2023-12-03 23:35:06 25 4
gpt4 key购买 nike

我需要从麦克风录制音频并将其转换为文本。我已经使用从网上下载的几个音频剪辑尝试了这个转换过程,它工作正常。但是,当我尝试转换从麦克风录制的音频剪辑时,会出现以下错误。

回溯(最近一次通话最后):
文件“C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\site-packages\speech_recognition__init__.py”,第 203 行,位于 输入
self.audio_reader = wave.open(self.filename_or_fileobject, "rb")
文件“C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\wave.py”,第 510 行,打开
返回 Wave_read(f)
文件“C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\wave.py”,第 164 行,位于 初始化
self.initfp(f)
文件“C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\wave.py”,第 144 行,在 initfp
self._read_fmt_chunk( block )
_read_fmt_chunk 中的文件“C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\wave.py”,第 269 行
raise Error('未知格式: %r' % (wFormatTag,))
wave.Error:未知格式: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

关于python - wave.Error : unknown format: 3 arises when trying to convert a wav file into text in Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60352850/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com