Using a pipe character | with child_process spawn

半世苍凉 提交于 2020-06-11 05:54:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!