Skip to content

Commit

Permalink
samples: refactored speech transcribe sample to meet same standards a… (
Browse files Browse the repository at this point in the history
#491)

* samples: refactored speech transcribe sample to meet same standards as python

* refactored print statement

* wrapped async
  • Loading branch information
munkhuushmgl authored Nov 3, 2020
1 parent e68cabd commit a0a0212
Showing 1 changed file with 44 additions and 27 deletions.
71 changes: 44 additions & 27 deletions video-intelligence/analyze.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

Expand Down

0 comments on commit a0a0212

Please sign in to comment.