逐行解析node.js child_process的STDERR输出 [英] Parsing the STDERR output of node.js child_process line by line

查看:128
本文介绍了逐行解析node.js child_process的STDERR输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FFMPEG和Node.js编写一个简单的在线转换工具.我试图弄清楚如何解析从FFMPEG接收到的转换输出的每一行,并且仅在浏览器中显示相关结果.就我而言,我想要FFMPEG在命令行中弹出的编码时间计数器.

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();
  });     
}

最终返回的是多个数组,每个数组都包含来自上一个数组以及下一个数据块的数据.例如,函数返回数组1:{0 => A,1 => B},数组2:{0 => A,1 => B,2 => C},数组3:{0 => A,1 => B,2 => C,3 => D},依此类推.

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.

我对Node很陌生,所以我可能缺少一些简单的东西.任何指导将不胜感激!

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

推荐答案

这应该可以完成:

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

有关缓冲区的更多信息,请参见以下文档链接: http://nodejs.org/docs/v0.4.2/api/buffers.html

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

这篇关于逐行解析node.js child_process的STDERR输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆