Reputation: 163232
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.
Upvotes: 6
Views: 5449
Reputation: 958
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
Upvotes: 2
Reputation: 15003
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.
Upvotes: 11