@@ -269,34 +269,51 @@ async function analyzeVideoTranscription(gcsUri) {
269269 */
270270 // const gcsUri = 'GCS URI of video to analyze, e.g. gs://my-bucket/my-video.mp4';
271271
272- const videoContext = {
273- speechTranscriptionConfig : {
274- languageCode : 'en-US' ,
275- enableAutomaticPunctuation : true ,
276- } ,
277- } ;
278-
279- const request = {
280- inputUri : gcsUri ,
281- features : [ 'SPEECH_TRANSCRIPTION' ] ,
282- videoContext : videoContext ,
283- } ;
284-
285- const [ operation ] = await client . annotateVideo ( request ) ;
286- console . log ( 'Waiting for operation to complete...' ) ;
287- const [ operationResult ] = await operation . promise ( ) ;
288- console . log ( 'Word level information:' ) ;
289- const alternative =
290- operationResult . annotationResults [ 0 ] . speechTranscriptions [ 0 ]
291- . alternatives [ 0 ] ;
292- alternative . words . forEach ( wordInfo => {
293- const start_time =
294- wordInfo . startTime . seconds + wordInfo . startTime . nanos * 1e-9 ;
295- const end_time = wordInfo . endTime . seconds + wordInfo . endTime . nanos * 1e-9 ;
296- console . log ( '\t' + start_time + 's - ' + end_time + 's: ' + wordInfo . word ) ;
297- } ) ;
298- console . log ( 'Transcription: ' + alternative . transcript ) ;
272+ async function analyzeVideoTranscript ( ) {
273+ const videoContext = {
274+ speechTranscriptionConfig : {
275+ languageCode : 'en-US' ,
276+ enableAutomaticPunctuation : true ,
277+ } ,
278+ } ;
279+
280+ const request = {
281+ inputUri : gcsUri ,
282+ features : [ 'SPEECH_TRANSCRIPTION' ] ,
283+ videoContext : videoContext ,
284+ } ;
285+
286+ const [ operation ] = await client . annotateVideo ( request ) ;
287+ console . log ( 'Waiting for operation to complete...' ) ;
288+ const [ operationResult ] = await operation . promise ( ) ;
289+ // There is only one annotation_result since only
290+ // one video is processed.
291+ const annotationResults = operationResult . annotationResults [ 0 ] ;
292+
293+ for ( const speechTranscription of annotationResults . speechTranscriptions ) {
294+ // The number of alternatives for each transcription is limited by
295+ // SpeechTranscriptionConfig.max_alternatives.
296+ // Each alternative is a different possible transcription
297+ // and has its own confidence score.
298+ for ( const alternative of speechTranscription . alternatives ) {
299+ console . log ( 'Alternative level information:' ) ;
300+ console . log ( `Transcript: ${ alternative . transcript } ` ) ;
301+ console . log ( `Confidence: ${ alternative . confidence } ` ) ;
302+
303+ console . log ( 'Word level information:' ) ;
304+ for ( const wordInfo of alternative . words ) {
305+ const word = wordInfo . word ;
306+ const start_time =
307+ wordInfo . startTime . seconds + wordInfo . startTime . nanos * 1e-9 ;
308+ const end_time =
309+ wordInfo . endTime . seconds + wordInfo . endTime . nanos * 1e-9 ;
310+ console . log ( '\t' + start_time + 's - ' + end_time + 's: ' + word ) ;
311+ }
312+ }
313+ }
314+ }
299315
316+ analyzeVideoTranscript ( ) ;
300317 // [END video_speech_transcription_gcs]
301318}
302319
0 commit comments