【问题标题】:Node.js Child Process Issue with Args - Quotes Issue?, FFMPEG issue?带有 Args 的 Node.js 子进程问题 - 引号问题?,FFMPEG 问题?
【发布时间】:2012-08-31 21:16:35
【问题描述】:

我需要能够从我的 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

考虑到可能出于某种原因这是一个奇怪的权限错误,我决定在我的 Node 应用程序中使用 -list_devices true 运行 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 的子进程运行时无法识别我的音频输入设备的任何想法?

任何提示将不胜感激。

【问题讨论】:

  • 出于好奇,如果去掉引号会得到什么,例如:..., 'audio=Microphone (SoundMAX Integrated', ...
  • @BrandonTilley,成功了,谢谢!!

标签: node.js command-line process ffmpeg command-line-arguments


【解决方案1】:

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

例如,我创建了一个小节点脚本 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 下工作。

【讨论】:

  • 哈哈,我也是——这完全是一种预感,因为您通常只需要引号来防止 shell 将字符串的各个部分解释为不同的参数。
【解决方案2】:

正如ebolhman 解释得那么生动,默认情况下,spawn 函数不会创建 shell 来执行命令,因此不会剥离引号,如果你仍然想使用 spawn \ spawnSync,你要做的就是通过以下方式传递参数

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

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

【讨论】:

    猜你喜欢
    • 2012-07-06
    • 1970-01-01
    • 2011-04-01
    • 2010-09-12
    • 1970-01-01
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多