使用ffmpeg区分错误和标准终端日志-Node.js [英] Differentiate between error and standard terminal log with ffmpeg - nodejs

查看:101
本文介绍了使用ffmpeg区分错误和标准终端日志-Node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在节点js中使用ffmpeg.标准终端输出和错误似乎都发送到了stdout,所以我不知道如何区分错误和成功...这是我的代码:

I'm using ffmpeg in node js. Both the standard terminal output and the error seems to be sent to stdout, so I don't know how to differentiate between error and success... Here's my code:

var convertToMp3 = function(filePath) {
  var ffmpeg = child_process.spawn('ffmpeg',['-i', filePath, '-y', 'output.mp3']);
  var err = '';
  ffmpeg.stderr
    .on('data', function(c) { err += c; })
    .on('end', function() { console.log('stderr:', err); });
  var d = '';
  ffmpeg.stdout
    .on('data', function(c){d +=c;})
    .on('end', function(){ console.log('stdout', d); });
}

转换成功与否,stdout为空且stderr包含了如果我在终端中运行相应命令所得到的信息

wether the conversion succeeds or fails, stdout is empty and stderr contains what I'd get if I'd run the corresponding command in the terminal

推荐答案

受@aergistal注释的启发,这是一个对我有用的解决方案,其中,在任务结束时执行回调,签名为,照常.

Inspired by @aergistal comment, here is a solution that works for me, where callback is to be executed at the end of the task, with the signature function(error, success), as usual.

var convertToMp3 = function(filePath, callback) {
  var ffmpeg = child_process.spawn('ffmpeg',['-i', filePath, '-y', 'output.mp3']);
  var err = '';
  ffmpeg.stderr.on('data', function(c) { err += c; }).on('end', function() { console.log('stderr:', err); });

  ffmpeg.on('exit', function(code) {
    if (code) {
      callback({code: code, message: err});
    } else {
      callback(null, {success: true, message: err});
    }
  });
}

或采用新的js样式:

const convertToMp3 = (filePath) => new Promise((resolve, reject) {
  const ffmpeg = child_process.spawn('ffmpeg', ['-i', filePath, '-y', 'output.mp3']);
  let output = '';
  ffmpeg.stderr
    .on('data', c => { output += c; });

  ffmpeg.on('exit', code => {
    if (code) {
      reject({ code: code, message: output });
    } else {
      resolve(output);
    }
  });
});

const ffmpegOutput = await convertToMp3('./video.mp4');
...

这篇关于使用ffmpeg区分错误和标准终端日志-Node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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