【问题标题】:Correct way to catch an error with fluent ffmpeg使用流利的 ffmpeg 捕获错误的正确方法
【发布时间】:2016-05-31 07:23:41
【问题描述】:

我在我的 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
});

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

【问题讨论】:

    标签: node.js fluent-ffmpeg


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      要捕获错误,您可以使用 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);
      

      【讨论】:

        猜你喜欢
        • 2020-06-02
        • 1970-01-01
        • 2013-03-02
        • 2021-07-26
        • 1970-01-01
        • 1970-01-01
        • 2020-04-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多