将音频文件发送到语音到文本API时,结果为空

时间:2019-01-22 09:57:18

标签: php laravel google-speech-api

当我发送任何音频文件时,我希望API从音频文件返回翻译后的文本,而不是一个空数组。尝试了每种可用的编码类型,并带有正确的采样率(使用ffmpeg for PHP从文件中读取)。我需要怎么做才能获得翻译后的数据?

我尝试了每种可用的编码类型,几个具有不同文件类型的不同音频文件,语音非常清晰。尝试在本地和生产环境中排除本地计算机造成的混乱。 我还在Google API页面上查看了Google提供的可用代码示例,但是这些代码在许多地方已经过时,因此无法使用。

<?php

namespace App\Services\Google;

use App\Exceptions\RecordingWasEmptyException;
use App\Exceptions\RecordingWasTooLongException;
use App\Services\Media\EncodingType;
use App\Services\Media\Media;
use App\Services\User\Localization;
use Google\Cloud\Speech\SpeechClient;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Arr;
use Log;

/**
 * Class SpeechToTextTranslator
 *
 * @package App\Services\Google
 */
class SpeechToTextTranslator
{
    /**
     * @param UploadedFile $file
     *
     * @return array
     * @throws \Throwable
     */
    public static function translate(UploadedFile $file): array
    {
        $results = self::guessEncoding($file);

        throw_if(
            empty($results),
            RecordingWasEmptyException::class, // This exception is thrown 
            'Recording is empty, make sure to speak clearly into your microphone'
        );

        return Arr::map($results, function ($result) {
            return $result->topAlternative()['transcript'];
        });
    }

    /**
     * @param $file
     *
     * @return array|bool
     * @throws \Throwable
     */
    private static function guessEncoding($file)
    {
        $instance      = self::getInstance();
        $encodingTypes = EncodingType::all(); // Returns all constants in Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding in an array
        $media         = Media::parse($file); // Returns an php-ffmpeg Audio instance
        $sampleRate    = collect($media->getStreams()->all())->first()->get('sample_rate');

        throw_if(
            $media->getFormat()->get('duration') > 60,
            RecordingWasTooLongException::class,
            'Recording was too long!'
        );

        foreach ($encodingTypes as $encodingType) {
            try {
                $tmpResults = $instance->recognize(
                    $file,
                    [
                        'encoding'        => $encodingType,
                        'sampleRateHertz' => $sampleRate,
                    ]
                );

                if (!empty($tmpResults)) {
                    return $tmpResults;
                }
            } catch (\Exception $exception) {
                Log::error($exception);
            }
        }

        return false;
    }

    /**
     * @return SpeechClient
     */
    private static function getInstance(): SpeechClient
    {
        return new SpeechClient([
            'keyFilePath'  => storage_path('/path/to/actual/credentials.json'),
            'languageCode' => Localization::userLocale(), // Returns en-US (statically for now)
        ]);
    }
}

我希望在发送音频文件时说出This is a test的情况下得到包含This is a test的结果。

0 个答案:

没有答案