Google cloud platform 谷歌云平台:大型媒体文件的语音到文本转换

Google cloud platform 谷歌云平台:大型媒体文件的语音到文本转换,google-cloud-platform,speech-recognition,speech-to-text,google-speech-api,google-cloud-speech,Google Cloud Platform,Speech Recognition,Speech To Text,Google Speech Api,Google Cloud Speech,我正在尝试从从youtube下载的mp4媒体文件中提取文本。由于我在使用谷歌云平台,所以想尝试一下谷歌云语音 在完成所有安装和配置之后,我复制了以下代码段以开始使用: with io.open(file_name, 'rb') as audio_file: content = audio_file.read() audio = types.RecognitionAudio(content=content) config = types.RecognitionConfig(enc

我正在尝试从从youtube下载的mp4媒体文件中提取文本。由于我在使用谷歌云平台,所以想尝试一下谷歌云语音

在完成所有安装和配置之后,我复制了以下代码段以开始使用:

with io.open(file_name, 'rb') as audio_file:
    content = audio_file.read()
    audio = types.RecognitionAudio(content=content)

config = types.RecognitionConfig(encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16, sample_rate_hertz=16000, language_code='en-US')   

response = client.long_running_recognize(config, audio)
但我在文件大小方面遇到以下错误:

无效辩论:400内联音频超过持续时间限制。请使用 GCS URI

然后我读到我应该使用流来处理大型媒体文件。因此,我尝试了以下代码片段:

with io.open(file_name, 'rb') as audio_file:
    content = audio_file.read()

#In practice, stream should be a generator yielding chunks of audio data.

stream = [content]
requests = (types.StreamingRecognizeRequest(audio_content=chunk)for chunk in stream)

config = types.RecognitionConfig(encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,sample_rate_hertz=16000,language_code='en-US')

streaming_config = types.StreamingRecognitionConfig(config=config)

responses = client.streaming_recognize(streaming_config, requests)
但我还是犯了以下错误:

无效辩论:400无效音频内容:太长


那么,谁能建议一种转录mp4文件并提取文本的方法呢。我对非常大的媒体文件没有任何复杂的要求。媒体文件最长可达10-15分钟。谢谢

错误消息表示文件太大,您需要先将媒体文件复制到Google云存储,然后指定一个云存储URI,如gs://bucket/path/mediafile

使用云存储URI的关键是:

识别音频= RecognitionAudio.newBuilder().setUri(gcsUri.build()

下面的代码将向您展示如何为输入指定GCS URI。谷歌在github上有一个平台

  public static void syncRecognizeGcs(String gcsUri) throws Exception {
    // Instantiates a client with GOOGLE_APPLICATION_CREDENTIALS
    try (SpeechClient speech = SpeechClient.create()) {
      // Builds the request for remote FLAC file
      RecognitionConfig config =
          RecognitionConfig.newBuilder()
              .setEncoding(AudioEncoding.FLAC)
              .setLanguageCode("en-US")
              .setSampleRateHertz(16000)
              .build();
      RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(gcsUri).build();

      // Use blocking call for getting audio transcript
      RecognizeResponse response = speech.recognize(config, audio);
      List<SpeechRecognitionResult> results = response.getResultsList();

      for (SpeechRecognitionResult result : results) {
        // There can be several alternative transcripts for a given chunk of speech. Just use the
        // first (most likely) one here.
        SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
        System.out.printf("Transcription: %s%n", alternative.getTranscript());
      }
    }
  }
public static void syncRecognizeGcs(字符串gcsUri)引发异常{
//使用GOOGLE\u应用程序\u凭据实例化客户端
try(SpeechClient speech=SpeechClient.create()){
//生成远程FLAC文件的请求
识别配置=
RecognitionConfig.newBuilder()
.setEncoding(AudioEncoding.FLAC)
.setLanguageCode(“en-US”)
.setSampleRateHertz(16000)
.build();
RecognitionAudio=RecognitionAudio.newBuilder().setUri(gcsUri.build();
//使用阻止调用获取音频转录本
RecognizeResponse response=speech.recognize(配置,音频);
列表结果=response.getResultsList();
for(SpeechRecognitionResult:results){
//对于给定的一段演讲,可以有多个备选文本。只需使用
//第一个(很可能)在这里。
speechrecognitionalAlternative=result.getAlternativesList().get(0);
System.out.printf(“转录:%s%n”,alternative.getTranscript());
}
}
}

您能分享一个示例或示例代码snipet吗?更新了我的答案,包括代码和参考链接。这是我开始使用的代码。我正在使用python,但我有了一个想法,将尝试一下!如果您有任何用python实现的示例,请与我们分享。我正在关注官方网站上给出的视频文件,但正在努力阅读谷歌存储上的视频文件。@JohnHanley除了谷歌存储,还有其他选择吗?照付的那样。因此,它的存储成本以及读写成本都很高