google-app-engine - Error RecognitionConfig 必须未指定或与 google speech to text api 中的 FLAC header 音频中的值匹配

标签 google-app-engine speech-recognition speech google-speech-api google-cloud-speech

我已经尝试将音频从立体声转换为单声道,但没有成功, 我试图改变以赫兹为单位的速率,但也没有成功

from pydub import AudioSegment

from google.cloud import speech_v1p1beta1 as speech
import os, logging 
import urllib.request

KEY_API_ROOT = 'path'
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]=KEY_API_ROOT+"xx.json"
client = speech.SpeechClient()

url = incoming_message['entry'][0]['messaging'][0]['message']['attachments'][0]['payload']['url']

if '.aac' in url:
    formato = 'aac'
else:
    formato = 'mp4'

# download audio
urllib.request.urlretrieve(url, VOICE_ROOT + fbid + "." + formato)

# path
diretorio_audio = VOICE_ROOT + fbid + "." + formato

mp4_version = AudioSegment.from_file(diretorio_audio, formato)

mp4_version.export(VOICE_ROOT + fbid + ".flac", format="flac", bitrate="400k", parameters=["-ac", "1"])

with open(VOICE_ROOT + fbid + '.flac', 'rb') as audio_file:
    content = audio_file.read()

audio = speech.types.RecognitionAudio(content=content)

config = speech.types.RecognitionConfig(
    encoding=speech.enums.RecognitionConfig.AudioEncoding.FLAC,
    sample_rate_hertz=44100,
    language_code='en-US',
    enable_word_confidence=True)
try:
    response = client.recognize(config, audio)
except Exception as erro_stt:
    logging.info("Erro 66 ProcessarAudio no STT: {}".format(erro_stt))

错误:

400 sample_rate_hertz (44100) in RecognitionConfig must either be unspecified or match the value in the FLAC header (48000).

最佳答案

我解决了这个问题:

安装包

sudo apt-get install sox

sudo apt-get install libsox-fmt-mp3

执行

sox input.mp3 --rate 16k --bits 16 --channels 1 output.flac

关于google-app-engine - Error RecognitionConfig 必须未指定或与 google speech to text api 中的 FLAC header 音频中的值匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53675457/

相关文章:

speech-recognition - 无法在 Win 7 x64 上正确安装 MS Speech Platform 11

python-2.7 - osx-dyld : Library not loaded Reason: image not found - Python Google Speech Recognition API

java - GAE : Getting runtime exception when running the (java) guestbook tutorial (working fine locally)

python - 谷歌应用引擎 NDB : How to store document structure?

java - 超超大实体集团交易

android - Decoder_setSearch 返回 -1 异常

python - 如何在长时间运行的程序中立即确认 Pub/Sub 消息

algorithm - 如何使用python查找两个语音文件的差异

ios - 如何为 iOS 语音识别 API 提供提示?