diff --git a/packages/google-cloud-videointelligence/samples/quickstart.js b/packages/google-cloud-videointelligence/samples/quickstart.js index d09c139caa8..5adc54b73ae 100644 --- a/packages/google-cloud-videointelligence/samples/quickstart.js +++ b/packages/google-cloud-videointelligence/samples/quickstart.js @@ -14,67 +14,64 @@ */ 'use strict'; +async function main() { + // [START video_quickstart] + // Imports the Google Cloud Video Intelligence library + const videoIntelligence = require('@google-cloud/video-intelligence'); -// [START video_quickstart] -// Imports the Google Cloud Video Intelligence library -const videoIntelligence = require('@google-cloud/video-intelligence'); + // Creates a client + const client = new videoIntelligence.VideoIntelligenceServiceClient(); -// Creates a client -const client = new videoIntelligence.VideoIntelligenceServiceClient(); + // The GCS uri of the video to analyze + const gcsUri = 'gs://nodejs-docs-samples-video/quickstart_short.mp4'; -// The GCS uri of the video to analyze -const gcsUri = 'gs://nodejs-docs-samples-video/quickstart_short.mp4'; + // Construct request + const request = { + inputUri: gcsUri, + features: ['LABEL_DETECTION'], + }; -// Construct request -const request = { - inputUri: gcsUri, - features: ['LABEL_DETECTION'], -}; + // Execute request + const [operation] = await client.annotateVideo(request); -// Execute request -client - .annotateVideo(request) - .then(results => { - const operation = results[0]; - console.log( - 'Waiting for operation to complete... (this may take a few minutes)' - ); - return operation.promise(); - }) - .then(results => { - // Gets annotations for video - const annotations = results[0].annotationResults[0]; + console.log( + 'Waiting for operation to complete... (this may take a few minutes)' + ); - // Gets labels for video from its annotations - const labels = annotations.segmentLabelAnnotations; - labels.forEach(label => { - console.log(`Label ${label.entity.description} occurs at:`); - label.segments.forEach(segment => { - segment = segment.segment; - if (segment.startTimeOffset.seconds === undefined) { - segment.startTimeOffset.seconds = 0; - } - if (segment.startTimeOffset.nanos === undefined) { - segment.startTimeOffset.nanos = 0; - } - if (segment.endTimeOffset.seconds === undefined) { - segment.endTimeOffset.seconds = 0; - } - if (segment.endTimeOffset.nanos === undefined) { - segment.endTimeOffset.nanos = 0; - } - console.log( - `\tStart: ${segment.startTimeOffset.seconds}` + - `.${(segment.startTimeOffset.nanos / 1e6).toFixed(0)}s` - ); - console.log( - `\tEnd: ${segment.endTimeOffset.seconds}.` + - `${(segment.endTimeOffset.nanos / 1e6).toFixed(0)}s` - ); - }); + const [operationResult] = await operation.promise(); + + // Gets annotations for video + const annotations = operationResult.annotationResults[0]; + + // Gets labels for video from its annotations + const labels = annotations.segmentLabelAnnotations; + labels.forEach(label => { + console.log(`Label ${label.entity.description} occurs at:`); + label.segments.forEach(segment => { + segment = segment.segment; + if (segment.startTimeOffset.seconds === undefined) { + segment.startTimeOffset.seconds = 0; + } + if (segment.startTimeOffset.nanos === undefined) { + segment.startTimeOffset.nanos = 0; + } + if (segment.endTimeOffset.seconds === undefined) { + segment.endTimeOffset.seconds = 0; + } + if (segment.endTimeOffset.nanos === undefined) { + segment.endTimeOffset.nanos = 0; + } + console.log( + `\tStart: ${segment.startTimeOffset.seconds}` + + `.${(segment.startTimeOffset.nanos / 1e6).toFixed(0)}s` + ); + console.log( + `\tEnd: ${segment.endTimeOffset.seconds}.` + + `${(segment.endTimeOffset.nanos / 1e6).toFixed(0)}s` + ); }); - }) - .catch(err => { - console.error('ERROR:', err); }); -// [END video_quickstart] + // [END video_quickstart] +} + +main().catch(console.error);