sox 和 ffmpeg 组合 mp3 失败“输入文件必须具有相同的采样率”

Joh*_*hnJ 3 mp3 ffmpeg sox

我正在尝试使用以下命令通过 sox 合并 mp3 文件:

sox in.mp3 in2.mp3 out.mp3
Run Code Online (Sandbox Code Playgroud)

我得到:

sox FAIL sox: Input files must have the same sample-rate
Run Code Online (Sandbox Code Playgroud)

尝试了该-m选项,但我想这是默认的。

我也尝试通过 ffmpeg 这样做:

printf "file '%s'\n" ./*.mp3 > mylist.txt && ffmpeg -sn -f concat -safe 0 -i mylist.txt -acodec copy output.mp3
Run Code Online (Sandbox Code Playgroud)

但输出文件output.mp3有点搞砸了,它只播放第一首歌,没有其他:(

有一个优雅的解决方案吗?

任何帮助都会很棒..

slh*_*hck 6

您将必须对受影响的文件重新采样,这意味着至少对其中一些文件进行重新编码。

例如,要重新采样到 44.1 kHz:

ffmpeg -i in.mp3 -ar 44100 out.mp3
Run Code Online (Sandbox Code Playgroud)

不要忘记设置适当的编码器和比特率或 VBR


Ahm*_*abi 5

您可以使用sox工具:

sox fileSource -r 48000 fileDestination
Run Code Online (Sandbox Code Playgroud)

或者使用ffmpeg

ffmpeg -i fileSource -ar 48000 fileDestination
Run Code Online (Sandbox Code Playgroud)

如果你使用NodeJs你可以使用这个函数

 var child_process = require('child_process');

   function convertFileSampleRate(file, rate, destination, callback){
        // using sox:
        let command = 'sox' +
            ' ' + file +
            ' ' + '-r' +
            ' ' + rate +
            ' ' + destination;

        // or using ffmpeg:  ffmpeg -i file -ar 44100 destination

        child_process.exec(command, function (err, stdout, stderr) {
            if (err) {
                console.log("error " + err);
                if (callback) {
                    callback(false);
                }
                return;
            }
            if (callback) {
                callback(true);
            }
        });

    }
Run Code Online (Sandbox Code Playgroud)