2

我想从视频中生成文本文件。我在 Laravel 5.6 中使用了 Google Speech to Text 服务。

使用的服务/包

  1. 谷歌语音转文本
  2. 适用于 PHP 的 Google Cloud 语音

代码

    use Google\Cloud\Speech\V1\RecognitionAudio;
    use Google\Cloud\Speech\V1\RecognitionConfig;
    use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
    use Google\Cloud\Speech\V1\SpeechClient;

    $jsonFileUrl = Storage::disk('local')->path('');  //Googel service account json file path
    putenv("GOOGLE_APPLICATION_CREDENTIALS=$jsonFileUrl");

    $videoFile = Storage::disk('review')->path('UHD_4K_65_TV.mp4');
    $encoding = AudioEncoding::LINEAR16;
    $sampleRateHertz = 32000;
    $languageCode = 'en-US';
    $content = file_get_contents($videoFile);

    $audio = (new RecognitionAudio())
        ->setContent($content);
    $config = (new RecognitionConfig())
        ->setEncoding($encoding)
        ->setSampleRateHertz($sampleRateHertz)
        ->setLanguageCode($languageCode)
        ->setModel('video');

    $client = new SpeechClient();
    $response = $client->longRunningRecognize($config, $audio);
    $response->pollUntilComplete();
    if ( $response->operationSucceeded () ) {
        foreach ($response->getResult ()->getResults() as $result) {
            $saida = [ 'transcript' => [], 'word' => [] ];
            $alternatives = $result->getAlternatives ();
            $mostLikely   = $alternatives[ 0 ];
            $transcript   = $mostLikely->getTranscript ();
            $confidence   = $mostLikely->getConfidence ();

            $saida[ 'transcripts' ][] = [
                'transcript' => $transcript,
                'confidence' => $confidence
            ];

            foreach ( $mostLikely->getWords () as $wordInfo ) {
                $saida[ 'words' ][] = [
                    'word'  => $wordInfo->getWord (),
                    'start' => GPBUtil::formatDuration ( $wordInfo->getStartTime () ),
                    'end'   => GPBUtil::formatDuration ( $wordInfo->getEndTime () )
                ];
            }
            dd($saida);
        }
    } else {
        dd($response->getError ());
    }
    $client->close();

结果

["transcript"=>[],"word"=>[],"transcripts"=>[0=>["transcript"=>"","confidence"=>0.0]]]

示例视频:https ://vimeo.com/425422475/ff471a7edb

我必须从上面的视频语音中生成文本。

我已成功从https://cloud.google.com/speech-to-text生成此视频的文本文件

请建议我如何使用 API 转换视频。

4

0 回答 0