Created
August 8, 2020 16:27
-
-
Save ratulbasak/b012c542a0ffb4f7adcbfce2762c41d8 to your computer and use it in GitHub Desktop.
Video to Audio using Elastic Transcoder, Lambda with SNS Notification...
This file contains hidden or 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
'use strict'; | |
var AWS = require('aws-sdk'), | |
transcoder = new AWS.ElasticTranscoder({ | |
apiVersion: '2012-09-25', | |
region: 'ap-south-1' | |
}); | |
exports.handler = (event, context, callback) => { | |
let fileName = event.Records[0].s3.object.key; | |
var srcKey = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " ")); | |
var newKey = fileName.split('.')[0]; | |
console.log('New video has been uploaded:', fileName); | |
transcoder.createJob({ | |
PipelineId: process.env.PIPELINE_ID, | |
Input: { | |
Key: srcKey, | |
FrameRate: 'auto', | |
Resolution: 'auto', | |
AspectRatio: 'auto', | |
Interlaced: 'auto', | |
Container: 'auto' | |
}, | |
Output: { | |
Key: getOutputName(fileName), | |
ThumbnailPattern: '', | |
PresetId: '1351620000001-300040', | |
Rotate: 'auto' | |
} | |
}, function(err, data){ | |
if(err){ | |
console.log('Something went wrong:',err) | |
}else{ | |
console.log('Converting is done'); | |
} | |
callback(err, data); | |
}); | |
}; | |
function getOutputName(srcKey){ | |
let baseName = srcKey.replace('videos/',''); | |
let withOutExtension = removeExtension(baseName); | |
return 'audios/' + withOutExtension + '.mp3'; | |
} | |
function removeExtension(srcKey){ | |
let lastDotPosition = srcKey.lastIndexOf("."); | |
if (lastDotPosition === -1) return srcKey; | |
else return srcKey.substr(0, lastDotPosition); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment