Skip to content

Commit 5aa83e1

Browse files
author
Ace Nassri
committed
Add video quickstart
1 parent bd96cde commit 5aa83e1

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

video/quickstart.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
25+
// The GCS filepath of the video to analyze
26+
const gcsUri = 'gs://nodejs-docs-samples/videointelligence_quickstart.mp4';
27+
28+
// Construct request
29+
const request = {
30+
inputUri: gcsUri,
31+
features: ['FACE_DETECTION', 'LABEL_DETECTION', 'SHOT_CHANGE_DETECTION']
32+
};
33+
34+
// Execute request
35+
video.annotateVideo(request)
36+
.then((results) => {
37+
const operation = results[0];
38+
console.log('Waiting for operation to complete... (this may take a few minutes)');
39+
return operation.promise();
40+
})
41+
.then((results) => {
42+
// Gets annotations for video
43+
const annotations = results[0].annotationResults[0];
44+
45+
// Gets faces for video from its annotations
46+
const faces = annotations.faceAnnotations;
47+
faces.forEach((face, faceIdx) => {
48+
console.log('Thumbnail size:', face.thumbnail.buffer.length);
49+
face.segments.forEach((segment, segmentIdx) => {
50+
console.log(`Track ${segmentIdx} of face ${faceIdx}: frames ${segment.startTimeOffset} to ${segment.endTimeOffset}`);
51+
});
52+
});
53+
54+
// Gets labels for video from its annotations
55+
const labels = annotations.labelAnnotations;
56+
labels.forEach((label) => {
57+
console.log('Label description:', label.description);
58+
console.log('Locations:');
59+
label.locations.forEach((location) => {
60+
console.log(`\tFrames ${location.segment.startTimeOffset} to ${location.segment.endTimeOffset}`);
61+
});
62+
});
63+
64+
// Gets shot changes for video from its annotations
65+
const shotChanges = annotations.shotAnnotations;
66+
shotChanges.forEach((shot, shotIdx) => {
67+
console.log(`Scene ${shotIdx}:`);
68+
console.log(`\tStart: ${shot.startTimeOffset}`);
69+
console.log(`\tEnd: ${shot.endTimeOffset}`);
70+
});
71+
})
72+
.catch((err) => {
73+
console.error('ERROR:', err);
74+
});
75+
// [END videointelligence_quickstart]

video/system-test/quickstart.test.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
const path = require(`path`);
19+
const test = require(`ava`);
20+
const tools = require(`@google-cloud/nodejs-repo-tools`);
21+
22+
const cmd = `node quickstart.js`;
23+
const cwd = path.join(__dirname, `..`);
24+
25+
test(`should analyze a hardcoded video`, async (t) => {
26+
const output = await tools.runAsync(cmd, cwd);
27+
t.regex(output, /Label description:/);
28+
t.regex(output, /Frames \d+ to \d+/);
29+
t.regex(output, /Track \d+ of face \d+: frames \d+ to \d+/);
30+
t.regex(output, /Scene \d+/);
31+
});

0 commit comments

Comments
 (0)