6

我需要能够从我的 Node.js 应用程序执行 FFMPEG。我相信这个问题可能与正确指定命令行参数有关,而不是特定于 FFMPEG,但由于我无法缩小问题范围,我提出了我的整个问题。

我可以从命令提示符成功执行以下命令:

C:\Brad\ffmpeg.exe -f dshow -i audio="Microphone (SoundMAX Integrated" testaaa.mp3

FFMPEG 按预期启动,从我的音频设备录制音频,并写入 MP3 文件。现在,我尝试在我的 Node.js 应用程序中做同样的事情

childProcess = child_process.spawn('C:\\Brad\\ffmpeg.exe', ['-f', 'dshow', '-i', 'audio="Microphone (SoundMAX Integrated"', 'testaaa.mp3']);
childProcess.stderr.on('data', function (data) {
    console.log('StdioSource received data from STDERR: ' + data);
});

在 Node.js 中,FFMPEG 失败了! 错误很简单:

[dshow @ 0000000001eded80] Could not find audio device.
audio="Microphone (SoundMAX Integrated": Input/output error

考虑到可能出于某种原因这是一个奇怪的权限错误,我决定-list_devices true在我的 Node 应用程序中运行 FFMPEG,果然,列出了有问题的设备:

[dshow @ 000000000228ecc0] DirectShow video devices
[dshow @ 000000000228ecc0] Could not enumerate video devices.
[dshow @ 000000000228ecc0] DirectShow audio devices
[dshow @ 000000000228ecc0]  "Microphone (SoundMAX Integrated"

关于为什么我无法在 FFMPEG 的参数中正确指定音频输入设备,或者为什么 FFMPEG 在作为 Node.js 的子进程运行时无法识别我的音频输入设备的任何想法?

任何提示将不胜感激。

4

2 回答 2

11

布兰登在正确的轨道上。当您在 Windows 命令行上的参数周围使用双引号时,shell 会将它们去掉,并且程序会看到它们没有被引用。当您使用时,child_process.spawn()您绕过了外壳程序,因此程序将文字引号视为参数的一部分,并且不准备处理它们。

例如,我创建了一个小节点脚本,pargs.js仅包含console.log(process.argv);使用您提供给 FFMPEG 的相同参数运行它,我得到:

C:\Documents and Settings\Eric Bohlman>node pargs -f dshow -i audio="Microphone(SoundMAX Integrated" testaaa.mp3
[ 'node',
  'C:\\Documents and Settings\\Eric Bohlman\\pargs',
  '-f',
  'dshow',
  '-i',
  'audio=Microphone (SoundMAX Integrated',
  'testaaa.mp3' ]

C:\Documents and Settings\Eric Bohlman>

如您所见,shell 在使用引号后去掉了引号,以避免audio=...在空格处破坏参数。

请注意,Windows shell(至少从 XP SP3 开始)不会去掉单引号或将它们用于分组,这与 Linux 系统上常用的 bash 不同。因此,如果您正在查看某人的示例 bash 命令行并且它使用单引号,则通常必须将它们替换为双引号才能在 Windows 下工作。

于 2012-09-07T07:40:51.893 回答
2

正如ebolhman解释的那样生动,默认情况下,spawn 函数不会创建一个 shell 来执行命令,因此不会去掉引号,如果你仍然想使用 spawn\spawnSync,你所要做的就是将它的参数传入以下方式

require('child_process').spawn('ExePathHere', arrOfArguments, { shell: true });

Exe 本身将获得没有他无法处理的引号的参数

于 2017-09-25T11:14:19.287 回答