问题
I'm running nodejs on a raspberry pi and I want to run a child process to spawn a webcam stream.
Outside of node my command is:
raspivid -n -mm matrix -w 320 -h 240 -fps 18 -g 100 -t 0 -b 5000000 -o - | ffmpeg -y -f h264 -i - -c:v copy -map 0:0 -f flv -rtmp_buffer 100 -rtmp_live live "rtmp://example.com/big/test"
With child_process
I have to break each argument up
var args = ["-n", "-mm", "matrix", "-w", "320", "-h", "240", "-fps", "18", "-g", "100", "-t", "0", "-b", "5000000", "-o", "-", "|", "ffmpeg", "-y", "-f", "h264", "-i", "-", "-c:v", "copy", "-map", "0:0", "-f", "flv", "-rtmp_buffer", "100", "-rtmp_live", "live", "rtmp://example.com/big/test"];
camera.proc = child.spawn('raspivid', args);
However it chokes on the |
character:
error, exit code 64
Invalid command line option (|)
How do I use this pipe character as an argument?
回答1:
This has been answered in another question: Using two commands (using pipe |) with spawn
In summary, with child.spawn
everything in args
should be an argument of your 'raspivid' command. In your case, the pipe and everything after it are actually arguments for sh
.
A workaround is to call child.spawn('sh', args)
where args is:
var args = ['-c', <the entire command you want to run as a string>];
来源:https://stackoverflow.com/questions/28968662/using-a-pipe-character-with-child-process-spawn