-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathindex.js
30 lines (26 loc) · 1.14 KB
/
index.js
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
const s3Util = require('./s3-util'),
childProcessPromise = require('./child-process-promise'),
path = require('path'),
os = require('os'),
EXTENSION = process.env.EXTENSION,
THUMB_WIDTH = process.env.THUMB_WIDTH,
OUTPUT_BUCKET = process.env.OUTPUT_BUCKET,
MIME_TYPE = process.env.MIME_TYPE;
exports.handler = function (eventObject, context) {
const eventRecord = eventObject.Records && eventObject.Records[0],
inputBucket = eventRecord.s3.bucket.name,
key = eventRecord.s3.object.key,
id = context.awsRequestId,
resultKey = key.replace(/\.[^.]+$/, EXTENSION),
workdir = os.tmpdir(),
inputFile = path.join(workdir, id + path.extname(key)),
outputFile = path.join(workdir, id + EXTENSION);
console.log('converting', inputBucket, key, 'using', inputFile);
return s3Util.downloadFileFromS3(inputBucket, key, inputFile)
.then(() => childProcessPromise.spawn(
'/opt/ffmpeg/ffmpeg',
['-loglevel', 'error', '-y', '-i', inputFile, '-vf', `thumbnail,scale=${THUMB_WIDTH}:-1`, '-frames:v', '1', outputFile],
{env: process.env, cwd: workdir}
))
.then(() => s3Util.uploadFileToS3(OUTPUT_BUCKET, resultKey, outputFile, MIME_TYPE));
};