在 Node js 中连接两个音频文件

Kis*_*nti 2 audio node.js angularjs

我正在开发一个网页,用户将在其中选择两个音频文件,应用程序将连接这两个音频文件并将其作为单个输出音频文件。我在后端使用nodejs,在客户端使用angularjs。我怎样才能达到这个要求?我浏览了很多图书馆,没有一个适合它。

Pez*_*eza 5

我目前正在研究类似的用例。这些库不是很好,因为大多数库都需要在服务器环境中安装一个大程序。例子有:

  • sox-audio:只要您不需要迭代(可变数量的文件)串联,这应该没问题。但这需要安装SoX
  • audio-concat : ffmpeg的包装器,但还需要安装 ffmpeg 。

或者您不需要输出音频可搜索,您可以简单地使用流来实现。概念:

var fs = require('fs')
var writeStream = fs.createWriteStream('outputAudio.mp3'); // Or whatever you want to call it    

// Input files should be an array of the paths to the audio files you want to stitch
recursiveStreamWriter(inputFiles) {
    if(inputFiles.length == 0) {
        console.log('Done!')
        return;
    }

    let nextFile = inputFiles.shift(); 
    var readStream = fs.createReadStream(nextFile);

    readStream.pipe(writeStream, {end: false});
    readStream.on('end', () => {
        console.log('Finished streaming an audio file');
        recursiveStreamWriter(inputFiles);
    });
}
Run Code Online (Sandbox Code Playgroud)

这有效并且过渡很好,但是音频播放器很难寻找音频。可以在此处找到递归流方法的完整示例。