Skip to content

Commit 54b9c21

Browse files
authored
Added Video Transcription sample code. (#30)
* Added Video Transcription sample code. * Code cleanup after running prettier. * Removed the ^
1 parent 3ce6787 commit 54b9c21

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

video-intelligence/analyze.js

+60
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,59 @@ function analyzeSafeSearch(gcsUri) {
355355
// [END analyze_safe_search]
356356
}
357357

358+
function analyzeVideoTranscription(gcsUri) {
359+
// [START video_speech_transcription]
360+
// Imports the Google Cloud Video Intelligence library
361+
const videoIntelligence = require('@google-cloud/video-intelligence')
362+
.v1p1beta1;
363+
364+
// Creates a client
365+
const client = new videoIntelligence.VideoIntelligenceServiceClient();
366+
367+
/**
368+
* TODO(developer): Uncomment the following line before running the sample.
369+
*/
370+
// const gcsUri = 'GCS URI of video to analyze, e.g. gs://my-bucket/my-video.mp4';
371+
372+
const videoContext = {
373+
speechTranscriptionConfig: {
374+
languageCode: 'en-US',
375+
},
376+
};
377+
378+
const request = {
379+
inputUri: gcsUri,
380+
features: ['SPEECH_TRANSCRIPTION'],
381+
videoContext: videoContext,
382+
};
383+
384+
client
385+
.annotateVideo(request)
386+
.then(results => {
387+
const operation = results[0];
388+
console.log('Waiting for operation to complete...');
389+
return operation.promise();
390+
})
391+
.then(results => {
392+
console.log('Word level information:');
393+
const alternative =
394+
results[0].annotationResults[0].speechTranscriptions[0].alternatives[0];
395+
alternative.words.forEach(wordInfo => {
396+
let start_time =
397+
wordInfo.startTime.seconds + wordInfo.startTime.nanos * 1e-9;
398+
let end_time = wordInfo.endTime.seconds + wordInfo.endTime.nanos * 1e-9;
399+
console.log(
400+
'\t' + start_time + 's - ' + end_time + 's: ' + wordInfo.word
401+
);
402+
});
403+
console.log('Transcription: ' + alternative.transcript);
404+
})
405+
.catch(err => {
406+
console.error('ERROR:', err);
407+
});
408+
// [END video_speech_transcription]
409+
}
410+
358411
require(`yargs`)
359412
.demand(1)
360413
.command(
@@ -387,11 +440,18 @@ require(`yargs`)
387440
{},
388441
opts => analyzeSafeSearch(opts.gcsUri)
389442
)
443+
.command(
444+
`transcription <gcsUri>`,
445+
`Extract the video transcription using the Cloud Video Intelligence API.`,
446+
{},
447+
opts => analyzeVideoTranscription(opts.gcsUri)
448+
)
390449
.example(`node $0 faces gs://demomaker/larry_sergey_ice_bucket_short.mp4`)
391450
.example(`node $0 shots gs://demomaker/sushi.mp4`)
392451
.example(`node $0 labels-gcs gs://demomaker/tomatoes.mp4`)
393452
.example(`node $0 labels-file cat.mp4`)
394453
.example(`node $0 safe-search gs://demomaker/tomatoes.mp4`)
454+
.example(`node $0 transcription gs://demomaker/tomatoes.mp4`)
395455
.wrap(120)
396456
.recommendCommands()
397457
.epilogue(

video-intelligence/system-test/analyze.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,15 @@ test.serial(`should analyze safe search results in a GCS file`, async t => {
8282
t.regex(output, /Time: \d+\.\d+s/);
8383
t.regex(output, /Explicit annotation results:/);
8484
});
85+
86+
// analyze_video_transcription
87+
test.serial(
88+
`should analyze video transcription results in a GCS file`,
89+
async t => {
90+
const output = await tools.runAsync(
91+
`${cmd} transcription ${shortUrl}`,
92+
cwd
93+
);
94+
t.regex(output, /over the past/);
95+
}
96+
);

0 commit comments

Comments
 (0)