问题
Is there any npn packages to convert a wav file to mp3 file in node js? I want to run this script in aws lambda function. I tried sox and sox-audio packages, but it is not supported in lambda. I googled ffmpeg, didn't find any convertion between wav to mp3.
Does any one give a good convertion package in node js?
console.log('New Lambda Call');
var async = require('async');
var aws = require('aws-sdk');
var fs = require('fs');
var uuid = require('node-uuid');
var SoxCommand = require('sox-audio');
var sox = require('sox');
var s3 = new aws.S3({ apiVersion: '2006-03-01' });
exports.handler = function(event, context) {
//console.log('Reading event option : ', JSON.stringify(event, null, 2));
var src_bucket = event.Records[0].s3.bucket.name;
var key = event.Records[0].s3.object.key;
var src_key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));
var dest_bucket = "dmlambda";
var desk_key = src_key;
var params = {
Bucket: src_bucket,
Key: src_key
};
if (src_bucket == dest_bucket) {
console.error("Destination bucket must not match source bucket.");
return;
}
var typeMatch = src_key.match(/\.([^.]*)$/);
if (!typeMatch) {
console.error('unable to infer audio type for key ' + src_key);
return;
}
console.log()
var audioType = typeMatch[1];
if (audioType != "wav") {
console.log('skipping non wav audio ' + src_key);
return;
}
s3.getObject(params, function(err, data){
if (err) {
console.log(err);
var message = "Error getting object " + src_key + " from bucket " + src_bucket +
". Make sure they exist and your bucket is in the same region as this function.";
console.log(message);
} else {
console.log('Audio Content type:', data.ContentType);
fs.writeFile('./tmp/temp_'+src_key, data.Body, {encoding: null}, function(err){
if(err){
console.log("download file write error"+ err);
} else {
console.log('Added temp_'+src_key+' to queue');
var arr = src_key.split(".");
var filename = arr[0];
var command = SoxCommand()
.input('./tmp/temp_'+src_key)
.output('./tmp/'+filename+'.mp3')
.outputFileType('mp3')
.outputSampleRate(44100)
.outputBits(192 * 1024)
.outputChannels(2);
command.on('prepare', function(args) {
console.log('Preparing sox command with args ' + args.join(' '));
});
command.on('start', function(commandLine) {
console.log('Spawned sox with command ' + commandLine);
});
command.on('progress', function(progress) {
console.log('Processing progress: ', progress);
});
command.on('error', function(err, stdout, stderr) {
console.log('Cannot process audio: ' + err.message);
console.log('Sox Command Stdout: ', stdout);
console.log('Sox Command Stderr: ', stderr);
});
command.on('end', function() {
fs.unlink('./tmp/temp_'+src_key, function(err){
if(err){
console.log("delete error : ",err);
} else {
console.log('Moved temp_'+src_key);
}
});
console.log('Sox command succeeded!');
var stream = fs.createReadStream('./tmp/'+filename+'.mp3');
s3.putObject({
Bucket: dest_bucket,
Key: filename+'.mp3',
Body: stream
}, function(err, data){
if(err){
console.log("upload error : ",err);
} else {
fs.unlink('./tmp/'+filename+'.mp3', function(err){
if(err){
console.log("delete error : ",err);
} else {
console.log('Moved '+filename+'.mp3');
}
});
}
});
});
command.run();
}
});
}
});
};
Lambda log is :
2015-08-13T05:39:21.845Z vg7ewk1gk1bjvn30 New Lambda Call
START RequestId: a5ec9286-417d-11e5-9a79-310ba41a5a40
2015-08-13T05:39:21.990Z a5ec9286-417d-11e5-9a79-310ba41a5a40
2015-08-13T05:39:22.285Z a5ec9286-417d-11e5-9a79-310ba41a5a40 Audio Content type: audio/x-wav
2015-08-13T05:39:22.320Z a5ec9286-417d-11e5-9a79-310ba41a5a40 Added temp_RE4bc9f41b1738c6def250a58e32d473aa.wav to queue
2015-08-13T05:39:22.322Z a5ec9286-417d-11e5-9a79-310ba41a5a40 Preparing sox command with args ./tmp/temp_RE4bc9f41b1738c6def250a58e32d473aa.wav -t mp3 -r 44100 -b 196608 -c 2 ./tmp/RE4bc9f41b1738c6def250a58e32d473aa.mp3
2015-08-13T05:39:22.366Z a5ec9286-417d-11e5-9a79-310ba41a5a40 Cannot process audio: Cannot find sox
2015-08-13T05:39:22.366Z a5ec9286-417d-11e5-9a79-310ba41a5a40 Sox Command Stdout: null
2015-08-13T05:39:22.366Z a5ec9286-417d-11e5-9a79-310ba41a5a40 Sox Command Stderr:
END RequestId: a5ec9286-417d-11e5-9a79-310ba41a5a40
Process exited before completing request
回答1:
The sox npm package is just an interface to the sox CLI.
In the error log, it says Cannot process audio: Cannot find sox
. It is because you do not have the actual sox CLI installed. Install it.
In order to include the sox CLI in Lambda, from the AWS blog:
Including your own executables is easy; just package them in the ZIP file you upload, and then reference them (including the relative path within the ZIP file you created) when you call them from Node.js or from other processes that you’ve previously started. Ensure that you include the following at the start of your function code:
process.env[‘PATH’] = process.env[‘PATH’] + ‘:’ + process.env[‘LAMBDA_TASK_ROOT’]
To summarize, you want to:
- download the sox CLI here,
- include it in the zip you upload,
- include the special
process.env ...
line at the top of your node file, and - run
回答2:
Since you are already using AWS. Why don't you use elastic transcoder for this instead of Lambda. The elastic transcoder is specifically for transcoding audio/video files.
You can even trigger it based on files being put into a s3 bucket.
来源:https://stackoverflow.com/questions/31979747/convert-wav-to-mp3-in-node-js