|
| 1 | +/** |
| 2 | + * Copyright 2017, Google, Inc. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +// [START videointelligence_quickstart] |
| 19 | +// Imports the Google Cloud Video Intelligence library |
| 20 | +const Video = require('@google-cloud/videointelligence').v1beta1(); |
| 21 | + |
| 22 | +// Instantiates a client |
| 23 | +const video = Video.videoIntelligenceServiceClient({ |
| 24 | + projectId: process.env.GCLOUD_PROJECT // Replace with your Google Cloud project ID |
| 25 | +}); |
| 26 | + |
| 27 | +// The GCS filepath of the video to analyze |
| 28 | +const gcsUri = 'gs://nodejs-docs-samples/videointelligence_quickstart.mp4'; |
| 29 | + |
| 30 | +// Construct request |
| 31 | +const request = { |
| 32 | + inputUri: gcsUri, |
| 33 | + features: ['FACE_DETECTION', 'LABEL_DETECTION', 'SHOT_CHANGE_DETECTION'] |
| 34 | +}; |
| 35 | + |
| 36 | +// Execute request |
| 37 | +video.annotateVideo(request) |
| 38 | + .then((results) => { |
| 39 | + const operation = results[0]; |
| 40 | + console.log('Waiting for operation to complete... (this may take a few minutes)'); |
| 41 | + return operation.promise(); |
| 42 | + }) |
| 43 | + .then((results) => { |
| 44 | + // Gets annotations for video |
| 45 | + const annotations = results[0].annotationResults[0]; |
| 46 | + |
| 47 | + // Gets faces for video from its annotations |
| 48 | + const faces = annotations.faceAnnotations; |
| 49 | + faces.forEach((face, faceIdx) => { |
| 50 | + console.log('Thumbnail size:', face.thumbnail.buffer.length); |
| 51 | + face.segments.forEach((segment, segmentIdx) => { |
| 52 | + console.log(`Track ${segmentIdx} of face ${faceIdx}: frames ${segment.startTimeOffset} to ${segment.endTimeOffset}`); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + // Gets labels for video from its annotations |
| 57 | + const labels = annotations.labelAnnotations; |
| 58 | + labels.forEach((label) => { |
| 59 | + console.log('Label description:', label.description); |
| 60 | + console.log('Locations:'); |
| 61 | + label.locations.forEach((location) => { |
| 62 | + console.log(`\tFrames ${location.segment.startTimeOffset} to ${location.segment.endTimeOffset}`); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + // Gets shot changes for video from its annotations |
| 67 | + const shotChanges = annotations.shotAnnotations; |
| 68 | + shotChanges.forEach((shot, shotIdx) => { |
| 69 | + console.log(`Scene ${shotIdx}:`); |
| 70 | + console.log(`\tStart: ${shot.startTimeOffset}`); |
| 71 | + console.log(`\tEnd: ${shot.endTimeOffset}`); |
| 72 | + }); |
| 73 | + }) |
| 74 | + .catch((err) => { |
| 75 | + console.error('ERROR:', err); |
| 76 | + }); |
| 77 | +// [END videointelligence_quickstart] |
0 commit comments