Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a pipe character | with child_process spawn

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?

like image 523
Titan Avatar asked Mar 10 '15 16:03

Titan


People also ask

What is the use of Child_process?

js allows single-threaded, non-blocking performance but running a single thread in a CPU cannot handle increasing workload hence the child_process module can be used to spawn child processes. The child processes communicate with each other using a built-in messaging system.

How do you use the spawn process for kids?

Spawned Child Processes. The spawn function launches a command in a new process and we can use it to pass that command any arguments. For example, here's code to spawn a new process that will execute the pwd command. const { spawn } = require('child_process'); const child = spawn('pwd');

How do I run an exec in node JS?

The exec() function in Node. js creates a new shell process and executes a command in that shell. The output of the command is kept in a buffer in memory, which you can accept via a callback function passed into exec() .


1 Answers

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>];
like image 54
dmeek Avatar answered Oct 21 '22 01:10

dmeek