2

我在我的 NodeJS 应用程序中使用 Fluent FFMpeg,并尝试在输入不存在的情况下添加一些错误处理。目前它只是因为这条消息而崩溃:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: ffmpeg exited with code 1: http://localhost:9001: Connection refused

当输入源不存在时,我想等待一段时间(比如 1 秒)然后重试。这是目前我的代码:

var command = FFmpeg("http://localhost:9001")
  // set options here
;

var stream = command.pipe();
stream.on('data', function(chunk) {
  // do something with the data
});

当输入(尚)不存在时,如何正确处理错误?

4

2 回答 2

5

您可以在“错误”处理程序中获取错误信息,例如:

stream.on('error', function(err, stdout, stderr) {
  console.log("ffmpeg stdout:\n" + stdout);
  console.log("ffmpeg stderr:\n" + stderr);
})
于 2017-11-15T12:51:20.150 回答
1

要捕获错误,您可以使用 try, catch 构造函数。以下,可能是一个可能的实现:

var FFmpeg = require('ffmpeg')

function ffmepgFunction(timeout, attempts) {
    try {
    var command = FFmpeg("http://localhost:9001");

    var stream = command.pipe();
    stream.on('data', function(chunk) {
    // do something with the data
    });
    } catch(e) {
        console.log(e);
        if(attempts > 0)
            setTimeout(() => ffmepgFunction(timeout, --attempts), timeout);
    }
}

ffmepgFunction(2000, 5);
于 2016-05-31T16:12:30.080 回答