1

I am trying to read in large volumes of binary data from a stream (created form a child process) in Node. I am using the stdout.on('data') event for my process to get the data.
No matter how much I search, I can't seem to find any docs on the arguments that are passed in to the callback. typeof(data) says it's an object, but from what I can tell, it is just an array.

Is it an array? Or a string? Whatever it is, how can I get an array of bytes out?
Here's my code:

var childProc = require('child_process'),
    spawn = childProc.spawn;

// /home/ubuntu/bin/ffmpeg -f video4linux2 -r 1 -s 640x480 -i /dev/video0 -c:v rawvideo -f rawvideo -pix_fmt rgb8 -

var streamProc = spawn('/home/ubuntu/bin/ffmpeg', [
    '-f', 'video4linux2',
    '-r', '1',
    '-s', '640x480',
    '-i', '/dev/video0',
    '-c:v', 'rawvideo',
    '-f', 'rawvideo',
    '-pix_fmt', 'rgb24',
    '-'
    ]);

streamProc.stdout.on('data', function(data) {

    var bytes = [];

    //Do some magical conversion here to populate the array

    //debugger;
    console.log('OUT: ' + bytes.join()); //Just some debugging steps
});

streamProc.stderr.on('data', function(data) {
    //debugger;
    if(process.argv.indexOf('showerr') != -1)
        console.log('ERR: ' + data);
});

streamProc.on('close',function(exitCode) {
    console.log('EXT: Program exited with code ' + exitCode);
});

Am I missing something obvious?

1 Answer 1

1

It's Buffer instance

Also take a look at NodeBasicFFMPEG, ffmpeg-node, node-fluent-ffmpeg

Note that "data" event gives you just a chunk of data, you might need to re-assemble it yourself using something like bufferlist library

Sign up to request clarification or add additional context in comments.

5 Comments

I can take a closer look, but I already looked at most of the node modules for FFMPEG. I would need one that works on an ARM machine, and I decided it would be easier to just write it out as a child process.
This might just be me being stupid, but shouldn't typeof(data) return 'Buffer' if it was created internally from a class?
data.constructor.name == 'Buffer'. typeof always return 'object' for objects, try it with function F() { this.foo = 1; }; f = new F(); console.log(typeof(f))
I see... I made the assumption that typeof would give me the same thing as some debuggers do... my mistake! As a side note, any idea if there is an output limit on a child process? It stops the stream of data half way through one burst; and then it stops sending data altogether.
There is no limit afaik. For example, I use socat process to connect to unix abstract socket and can exchange megabytes without a problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.