Skip to content

Instantly share code, notes, and snippets.

@Sitebase
Last active September 4, 2019 01:51
Show Gist options
  • Save Sitebase/1004af7d738929d0a7f1 to your computer and use it in GitHub Desktop.
Save Sitebase/1004af7d738929d0a7f1 to your computer and use it in GitHub Desktop.
Lambda auto transcode
{
"Records": [
{
"eventVersion": "2.0",
"eventSource": "aws:s3",
"awsRegion": "us-east-1",
"eventTime": "1970-01-01T00:00:00.000Z",
"eventName": "ObjectCreated:Put",
"userIdentity": {
"principalId": "EXAMPLE"
},
"requestParameters": {
"sourceIPAddress": "127.0.0.1"
},
"responseElements": {
"x-amz-request-id": "C3D13FE58DE4C810",
"x-amz-id-2": "FMyUVURIY8/IgAtTv8xRjskZQpcIZ9KG4V5Wp6S7S/JRWeUWerMUE5JgHvANOjpD"
},
"s3": {
"s3SchemaVersion": "1.0",
"configurationId": "testConfigRule",
"bucket": {
"name": "bubobox.test",
"ownerIdentity": {
"principalId": "EXAMPLE"
},
"arn": "arn:aws:s3:::mybucket"
},
"object": {
"key": "ted.mov",
"size": 1024,
"eTag": "d41d8cd98f00b204e9800998ecf8427e"
}
}
}
]
}
console.log('Loading function');
var aws = require('aws-sdk');
var s3 = new aws.S3({apiVersion: '2006-03-01'});
var eltr = new aws.ElasticTranscoder({
apiVersion: '2012-09-25',
region: 'us-east-1'
});
var pipelineId = '1430948749142-kbp1p4';
var webPreset = '1351620000001-100070';
exports.handler = function(event, context) {
//console.log('Received event:', JSON.stringify(event, null, 2));
// Get the object from the event and show its content type
var bucket = event.Records[0].s3.bucket.name;
var key = event.Records[0].s3.object.key;
//s3.getObject({Bucket: bucket, Key: key}, function(err, data) {
s3.headObject({Bucket: bucket, Key: key}, function(err, data) {
if (err) {
console.log("Error getting object " + key + " from bucket " + bucket +
". Make sure they exist and your bucket is in the same region as this function.");
context.fail('Error', "Error getting file: " + err);
} else {
//console.log('CONTENT TYPE:', data.ContentType);
//context.succeed();
console.log('Media type:', data.ContentType);
if (data.ContentType == 'video/quicktime') {
console.log('Found new video: ' + key + ', sending to ET');
sendVideoToET(key, function() {
context.succeed();
});
} else {
console.log('Upload ' + key + 'was not video');
console.log(JSON.stringify(data.Metadata));
}
}
});
};
function sendVideoToET(key, callback){
console.log('Sending ' + key + ' to ET');
var params = {
PipelineId: pipelineId,
OutputKeyPrefix: 'lambdatest/',
Input: {
Key: key,
FrameRate: 'auto',
Resolution: 'auto',
AspectRatio: 'auto',
Interlaced: 'auto',
Container: 'auto'
},
Output: {
Key: key + '__mp4',
ThumbnailPattern: 'thumbs-{count}',
PresetId: webPreset,
Rotate: 'auto'
}
};
eltr.createJob(params, function (err, data) {
if (err) {
console.log('Failed to send new video ' + key + ' to ET');
console.log(err);
console.log(err.stack)
context.fail('Error', "Error getting file: " + err);
} else {
console.log(data);
callback();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment