Skip to content

Instantly share code, notes, and snippets.

@nilesolutions
Forked from raecoo/lambda-handler.js
Created May 13, 2021 14:30
Show Gist options
  • Save nilesolutions/20383c7956f90bb90624d64a4c248466 to your computer and use it in GitHub Desktop.
Save nilesolutions/20383c7956f90bb90624d64a4c248466 to your computer and use it in GitHub Desktop.
AWS S3 + Lambda + Elastic Transcoder to encode incoming media files automagically
/*
* Copyright (C) 2016 Raecoo Cao
* See LICENSE for the copy of MIT license
*/
// https://support.google.com/youtube/answer/2853702?hl=zh-Hans
// BEGIN Lambda configuration
//
var pipelineId = '1481448705022-x1efcv'; // change to your pipeline!
// AWS elastic transcoder presets
// Ref: http://docs.aws.amazon.com/elastictranscoder/latest/developerguide/system-presets.html
var webPreset = '1351620000001-200050';
var audio = '1351620000001-200071';
var video1M = '1351620000001-200035';
var video2M = '1351620000001-200015';
// change these to match your S3 setup
// note: transcoder is picky about characters in the metadata
var copyright = '';
// BEGIN Lambda code
var aws = require('aws-sdk');
var s3 = new aws.S3();
var eltr = new aws.ElasticTranscoder({region: 'us-west-2'});
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;
var index = key.indexOf('/');
if(index < 1) {
context.fail('[*] This wasnt in a path with ID')
return
}
key = key.substr(0,index) + '/manifest.json'
console.log('[*] Requesting manifest in', bucket, 'key', key)
s3.getObject({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('[*] Received data:', data.length);
sendVideoToET(JSON.parse(data.Body.toString()), function(err) {
if(err) {
context.fail(err)
}else {
context.succeed();
}
});
}
});
};
function sendVideoToET(manifest, callback){
console.log('[*] SendVideoToET()', manifest)
var key = manifest.mediaID + '/' + manifest.media
var params = {
PipelineId: pipelineId,
OutputKeyPrefix: manifest.mediaID + '/',
Input: {
Key: key,
FrameRate: 'auto',
Resolution: 'auto',
AspectRatio: 'auto',
Interlaced: 'auto',
Container: 'auto'
},
Outputs: [
{
Key: 'hls1000k_mp4',
PresetId: video1M,
Rotate: 'auto',
SegmentDuration: '10'
},
{
Key: 'hls2000k_mp4',
PresetId: video2M,
Rotate: 'auto',
SegmentDuration: '10'
}
,{
Key: 'hls64k_m4a',
PresetId: audio,
SegmentDuration: '10'
}
],
UserMetadata: {
date: '' + manifest.date,
copyright: copyright
},
Playlists: [
{
Format: 'HLSv4',
Name: manifest.mediaID,
OutputKeys: [
'hls64k_m4a',
'hls1000k_mp4',
'hls2000k_mp4'
]
}
]
};
console.log('[*] Sending ', params,' to ET', eltr);
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)
callback("[*] Error creating job: " + err);
} else {
console.log('job submitted!')
console.log("[*] ET request data: " + data);
}
});
}
// the original media files metadata
{
"mediaID":"0daa547b24de47cab0b1fdd20fd5dad0",
"date":1441257874000,
"title":"HYBE_S1_teaser1_45s_v2",
"thumbnails":[
"0daa547b24de47cab0b1fdd20fd5dad0_small.jpg",
"0daa547b24de47cab0b1fdd20fd5dad0_large.jpg"
],
"tags":"entrepreneurship,hybe,ideas,innovation,lifestyle,startups",
"size":23825520,
"description":"Hybe is a new international media exploring the entrepreneurial lifestyle, fresh ideas and the people behind them.",
"media":"0daa547b24de47cab0b1fdd20fd5dad0_hybe_teaser_45s-v2.mp4"
}
{
"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"
}
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment