diff --git a/video-intelligence/analyze.js b/video-intelligence/analyze.js index f2f4ab10b4..17df0b6d44 100644 --- a/video-intelligence/analyze.js +++ b/video-intelligence/analyze.js @@ -269,34 +269,51 @@ async function analyzeVideoTranscription(gcsUri) { */ // const gcsUri = 'GCS URI of video to analyze, e.g. gs://my-bucket/my-video.mp4'; - const videoContext = { - speechTranscriptionConfig: { - languageCode: 'en-US', - enableAutomaticPunctuation: true, - }, - }; - - const request = { - inputUri: gcsUri, - features: ['SPEECH_TRANSCRIPTION'], - videoContext: videoContext, - }; - - const [operation] = await client.annotateVideo(request); - console.log('Waiting for operation to complete...'); - const [operationResult] = await operation.promise(); - console.log('Word level information:'); - const alternative = - operationResult.annotationResults[0].speechTranscriptions[0] - .alternatives[0]; - alternative.words.forEach(wordInfo => { - const start_time = - wordInfo.startTime.seconds + wordInfo.startTime.nanos * 1e-9; - const end_time = wordInfo.endTime.seconds + wordInfo.endTime.nanos * 1e-9; - console.log('\t' + start_time + 's - ' + end_time + 's: ' + wordInfo.word); - }); - console.log('Transcription: ' + alternative.transcript); + async function analyzeVideoTranscript() { + const videoContext = { + speechTranscriptionConfig: { + languageCode: 'en-US', + enableAutomaticPunctuation: true, + }, + }; + + const request = { + inputUri: gcsUri, + features: ['SPEECH_TRANSCRIPTION'], + videoContext: videoContext, + }; + + const [operation] = await client.annotateVideo(request); + console.log('Waiting for operation to complete...'); + const [operationResult] = await operation.promise(); + // There is only one annotation_result since only + // one video is processed. + const annotationResults = operationResult.annotationResults[0]; + + for (const speechTranscription of annotationResults.speechTranscriptions) { + // The number of alternatives for each transcription is limited by + // SpeechTranscriptionConfig.max_alternatives. + // Each alternative is a different possible transcription + // and has its own confidence score. + for (const alternative of speechTranscription.alternatives) { + console.log('Alternative level information:'); + console.log(`Transcript: ${alternative.transcript}`); + console.log(`Confidence: ${alternative.confidence}`); + + console.log('Word level information:'); + for (const wordInfo of alternative.words) { + const word = wordInfo.word; + const start_time = + wordInfo.startTime.seconds + wordInfo.startTime.nanos * 1e-9; + const end_time = + wordInfo.endTime.seconds + wordInfo.endTime.nanos * 1e-9; + console.log('\t' + start_time + 's - ' + end_time + 's: ' + word); + } + } + } + } + analyzeVideoTranscript(); // [END video_speech_transcription_gcs] }