-
-
Save tigaszzz/114f24f5816f33201ac7 to your computer and use it in GitHub Desktop.
AWS Lambda Function to Trigger ElasticTranscoder Job on S3 Upload
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
console.log('Checking newly uploaded file'); | |
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' | |
}); | |
// ID of pipeline | |
var pipelineId = 'myID'; | |
// ID of ET's web output preset | |
var webMP4Preset = 'webMP4Preset'; | |
// Our custom WebM format | |
var webMPresetCustom = 'webMPreset'; | |
// Our custom ogg format | |
var oggPresetCustom = 'oggPreset'; | |
exports.handler = function(event, context) { | |
// 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; | |
if (bucket != 'my-bucket') { | |
console.log('Not my-bucket') | |
} | |
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.done('ERROR', 'error getting file' + err); | |
} | |
else { | |
if (data.Metadata.type == 'video') { | |
console.log('Found new video: ' + key + ', sending to ET'); | |
var today = new Date(); | |
var dd = today.getDate(); | |
var mm = today.getMonth() + 1; | |
var yyyy = today.getFullYear(); | |
if (dd < 10) { | |
dd = '0' + dd; | |
}; | |
if (mm < 10) { | |
mm = '0' + mm; | |
}; | |
today = dd + '-' + mm + '-' + yyyy + '/'; | |
var newKey = key.split('.')[0]; | |
var params = { | |
PipelineId: pipelineId, | |
OutputKeyPrefix: today, | |
Input: { | |
Key: key, | |
FrameRate: 'auto', | |
Resolution: 'auto', | |
AspectRatio: 'auto', | |
Interlaced: 'auto', | |
Container: 'auto' | |
}, | |
Outputs: [ | |
{ | |
Key: 'mp4-' + newKey, | |
ThumbnailPattern: 'thumbs-' + newKey + '-{count}', | |
PresetId: webMP4Preset, | |
Rotate: 'auto', | |
UserMetadata: { | |
uuid: data.Metadata.uuid, | |
video_type: 'mp4' | |
} | |
}, | |
{ | |
Key: 'webm-' + newKey, | |
ThumbnailPattern: '', | |
PresetId: webMPresetCustom, | |
Rotate: 'auto', | |
UserMetadata: { | |
uuid: data.Metadata.uuid, | |
video_type: 'webm' | |
} | |
} | |
] | |
}; | |
eltr.createJob(params, function (error, data) { | |
if (error) { | |
console.log('Failed to send new video ' + key + ' to ET'); | |
console.log(error); | |
} else { | |
console.log(data); | |
} | |
context.done(null,''); | |
}); | |
} else { | |
console.log('Upload ' + key + 'was not video'); | |
console.log(JSON.stringify(data.Metadata)); | |
} | |
} | |
} | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment