最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Retrieving a value from a node child process - Stack Overflow

programmeradmin1浏览0评论
var fp = 'ffprobe ' + fileName + ' -show_streams | grep '
var width = exec(fp+'width', function(err, stdout, stderr){
    return stdout;
});
alert(stdout + 'random example');

how do I get the stdout 'out' of the process so that I can use it later.

var fp = 'ffprobe ' + fileName + ' -show_streams | grep '
var width = exec(fp+'width', function(err, stdout, stderr){
    return stdout;
});
alert(stdout + 'random example');

how do I get the stdout 'out' of the process so that I can use it later.

Share Improve this question edited Jul 15, 2011 at 19:49 pimvdb 155k80 gold badges311 silver badges356 bronze badges asked Jul 15, 2011 at 19:37 ConfusedCoderConfusedCoder 331 silver badge3 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 6

Node's exec function is asynchronous. This means that there is no guarantee that code below the exec call will wait until the child process finishes to run. To execute code once the process quits, then, you must provide a callback which deals with the results. Your code can branch off from there:

var fp = 'ffprobe ' + fileName + ' -show_streams | grep ';
var width = exec(fp+'width', function(err, stdout, stderr){
    console.log(stdout);

    // ... process stdout a bit ...

    afterFFProbe(stdout);
});

function afterFFProbe(output) {
    // your program continues here
}

None of the answers above worked for me. This did though.

var probeCommand = 'rtsp://xx.xx.xx.xx/axis-media/media.3gp'
exec('ffprobe '+probeCommand+' | echo ',function(err,stdout,stderr){
    console.log(stdout+stderr)
})

If I'm understanding you correctly:

var fp = 'ffprobe ' + fileName + ' -show_streams | grep ',
    value,
    width = exec(fp+'width', function(err, stdout, stderr) {
        value = stdout;
        return stdout;
    });
alert(value + 'random example');

I think this might work:

var output = "";
var fp = 'ffprobe ' + fileName + ' -show_streams | grep '
var width = exec(fp+'width', function(err, stdout, stderr){
this.output = stdout;
});
alert(output + 'random example');
发布评论

评论列表(0)

  1. 暂无评论