Skip to content

Instantly share code, notes, and snippets.

@mrxyo
Created July 2, 2018 14:46
Show Gist options
  • Select an option

  • Save mrxyo/8300ed01c947da8310cef8c6e5296606 to your computer and use it in GitHub Desktop.

Select an option

Save mrxyo/8300ed01c947da8310cef8c6e5296606 to your computer and use it in GitHub Desktop.
transcoding
'use strict';
console.log('Loading transcoding Lambda function');
const config = {
pipeline_id: process.env.pipeline_id,
hls_1080_id: process.env.hls_1080_id,
hls_720_id: process.env.hls_720_id,
hls_480_id: process.env.hls_480_id,
hls_360_id: process.env.hls_360_id,
mp4_720_id: process.env.mp4_720_id,
hls_audio_id: process.env.hls_audio_id,
segment_length: process.env.segment_length
};
let AWS = require('aws-sdk');
let path = require('path');
let eltr = new AWS.ElasticTranscoder({
apiVersion: '2012-09-25',
region: 'eu-west-1'
});
exports.handler = function(event, context) {
let key = event.Records[0].s3.object.key;
sendVideoToElasticTranscoder(key);
};
function sendVideoToElasticTranscoder(key) {
console.log('Sending ' + key + ' to Elastic Transcoder');
// Returns new string with extension removed from key name and non alphanumeric characters replaced by underscores
let baseOutputKey = path.basename(key, path.extname(key)).toLowerCase().replace(/[^a-z0-9]+/g, '_').replace(/^_|_$/, '');
let directory = path.dirname(key);
let videoType = directory.split(path.sep).pop();
let typeResolution = 'desktop';
let outputsDefault = [
{
Key: baseOutputKey + '-1080',
ThumbnailPattern: '',
PresetId: config.hls_1080_id,
SegmentDuration: config.segment_length,
Rotate: 'auto'
},
{
Key: baseOutputKey + '-720',
ThumbnailPattern: '',
PresetId: config.hls_720_id,
SegmentDuration: config.segment_length,
Rotate: 'auto'
},
{
Key: baseOutputKey + '-480',
ThumbnailPattern: '',
PresetId: config.hls_480_id,
SegmentDuration: config.segment_length,
Rotate: 'auto'
},
{
Key: baseOutputKey + '-360',
ThumbnailPattern: '',
PresetId: config.hls_360_id,
SegmentDuration: config.segment_length,
Rotate: 'auto'
},
{
Key: baseOutputKey + '.mp4',
ThumbnailPattern: '',
PresetId: config.mp4_720_id
}
];
let outputsDesktop = [
{
Key: baseOutputKey + '-1080',
ThumbnailPattern: '',
PresetId: config.hls_1080_id,
SegmentDuration: config.segment_length,
Rotate: 'auto'
},
{
Key: baseOutputKey + '-720',
ThumbnailPattern: '',
PresetId: config.hls_720_id,
SegmentDuration: config.segment_length,
Rotate: 'auto'
},
{
Key: baseOutputKey + '.mp4',
ThumbnailPattern: '',
PresetId: config.mp4_720_id
}
];
let outputKeysDefault = [
baseOutputKey+'-1080',
baseOutputKey+'-720',
baseOutputKey+'-480',
baseOutputKey+'-360'
];
let outputKeysDesktop = [
baseOutputKey+'-1080',
baseOutputKey+'-720'
];
let params = {
PipelineId: config.pipeline_id,
Input: {
Key: key,
FrameRate: 'auto',
Resolution: 'auto',
AspectRatio: 'auto',
Interlaced: 'auto',
Container: 'auto'
},
OutputKeyPrefix: directory + '/' + baseOutputKey + '/',
Outputs: getResolution === 'desktop' ? outputsDesktop : outputsDefault,
Playlists: [{
Format: 'HLSv4',
Name: baseOutputKey,
OutputKeys: getResolution === 'desktop' ? outputKeysDesktop : outputKeysDefault,
}],
UserMetadata:{
Filename: baseOutputKey,
Type: videoType,
},
};
eltr.createJob(params, function(err, data) {
if (err) {
console.log('Failed to send new video ' + key + ' to ElasticTranscoder');
console.log(err);
console.log(err.stack)
} else {
console.log(data);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment