我无法使用谷歌云语音 api 转录一个简单的 3gp 音频文件。他们的示例在 audio.raw 上运行良好,但是当我将其更改为我的文件时,它出错了。
在我的 android 设备中录制是这样的:
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
我是这样转录的:
import io
import os
# Imports the Google Cloud client library
from google.cloud import speech
# Instantiates a client
speech_client = speech.Client()
# The name of the audio file to transcribe
file_name = os.path.join(
os.path.dirname(__file__),
'resources',
'phone.3gp')
# Loads the audio into memory
with io.open(file_name, 'rb') as audio_file:
content = audio_file.read()
audio_sample = speech_client.sample(
content,
source_uri=None,
encoding='AMR',
sample_rate=8000) #LINEAR16
# Detects speech in the audio file
alternatives = speech_client.speech_api.sync_recognize(audio_sample)
for alternative in alternatives:
print('Transcript: {}'.format(alternative.transcript))
编码和采样率是正确的,但我得到:
Traceback (most recent call last):
File "transcribe.py", line 26, in <module>
alternatives = speech_client.speech_api.sync_recognize(audio_sample)
File "C:\Program Files\Anaconda2\lib\site-packages\google\cloud\speech\_gax.py", line 266, in sync_recognize
raise ValueError('More than one result or none returned from API.')
ValueError: More than one result or none returned from API.