Parsing the STDERR output of node.js child_process

2019-08-18 11:24发布

问题:

I'm writing a simple online conversion tool using FFMPEG and Node.js. I'm trying to figure out how to parse each line of the conversion output received from FFMPEG and only display pertinent results client side in the browser. In my case I want the encoding time counter that FFMPEG spits out on the command line.

My function thus far is:

function metric(ffmpeg, res) { 

  ffmpeg.stdout.on('data', function(data) {
     res.writeHead(200, {'content-type': 'text/html'});
     res.write('received upload:\n\n');
     console.log(data);
  });

  ffmpeg.stderr.on('data', function (data) {
     var temp += data.toString();
            var lines = temp.split('\n');

            //for debugging purposes
            for(var i = 0;i<lines.length;i++) {
                console.log('this is line: ' + i + '----' + lines[i]);
            }
     res.write(lines);
  });

  ffmpeg.on('exit', function (code) {
     console.log('child process exited with code ' + code);
     res.end();
  });     
}

What this ends up returning is multiple arrays, each of which includes the data from the previous array as well as the next data chunk. For example, the function returns array 1:{0=>A, 1=>B}, array 2:{0=>A, 1=>B, 2=>C}, array 3:{0=>A, 1=>B, 2=>C, 3=>D}, and so on.

I'm quite new to Node so I'm probably missing something simple. Any guidance would be much appreciated!

回答1:

This should do the job:

var buff = new Buffer(data);
console.log(buff.toString('utf8'));

For more information on buffers, here is a link to the doc: http://nodejs.org/docs/v0.4.2/api/buffers.html