繁体   English   中英

wave.Error: unknown format: 3 尝试在 Python 中将 wav 文件转换为文本时出现

[英]wave.Error: unknown format: 3 arises when trying to convert a wav file into text in Python

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

回溯(最近一次调用):文件“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") File "C:\\Users\\HP\\AppData\\Local\\Programs\\Python\\Python37\\lib\\wave.py", line 510, in open return Wave_read(f) File "C:\\ Users\\HP\\AppData\\Local\\Programs\\Python\\Python37\\lib\\wave.py”,第 164 行,在init self.initfp(f) 文件“C:\\Users\\HP\\AppData\\Local\\Programs\\Python\\Python37 \\lib\\wave.py”,第 144 行,在 initfp self._read_fmt_chunk(chunk) 文件“C:\\Users\\HP\\AppData\\Local\\Programs\\Python\\Python37\\lib\\wave.py”,第 269 行,在 _read_fmt_chunk引发错误('未知格式:%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

方法一

您什么也听不到,因为您将浮点值转换为不正确的整数。 信号中的浮点值在 WAV 文件中从 -1 到 1,而 16 位 PCM(整数)值从 -32,768 到 32,767。 所以基本上,你的信号是从类似的东西转换而来的
[-1.4240753e-05, 4.3602209e-05, 1.0526689e-06, ..., 1.7763522e-02, 1.6644333e-02, 6.7148944e-03]

[0, 0, 0, ..., 0, 0, 0]

上面的转换是错误的。

要将文件正确转换为整数(PCM 格式),您需要转换而不是强制转换. 下面给出了这样做的一种方法`def float2pcm(sig, dtype='int16'): sig = np.asarray(sig) dtype = np.dtype(dtype)

i = np.iinfo(dtype)
abs_max = 2 ** (i.bits - 1)
offset = i.min + abs_max
return (sig * abs_max + offset).clip(i.min, i.max).astype(dtype)`

因此您可以在使用sd.wait行后立即使用以下代码

float2pcm(myrecording)

方法二

解决问题的另一种(更简单)方法是使用sounddevice库的功能在内部执行此操作,方法是调用以下函数进行录音。

import numpy as np
myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2, dtype=np.int16)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM