在Node JS中将WAV转换为mp3 [英] Convert wav to mp3 in node js

查看:311
本文介绍了在Node JS中将WAV转换为mp3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在节点js中是否有任何npn软件包将wav文件转换为mp3文件?我想在aws lambda函数中运行此脚本.我尝试了sox和sox-audio程序包,但是lambda不支持它.我在ffmpeg上进行了搜索,未发现wav到mp3之间的任何转换.

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.

有人在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日志为:

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

推荐答案

sox npm软件包只是 the sox CLI 的接口.

在错误日志中,显示为Cannot process audio: Cannot find sox.这是因为您没有安装实际的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.

为了在Lambda中包含sox CLI,请从 AWS博客:

In order to include the sox CLI in Lambda, from the AWS blog:

包含您自己的可执行文件很容易;只需将它们打包在您上载的ZIP文件中,然后在从Node.js或以前启动的其他进程中调用它们时就引用它们(包括创建的ZIP文件中的相对路径).确保在功能代码的开头包含以下内容:

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']

process.env[‘PATH’] = process.env[‘PATH’] + ‘:’ + process.env[‘LAMBDA_TASK_ROOT’]

总而言之,您要:

  1. 此处下载,
  2. 将其包含在您上传的zip中,
  3. 在节点文件的顶部包括特殊的process.env ...行,并且
  4. 运行
  1. download the sox CLI here,
  2. include it in the zip you upload,
  3. include the special process.env ... line at the top of your node file, and
  4. run

这篇关于在Node JS中将WAV转换为mp3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆