Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js Child Process Issue with Args - Quotes Issue?, FFMPEG issue?

I need to be able to execute FFMPEG from my Node.js application. I believe this problem likely has to do with properly specifying command line arguments, and not specific to FFMPEG, but as I have been unable to narrow down the issue, I present my entire problem.

I can execute the following command from the command prompt successfully:

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

FFMPEG starts as expected, records audio from my audio device, and writes an MP3 file. Now, I try to do the same thing within my Node.js application:

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);
});

From within Node.js, FFMPEG fails! The error is simply:

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

Thinking that maybe for some reason this was a weird permissions error, I decided to run FFMPEG with -list_devices true from within my Node application, and sure enough, the device in question is listed:

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

Any thoughts as to why I cannot properly specify the audio input device in the arguments for FFMPEG, or why FFMPEG does not recognize my audio input device when running as a child process to Node.js?

Any hints would be most appreciated.

like image 405
Brad Avatar asked Sep 07 '12 01:09

Brad


People also ask

What version of FFmpeg is used with NodeJS?

NodeJS maintains an open child_process with the ffmpeg command that will continue until process termination: ffmpeg version 4.2.3 Copyright (c) 2000-2020 the FFmpeg developers built with Apple clang version 11.0.3 (clang-1103.0.32.59) configuration: ...

Why does FFmpeg keep aborting on node?

It's ffmpeg that's aborting, not node. An abort trap is usually a tripped assert so my guess is that ffmpeg doesn't like something about the environment it's executed in. You can try turning on core dumps with ulimit -c unlimited, then checking the core dump afterwards with lldb.

Is it possible to spin a child process in Node JS?

Node.js streams have a reputation for being hard to work with, and even harder to understand. Well I’ve got good news… We can easily spin a child process using Node’s child_process module and those child processes can easily communicate with each other with a messaging system.

What is the use of the child_process module?

The child_process module enables us to access Operating System functionalities by running any system command inside a, well, child process. We can control that child process input stream, and listen to its output stream. We can also control the arguments to be passed to the underlying OS command, and we can do whatever we want with that command ...


2 Answers

As ebolhman explained so vividly, By default, the spawn function does not create a shell to execute the command, therefore the quotes are not stripped, if you still want to use spawn \ spawnSync, all you got do to is to pass it arguments in the following way

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

The Exe itself will get the arguments without the quotes he can't handle

like image 36
Yaron Avital Avatar answered Nov 15 '22 08:11

Yaron Avital


Brandon's on the right track. When you use double quotes around arguments on the Windows command line, the shell strips them off and the program sees them unquoted. When you use child_process.spawn() you're bypassing the shell, and as a result the program sees literal quotes as part of the argument and isn't prepared to deal with them.

For example, I created a tiny node script, pargs.js, consisting only of console.log(process.argv); Running it with the same arguments you gave to FFMPEG, I get:

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>

As you can see, the shell stripped off the quotes after using them to avoid breaking the audio=... argument at the spaces.

Note that the Windows shell (at least as of XP SP3) doesn't strip off single quotes or use them for grouping, unlike, say, bash as commonly used on Linux systems. Thus if you're looking at someone's example bash command line and it uses single quotes, you'll generally have to replace them with double quotes for it to work under Windows.

like image 62
ebohlman Avatar answered Nov 15 '22 07:11

ebohlman