This repository was archived by the owner on Nov 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcp-video-intelligence.js
More file actions
148 lines (125 loc) · 3.84 KB
/
gcp-video-intelligence.js
File metadata and controls
148 lines (125 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/** @format */
"use strict";
const VideoIntelligence = require("@google-cloud/video-intelligence").v1p3beta1;
const S3 = require("aws-sdk/clients/s3");
const videoIntelligenceFeatures = {
LABEL_DETECTION: {
responseScope: "segmentLabelAnnotations",
},
FACE_DETECTION: {
responseScope: "faceDetectionAnnotations",
},
LOGO_RECOGNITION: {
responseScope: "logoRecognitionAnnotations",
},
PERSON_DETECTION: {
responseScope: "personDetectionAnnotations",
},
SPEECH_TRANSCRIPTION: {
responseScope: "speechTranscriptions",
},
};
const prefix =
process.env.VIDEO_INTELLIGENCE_FILE_REFIX || "gcp-video-intelligence";
const executeVideoIntelligenceFeature = async (
videoIntelligenceInstance,
s3Instance,
objectBody,
key,
bucket
) => {
try {
const videoIntelligenceFeature =
process.env.VIDEO_INTELLIGENCE_FEATURE || "LABEL_DETECTION";
const request = {
inputContent: objectBody.toString("base64"),
features: [videoIntelligenceFeature],
videoContext: {
speechTranscriptionConfig: {
languageCode: "en-US",
enableAutomaticPunctuation: true,
enableSpeakerDiarization: true,
},
},
};
if (!(videoIntelligenceFeature in videoIntelligenceFeatures)) {
throw new Error("Uknown video intelligence feature.");
}
const [operation] = await videoIntelligenceInstance.annotateVideo(request);
const results = await operation.promise();
const resultsData =
results[0].annotationResults[0][
videoIntelligenceFeatures[videoIntelligenceFeature].responseScope
];
await s3Instance
.upload({
Bucket: bucket,
Key: `${prefix}-${videoIntelligenceFeature}-${key}.json`,
Body: JSON.stringify({ results: resultsData }, null, 4),
ContentType: "application/json",
})
.promise();
} catch (error) {
throw error;
}
};
const getSourceObject = async (s3Instance, bucket, key) => {
try {
const response = await s3Instance
.getObject({
Bucket: bucket,
Key: key,
})
.promise();
return response.Body;
} catch (error) {
throw error;
}
};
const getS3Configuration = (sourceBucket) => {
return {
accessKeyId: process.env[`KOYEB_STORE_${sourceBucket}_ACCESS_KEY`],
secretAccessKey: process.env[`KOYEB_STORE_${sourceBucket}_SECRET_KEY`],
region: process.env[`KOYEB_STORE_${sourceBucket}_REGION`],
endpoint: process.env[`KOYEB_STORE_${sourceBucket}_ENDPOINT`],
};
};
const validateEnvironment = (sourceBucket) => {
if (!sourceBucket) {
throw new Error("Bucket name not present in event payload.");
}
if (
!process.env?.[`KOYEB_STORE_${sourceBucket}_ACCESS_KEY`] ||
!process.env?.[`KOYEB_STORE_${sourceBucket}_SECRET_KEY`] ||
!process.env[`KOYEB_STORE_${sourceBucket}_REGION`] ||
!process.env[`KOYEB_STORE_${sourceBucket}_ENDPOINT`]
) {
throw new Error(
`One of the following environment variables are missing: KOYEB_STORE_${sourceBucket}_ACCESS_KEY, KOYEB_STORE_${sourceBucket}_SECRET_KEY, KOYEB_STORE_${sourceBucket}_ENDPOINT, KOYEB_STORE_${sourceBucket}_REGION.`
);
}
if (!process.env.GOOGLE_APPLICATION_CREDENTIALS) {
throw new Error(
"Environment variables GOOGLE_APPLICATION_CREDENTIALS must be set."
);
}
};
const handler = async (event) => {
const bucket = event?.bucket?.name;
const key = event?.object?.key;
if (key.startsWith(prefix)) {
return;
}
validateEnvironment(bucket);
const s3Instance = new S3(getS3Configuration(bucket));
const videoIntelligenceInstance = new VideoIntelligence.VideoIntelligenceServiceClient();
const objectBody = await getSourceObject(s3Instance, bucket, key);
await executeVideoIntelligenceFeature(
videoIntelligenceInstance,
s3Instance,
objectBody,
key,
bucket
);
};
module.exports.handler = handler;