4

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

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

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

)

我尝试了几种文件类型和几种编码,找不到正确的组合。或者也许还有另一个问题。请帮忙。

4

2 回答 2

5

通过使用 ffmpeg 库将音频编码为 flac whit 单声道来解决它。

于 2017-08-05T13:35:21.393 回答
2

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

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

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

于 2018-06-10T14:19:13.490 回答