Created
January 6, 2020 04:52
-
-
Save ckxng/4be35c7b9641daad3788141499da8615 to your computer and use it in GitHub Desktop.
AWS Lambda Video Transcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// trigger: adding a file to S3 bucket | |
// result: files are transcoded | |
var aws = require('aws-sdk'); | |
var elastictranscoder = new aws.ElasticTranscoder(); | |
// return basename without extension | |
function basename(path) { | |
return path.split('/').reverse()[0].split('.')[0]; | |
} | |
// return output file name with timestamp and extension | |
function outputKey(name, ext) { | |
return name + '.' + ext; | |
} | |
exports.handler = function(event, context) { | |
console.log('Received event:', JSON.stringify(event, null, 2)); | |
var key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' ')); | |
var params = { | |
Input: { | |
Key: key | |
}, | |
PipelineId: 'XXXXXXXXXXXXXXXXXXX', /* PC Videos */ | |
Outputs: [ | |
{ | |
Key: outputKey(basename(key),'mp4'), | |
PresetId: '1351620000001-000010', /* System preset: Generic 720p */ | |
ThumbnailPattern: basename(key)+"/"+basename(key)+"_{count}", | |
} | |
] | |
}; | |
elastictranscoder.createJob(params, function(err, data) { | |
if (err){ | |
console.log(err, err.stack); // an error occurred | |
context.fail(); | |
return; | |
} | |
context.succeed(); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment