Using a pipe character | with child_process spawn
Asked Answered
G

1

15

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?

Gilolo answered 10/3, 2015 at 16:11 Comment(0)
D
32

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>];
Diplopia answered 14/9, 2016 at 4:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.