function analyze_transcription()

in video/src/analyze_transcription.php [32:90]


function analyze_transcription(string $uri, int $pollingIntervalSeconds = 0)
{
    # set configs
    $speechTranscriptionConfig = (new SpeechTranscriptionConfig())
        ->setLanguageCode('en-US')
        ->setEnableAutomaticPunctuation(true);
    $videoContext = (new VideoContext())
        ->setSpeechTranscriptionConfig($speechTranscriptionConfig);

    # instantiate a client
    $client = new VideoIntelligenceServiceClient();

    # execute a request.
    $features = [Feature::SPEECH_TRANSCRIPTION];
    $request = (new AnnotateVideoRequest())
        ->setInputUri($uri)
        ->setVideoContext($videoContext)
        ->setFeatures($features);
    $operation = $client->annotateVideo($request);

    print('Processing video for speech transcription...' . PHP_EOL);
    # Wait for the request to complete.
    $operation->pollUntilComplete([
        'pollingIntervalSeconds' => $pollingIntervalSeconds
    ]);

    # Print the result.
    if ($operation->operationSucceeded()) {
        $result = $operation->getResult();
        # there is only one annotation_result since only
        # one video is processed.
        $annotationResults = $result->getAnnotationResults()[0];
        $speechTranscriptions = $annotationResults ->getSpeechTranscriptions();

        foreach ($speechTranscriptions as $transcription) {
            # the number of alternatives for each transcription is limited by
            # $max_alternatives in SpeechTranscriptionConfig
            # each alternative is a different possible transcription
            # and has its own confidence score.
            foreach ($transcription->getAlternatives() as $alternative) {
                print('Alternative level information' . PHP_EOL);

                printf('Transcript: %s' . PHP_EOL, $alternative->getTranscript());
                printf('Confidence: %s' . PHP_EOL, $alternative->getConfidence());

                print('Word level information:');
                foreach ($alternative->getWords() as $wordInfo) {
                    printf(
                        '%s s - %s s: %s' . PHP_EOL,
                        $wordInfo->getStartTime()->getSeconds(),
                        $wordInfo->getEndTime()->getSeconds(),
                        $wordInfo->getWord()
                    );
                }
            }
        }
    }
    $client->close();
}