Google cloud -speech api返回null结果

时间:2017-07-16 19:29:49

标签: php encoding google-cloud-speech

我使用Google云语音API。当我运行我的脚本时,会调用API和响应。操作信息返回数据,但结果为空。

这是我的代码(文件网址,文件名,密钥网址,项目名称和存储桶名称,我删除了真实数据):

function __construct(){


        $file_url='file path.mp3';
        $filename='file name.mp3';

        /** Create google client **/
        $client = new Google_Client();
        $key='path to google key';
        putenv($key);
        $client->useApplicationDefaultCredentials();


        /** Create storage **/
        $str_config = array(
                            'projectId' => 'project id'
                            );
        $storage = new StorageClient($str_config);

        $bucket_name='bucket name';
        $bucket=$storage->bucket($bucket_name);
        $object = $bucket->object($filename);


        /** Create Speech **/
        $config = array(
                'projectId' => 'project id',
                'languageCode' => 'en-US'
        );

        $options = array(
                "encoding"=>'LINEAR16',
                "languageCode"=>"en-US",
                'sampleRateHertz' => 16000
        )
        ;
        $speech = new Google\Cloud\Speech\SpeechClient($config);
        $operation = $speech->beginRecognizeOperation(
                $object,
                $options
                );


        $backoff = new ExponentialBackoff(100);
        $backoff->execute(function () use ($operation) {
            print('Waiting for operation to complete' . PHP_EOL);
            $operation->reload();
            if (!$operation->isComplete()) {
                throw new Exception('Job has not yet completed', 500);
            }
        });

            if ($operation->isComplete()) {
                if (empty($results = $operation->results())) {
                    $results = $operation->info();

                }
                var_dump($results, $operatimon->results());
            }


}

我接到电话的结果:

Array
(
    [0] => Array
        (
            [name] => some name
            [metadata] => Array
                (




                    [@type]=> type.googleapis.com/google.cloud.speech.v1.LongRunningRecognizeMetadata
                    [progressPercent] => 100
                    [startTime] => 2017-07-16T19:15:58.768490Z
                    [lastUpdateTime] => 2017-07-16T19:15:59.999625Z
                )

            [done] => 1
            [response] => Array
                (
                    [@type]=> type.googleapis.com/google.cloud.speech.v1.LongRunningRecognizeResponse
                    [totalBilledTime] => 15s
                )

        )

    [1] => Array
        (
        )

)
我尝试了几种文件类型的whit几种编码,找不到合适的组合。或许还有另一个问题。请求帮助。

2 个答案:

答案 0 :(得分:4)

使用ffmpeg库将音频编码为flac whit mono通道解决了这个问题。

答案 1 :(得分:2)

对于遇到此问题的其他人,问题可能在于您的音频文件与您在选项数组中输入的编码不匹配。

检查此资源: https://cloud.google.com/speech-to-text/docs/reference/rest/v1beta1/RecognitionConfig#AudioEncoding

就像接受的答案一样,通过从“LINEAR16”更改为“FLAC”并将我的音频文件转换为FLAC,它对我有用。