admin管理员组

文章数量:1298180

I have 2 audio files for primary and background music that I want to merge (not concatenate). The final audio file should be as long as the primary file, and if the background music is shorter then it should repeat.

If there any node js package or a library that can be used to do this?

I have 2 audio files for primary and background music that I want to merge (not concatenate). The final audio file should be as long as the primary file, and if the background music is shorter then it should repeat.

If there any node js package or a library that can be used to do this?

Share Improve this question asked Aug 5, 2015 at 12:54 bCliksbCliks 2,9787 gold badges31 silver badges52 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 4

SOX is a good utility to do audio manipulation: http://sox.sourceforge/

There are node wrappers for it:

https://github./andrewrk/node-sox

https://github./substack/node-sox

Check this https://www.npmjs./package/sox-audio

bine(method) Select the input file bining method, which can be one of the following strings: 'concatenate' 'sequence' 'mix' 'mix-power' 'merge' 'multiply'

or use this if you need a simpler one https://github./ttippin84/audio-biner

I used sox-audio, finally with this code:

import SoxCommand from "sox-audio";
const TimeFormat = SoxCommand.TimeFormat;

export const merge = async (audio, music, audioSeconds, output) => {
    try {
        const mand = SoxCommand();
        const endTime = TimeFormat.formatTimeAbsolute(audioSeconds);
        mand.input(audio).inputFileType("mp3"); 
        const trimMusic = SoxCommand().input(music).output("-p").outputFileType("mp3").trim(0, endTime);
        mand.inputSubCommand(trimMusic).output(output).outputFileType("mp3").bine("merge");
        return new Promise((resolve, reject) => {
            mand.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);
                resolve(null);
            });
            mand.on("end", function () {
                console.log("Sox mand succeeded!");
                resolve(true);
            });
            mand.run();
        });
    } catch (error) {
        console.error(error);
        return null;
    }
};

本文标签: javascriptMerge Audio filesnode jsStack Overflow