使用ffmpeg转换音频格式

converting audio formats using ffmpeg

我想使用下面的代码将 wav 文件转换为 mp3 音频:安装了 ffmpeg 和 fluent-ffmpeg。

var ffmpeg = require('fluent-ffmpeg');

function decodeAudio() {
    let track = './sources/audio.wav';//your path to source file

    ffmpeg(track)
    //.setFfmpegPath("C:\ffmpeg\bin\ffmpeg.exe")
    .toFormat('mp3')
    .on('error', (err) => {
        console.log('An error occurred: ' + err.message);
    })
    .on('progress', (progress) => {
        // console.log(JSON.stringify(progress));
        console.log('Processing: ' + progress.targetSize + ' KB converted');
    })
    .on('end', () => {
        console.log('Processing finished !');
    })
    .save('./sources/hello.mp3');//path where you want to save your file
}

使用电子我看到这个错误:

An error occurred: Cannot find ffmpeg

我找到了一个解决方案,它说如果您添加这行代码,问题就会解决:

.setFfmpegPath("C:\ffmpeg\bin\ffmpeg.exe")

但是添加这个也会产生这个错误:

An error occurred: spawn C:\ffmpeg\bin\ffmpeg.exe ENOENT

如何解决这个问题?

编辑:我使用了很多其他方法,其中 none 有效:

Recording and saving mp3 files with node.js is impossible

据我了解,您没有下载 ffmpeg 二进制文件。

安装 ffmpeg-installer 使用:

npm i @ffmpeg-installer/ffmpeg

然后将以下命令添加到您的代码中:

const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path
const ffmpeg = require('fluent-ffmpeg')
ffmpeg.setFfmpegPath(ffmpegPath)

这会解决你的问题,开心。