Node.jssubprocess问题与参数 – 报价问题?,FFMPEG问题?

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

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

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

FFMPEG按预期启动,从我的audio设备录制audio,并写入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 

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

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

任何想法,为什么我不能正确指定audioinput设备的参数为FFMPEG,或者为什么FFMPEG无法识别我的audioinput设备作为subprocess运行到Node.js?

任何提示将不胜感激。

布兰登在正确的轨道上。 当在Windows命令行的参数周围使用双引号时,shell会将其剥离,程序会将其视为未加引号。 当你使用child_process.spawn()你正在绕过shell,结果程序会把引号看作是参数的一部分,而不是准备处理它们。

例如,我创build了一个小节点脚本, 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=...参数。

请注意,Windowsshell(至less从XP SP3开始)不会去掉单引号或将其用于分组,而不像Linux系统上常用的bash。 因此,如果您正在查看某人的示例bash命令行并使用单引号,则通常必须将其replace为双引号才能在Windows下工作。

正如ebolhman如此生动地解释,默认情况下,spawn函数不会创build一个shell来执行命令,因此如果你仍然想使用spawn \ spawnSync,引号不会被剥离,所有你需要做的就是传递它的参数采取以下方式

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

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