【问题标题】:Google Cloud Platform: Speech to Text Conversion of Large Media FilesGoogle Cloud Platform:大型媒体文件的语音到文本转换
【发布时间】:2018-11-14 19:43:31
【问题描述】:

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

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

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)

但我收到以下关于文件大小的错误:

InvalidArgument: 400 内嵌音频超出持续时间限制。请使用一个 GCS URI。

然后我读到我应该将流用于大型媒体文件。所以,我尝试了以下代码sn-p:

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)

但我仍然收到以下错误:

InvalidArgument:400 无效的音频内容:太长。

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

【问题讨论】:

    标签: google-cloud-platform speech-recognition speech-to-text google-speech-api google-cloud-speech


    【解决方案1】:

    错误信息表示文件太大,您需要先将媒体文件复制到 Google Cloud Storage,然后指定 Cloud Storage URI,例如 gs://bucket/path/mediafile。

    使用 Cloud Storage URI 的关键是:

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

    以下代码将向您展示如何为输入指定 GCS URI。 Google 在 github 上有一个 complete example

      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());
          }
        }
      }
    

    【讨论】:

    • 您能分享一个示例或示例代码片段吗?
    • 更新了我的答案以包含代码和参考链接。这是我开始使用的代码。
    • 我在 python 中工作,但有一个想法,会尝试一下!
    • 如果您有任何用 python 实现的示例,请分享。我正在关注官方网站上给出的内容,但在阅读谷歌存储上的视频文件时遇到了困难。
    • @JohnHanley 除了谷歌存储还有其他选择吗?因为它是支付的。所以它的存储成本以及读写成本
    猜你喜欢
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多