Google Speechtotext API,InvalidArgument:400必须使用单通道(单声道)

2024-04-27 23:51:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在googlespeech to text中遇到这个错误InvalidArgument: 400,问题似乎是我使用了2声道音频(立体声),而API正在等待wav-in(单声道)。在

如果我在音频编辑器中转换文件,它可能会工作,但我不能使用音频编辑器来转换一批文件。有没有办法改变Python或googlecloud中的音频类型。在

注意:我已经尝试过使用“wave module”,但是我一直收到错误7,因为文件类型无法识别(我无法从Python读取wave模块的wav文件)

-ERROR- InvalidArgument: 400 Must use single channel (mono) audio, but WAV header indicates 2 channels.


Tags: 文件totextinapi错误音频wave
2条回答

假设您使用的是google-cloud-speech库,那么可以使用recognitionConfig中的audio_channel_count属性,并指定输入音频数据中的频道数(默认为一个频道(mono))。你可以这样做:

from google.cloud import speech
client = speech.SpeechClient()
results = client.recognize(
    audio=speech.types.RecognitionAudio(
        uri='gs://your-bucket/recording.wav',
    ),
    config=speech.types.RecognitionConfig(
        encoding='LINEAR16',
        language_code='en-US',
        sample_rate_hertz=44100,
        audio_channel_count=2,
    ),
)

有关详细信息,请参阅API doc。在

您应该使用下面的函数动态返回音频通道和帧速率 它获取音频文件路径并返回帧速率和通道数

def frame_rate_channel(audio_file_name): print(audio_file_name) with wave.open(audio_file_name, "rb") as wave_file: frame_rate = wave_file.getframerate() channels = wave_file.getnchannels() return frame_rate,channels

相关问题 更多 >