forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
74 lines (62 loc) · 2.86 KB
/
index.ts
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
// Copyright 2016-2019, Pulumi Corporation. All rights reserved.
import * as aws from "@pulumi/aws";
import * as awsx from "@pulumi/awsx";
// A simple cluster to run our tasks in.
const cluster = awsx.ecs.Cluster.getDefault();
// A bucket to store videos and thumbnails.
const bucket = new aws.s3.Bucket("bucket");
// Export the bucket name.
export const bucketName = bucket.id;
// A task which runs a containerized FFMPEG job to extract a thumbnail image.
const ffmpegThumbnailTask = new awsx.ecs.FargateTaskDefinition("ffmpegThumbTask", {
container: {
image: awsx.ecs.Image.fromPath("ffmpegThumbTask", "./docker-ffmpeg-thumb"),
memoryReservation: 512,
},
});
// When a new video is uploaded, run the FFMPEG task on the video file.
// Use the time index specified in the filename (e.g. cat_00-01.mp4 uses timestamp 00:01)
bucket.onObjectCreated("onNewVideo", new aws.lambda.CallbackFunction<aws.s3.BucketEvent, void>("onNewVideo", {
// Specify appropriate policies so that this AWS lambda can run EC2 tasks.
policies: [
aws.iam.AWSLambdaFullAccess, // Provides wide access to "serverless" services (Dynamo, S3, etc.)
aws.iam.AmazonEC2ContainerServiceFullAccess, // Required for lambda compute to be able to run Tasks
],
callback: async bucketArgs => {
console.log("onNewVideo called");
if (!bucketArgs.Records) {
return;
}
for (const record of bucketArgs.Records) {
console.log(`*** New video: file ${record.s3.object.key} was uploaded at ${record.eventTime}.`);
const file = record.s3.object.key;
const thumbnailFile = file.substring(0, file.indexOf("_")) + ".jpg";
const framePos = file.substring(file.indexOf("_")+1, file.indexOf(".")).replace("-", ":");
await ffmpegThumbnailTask.run({
cluster,
overrides: {
containerOverrides: [{
name: "container",
environment: [
{ name: "S3_BUCKET", value: bucketName.get() },
{ name: "INPUT_VIDEO", value: file },
{ name: "TIME_OFFSET", value: framePos },
{ name: "OUTPUT_FILE", value: thumbnailFile },
],
}],
},
});
console.log(`Running thumbnailer task.`);
}
},
}), { filterSuffix: ".mp4" });
// When a new thumbnail is created, log a message.
bucket.onObjectCreated("onNewThumbnail", async bucketArgs => {
console.log("onNewThumbnail called");
if (!bucketArgs.Records) {
return;
}
for (const record of bucketArgs.Records) {
console.log(`*** New thumbnail: file ${record.s3.object.key} was saved at ${record.eventTime}.`);
}
}, { filterSuffix: ".jpg" });