【问题标题】:Google Cloud Speech API returns nothing for audio longer than 1 minute对于超过 1 分钟的音频,Google Cloud Speech API 不返回任何内容
【发布时间】:2017-01-02 09:43:51
【问题描述】:

短于 1 分钟的音频文件可以毫无问题地转录,但当我尝试转录较长的文件时,Google Speech API 返回一个空响应。

我使用以下 SoX 命令制作 .wav 文件:

sox input.flac --channels=1 --bits=16 --rate=16000 --encoding=signed-integer --endian=little output.wav

文件按预期播放。运行 SoXi,我得到以下信息:

Input File     : 'output.wav'
Channels       : 1
Sample Rate    : 16000
Precision      : 16-bit
Duration       : 00:02:35.71 = 2491408 samples ~ 11678.5 CDDA sectors
File Size      : 4.98M
Bit Rate       : 256k
Sample Encoding: 16-bit Signed Integer PCM

然后我将它上传到我的 Google 存储,因为文档指出任何大于 1 分钟的文件都必须驻留在 gs 存储桶中,以便 API 转录它。

然后我运行以下代码开始转录操作:

use \Google\Cloud\ServiceBuilder;

$cloud = new ServiceBuilder([
    'keyFilePath' => '/var/www/cert/gcloud_key.json',
    'projectId' => 'm****n-141000'
]);

$speech = $cloud->speech();

$operation = $speech->beginRecognizeOperation(
    "gs://m****n-141000.appspot.com/output.wav", [
    'encoding' => 'LINEAR16',
    'sampleRate' => 16000
]);

$isComplete = $operation->isComplete();

while (!$isComplete) {
    sleep(1);
    $operation->reload();
    $isComplete = $operation->isComplete();
}

var_dump($operation->results());

返回的响应是空的。完整的响应如下所示:

object(stdClass)#27 (4) {
  ["name"]=>
  string(19) "1904326252537199795"
  ["metadata"]=>
  object(stdClass)#24 (4) {
    ["@type"]=>
    string(70) "type.googleapis.com/google.cloud.speech.v1beta1.AsyncRecognizeMetadata"
    ["progressPercent"]=>
    int(100)
    ["startTime"]=>
    string(27) "2017-01-02T09:36:45.780425Z"
    ["lastUpdateTime"]=>
    string(27) "2017-01-02T09:36:46.720260Z"
  }
  ["done"]=>
  bool(true)
  ["response"]=>
  object(stdClass)#26 (1) {
    ["@type"]=>
    string(70) "type.googleapis.com/google.cloud.speech.v1beta1.AsyncRecognizeResponse"
  }
}

表示请求已成功运行并完成,但没有任何实际响应。我哪里错了?

【问题讨论】:

    标签: php google-speech-api google-cloud-speech


    【解决方案1】:

    语音 API 文档 (https://cloud.google.com/speech/docs/encoding) 说不支持 wav 文件。它应该是没有任何标题的原始文件(带有 *.raw 扩展名)。 sox 转换应该有“--type=FILETYPE”定义,但不幸的是我不确定它是“--type=raw”还是其他东西。

    【讨论】:

      【解决方案2】:

      在此处查看文档:https://cloud.google.com/speech/docs/basics

      请注意,目前还没有结果。 Speech API 将继续处理提供的音频并使用此操作来存储最终结果,该结果将在请求完成后出现在操作的响应字段(AsyncRecognizeResponse 类型)中。

      我假设有一种方法可以提供一个回调函数来处理包括转录在内的实际响应。

      【讨论】:

        【解决方案3】:

        您必须传递 Google Cloud Storage 对象

        那就试试吧:

        use \Google\Cloud\ServiceBuilder;
        
        $cloud = new ServiceBuilder([
            'keyFilePath' => '/var/www/cert/gcloud_key.json',
            'projectId' => 'm****n-141000'
        ]);
        
        $storage = $cloud->storage();
        $bucket = $storage->bucket($bucket_name);
        $object = $bucket->object($audio_filename);
        
        $speech = $cloud->speech();
        
        $operation = $speech->beginRecognizeOperation(
            $object, [
            'encoding' => 'LINEAR16',
            'sampleRate' => 16000
        ]);
        
        $isComplete = $operation->isComplete();
        
        while (!$isComplete) {
            sleep(1);
            $operation->reload();
            $isComplete = $operation->isComplete();
        }
        
        var_dump($operation->results());
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-09-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多