谷歌云语音在python中转录3gp

时间:2017-02-20 17:57:28

标签: android python google-cloud-speech

我无法使用google cloud speech 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.

1 个答案:

答案 0 :(得分:1)

如果有人遇到同样的问题:

mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);

应该是

mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

文档中的常量不同。