使用流畅的 ffmpeg 捕获错误的正确方法

Luk*_*uke 2 node.js fluent-ffmpeg

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

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: ffmpeg exited with code 1: http://localhost:9001: Connection refused
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

小智 5

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

stream.on('error', function(err, stdout, stderr) {
  console.log("ffmpeg stdout:\n" + stdout);
  console.log("ffmpeg stderr:\n" + stderr);
})
Run Code Online (Sandbox Code Playgroud)