Last active
August 25, 2023 03:19
-
-
Save ojczeo/1f7325b438945bb4878941fb2ebb8409 to your computer and use it in GitHub Desktop.
audiowaveform for lambda - https://github.com/bbc/audiowaveform
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
// install first binaries from http://ffxsam.s3.amazonaws.com/public/audiowaveform-lambda.zip | |
// @author Andrzej Ojczenasz https://github.com/ojczeo | |
// @version '1.0.0' | |
'use strict'; | |
process.env.PATH = process.env.PATH + ':' + process.env.LAMBDA_TASK_ROOT + '/bin'; | |
function getExtension(filename) { | |
var ext = path.extname(filename||'').split('.'); | |
return ext[ext.length - 1]; | |
}; | |
const aws = require('aws-sdk'); | |
const s3 = new aws.S3({apiVersion: '2006-03-01'}); | |
const child_process = require('child_process'); | |
const uuidv4 = require('uuid/v4'); | |
const fs = require('fs'); | |
const path = require('path'); | |
const os = require('os'); | |
exports.handler = (event, context, callback) => { | |
// vars | |
const { w, h, pps, b, ext, bucket, bg = '555555' } = event; | |
const tempDir = process.env['TEMP'] || (0, os.tmpdir)(); | |
// input | |
const key = decodeURIComponent(event.key.replace(/\+/g, ' ')); | |
const localFileName = (0, path.join)(tempDir, `${uuidv4()}.${getExtension(event.key)}`); | |
const localFile = fs.createWriteStream(localFileName); | |
// output | |
const outputKey = decodeURIComponent(event.outputKey.replace(/\+/g, ' ')); | |
const resFile = `${uuidv4()}.${ext}`; | |
const resPath = (0, path.join)(tempDir, resFile); | |
// audiowaveform args | |
const args = [ | |
'-i', localFileName, | |
'-w', w, | |
'-h', h, | |
'-c', 'audition', | |
'--no-axis-labels', | |
'--pixels-per-second', pps, | |
'--background-color', bg, | |
'-b', b, | |
'-o', resPath | |
]; | |
const cb = (error, stdout) => { | |
if (error) { | |
console.log('audiowaveform error: ', error); | |
callback(error); | |
} else { | |
console.log('audiowaveform success ', stdout); | |
fs.readFile(resPath, (err, data) => { | |
if (err) { | |
console.log('File read error: ', err); | |
callback(err); | |
} | |
const upload_params = { | |
Bucket: bucket, | |
Key: outputKey, | |
Body: data | |
}; | |
console.log('upload params: ', upload_params); | |
s3.putObject(upload_params, err => { | |
if (err) { | |
console.log('Upload error', err); | |
callback(err); | |
} | |
else { | |
console.log('Upload success'); | |
callback(null, { key: outputKey }); | |
} | |
}) | |
}); | |
} | |
}; | |
s3.getObject({ | |
Bucket: bucket, | |
Key: key, | |
}) | |
.createReadStream() | |
.on('error', (err) => { | |
console.log('createReadStream error', err); | |
callback(err); | |
}) | |
.pipe(localFile) // write to local /tmp directory | |
.on('data', (data) => { | |
console.log('data received', data); | |
}) | |
.on('finish', function(endRes) { | |
console.log(endRes); | |
(0, child_process.execFile)('audiowaveform', args, {}, cb) | |
.on('error', (err) => { | |
console.log('audiowaveform error', err); | |
callback(err); | |
}) | |
}) | |
.on('err', function(err) { | |
console.log('download error', err); | |
callback(err); | |
}) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment