You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js Express中FFmpeg音频转换失败问题求助

Node.js Express应用中FFmpeg音频转换失败排查

我开发了一个小型Node.js Web应用,在GET请求中使用如下代码处理音频:

app.get('/api/voice/:gender/:voice/:pitch', function(req, res){
        if(req.params.pitch == "1" || req.params.pitch == "1.0") {
            const file = "./voices/" + req.params.gender.toLowerCase() + "/" + req.params.voice + ".mp3"
            res.download(file);
            return
        }

        const inputFile = "./voices/" + req.params.gender.toLowerCase() + "/" + req.params.voice + ".mp3"
        const output_file = "./Audio/Temp/" + req.params.voice + ".mp3"
        ffmpeg()
                .input(inputFile)
                .audioCodec('pcm_s16le') // Set the audio codec to PCM with 16-bit depth
                .audioFrequency(44100) // Set the sample rate
                .on('error', function(err) {
                    console.error('Error while converting:', err);
                })
                .on('end', function() {
                    // res.download(output_file)
                })
                .save(output_file);

    });

运行时出现「Conversion Failed」错误:

[2024-07-11T18:11:22.880Z] Error while converting: Error: ffmpeg exited with code 1: Conversion failed!

    at ChildProcess.<anonymous> (d:\Dev\Anima\Client\node_modules\fluent-ffmpeg\lib\processor.js:180:22)
    at ChildProcess.emit (d:\Dev\Anima\Client\lib\events.js:519:28)
    at ChildProcess._handle.onexit (d:\Dev\Anima\Client\lib\internal\child_process.js:294:12)
    at Process.callbackTrampoline (node:internal/async_hooks:130:17) {stack: 'Error: ffmpeg exited with code 1: Conversion …Trampoline (node:internal/async_hooks:130:17)', message: 'ffmpeg exited with code 1: Conversion failed!
'}

相同代码在Node.js桌面应用(.exe)中可正常运行,但在Express应用中却失败,且错误信息不够明确,请问问题出在哪里?


排查与解决方向

1. 相对路径导致文件找不到

Express启动时的工作目录和桌面应用不一致,相对路径./voices/、./Audio/Temp/可能无法定位到正确文件。改用绝对路径拼接:

const path = require('path');
// 基于当前脚本目录生成绝对路径
const inputFile = path.join(__dirname, 'voices', req.params.gender.toLowerCase(), `${req.params.voice}.mp3`);
const output_file = path.join(__dirname, 'Audio', 'Temp', `${req.params.voice}.mp3`);

2. 临时目录权限或存在性问题

Express运行的进程可能没有Audio/Temp目录的读写权限,或者目录不存在。先确保目录存在并配置权限:

const fs = require('fs');
const tempDir = path.join(__dirname, 'Audio', 'Temp');
// 递归创建目录(包括父目录)
if (!fs.existsSync(tempDir)) {
  fs.mkdirSync(tempDir, { recursive: true });
}

3. FFmpeg路径未正确配置

桌面应用可能已将FFmpeg加入系统PATH或打包了可执行文件,但Express环境无法找到FFmpeg。手动指定FFmpeg路径:

const ffmpeg = require('fluent-ffmpeg');
// 替换为你的FFmpeg实际路径
ffmpeg.setFfmpegPath('C:/your-path-to/ffmpeg.exe');

4. 获取FFmpeg详细错误日志

当前错误仅返回退出码,无法定位具体问题。监听stderr事件获取FFmpeg原生输出:

ffmpeg()
  .input(inputFile)
  .audioCodec('pcm_s16le')
  .audioFrequency(44100)
  .on('error', function(err, stdout, stderr) {
    console.error('转换错误:', err);
    console.error('FFmpeg详细日志:', stderr); // 这里会输出具体失败原因,比如文件不存在、编码不支持等
  })
  .on('end', function() {
    // res.download(output_file)
  })
  .save(output_file);

内容的提问来源于stack exchange,提问作者cuneyttyler

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 06:54:58