gpt4 book ai didi

node.js - 使用 ffmpeg,我无法在 node.js spawn 上收到任何错误消息

转载 作者:行者123 更新时间:2023-12-04 23:20:50 26 4
gpt4 key购买 nike

我正在尝试从 nodejs 上的生成过程中获取错误,尝试运行 FFMPEG。我之前没有明确运行过子进程,所以我不确定这是否正确,但我从在线代码示例中收集到的内容:

    const {spawn} = require('child_process');
async function(req,res){

console.log(res.req.files.data[0].path);
var tst_loc = res.req.files.data[0].path;
try {
var the_arr = tst_loc.split('/');
the_arr.pop();
tst_loc1 = the_arr.join('/') +"/test.avi";
console.log("HERE");
var cmd = 'ffmpeg';
var tstspawn = spawn(cmd, [
'-i', tst_loc,
'-s', '800x400',
'-b:v', '64k',
'-c:v', 'avi',
'-c:a', tst_loc1,
'-o', outputfilename
], (error, stdout, stderr) => {
if (error) {
console.error('Error!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!:', stderr);
throw error;
}
console.log('Success!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!', stdout);
})

tstspawn.stderr.on('data', function(data) {
//Here is where the error output goes

console.log('stderr: ' + data);

data=data.toString();
scriptOutput+=data;
});



tstspawn.stderr.on('error',function(error){
console.log(error);
});

} catch (e) {
console.log(e.code);
console.log(e.msg);
}
仅返回路径数据的控制台日志、“HERE”的控制台日志,没有其他任何内容。我知道它没有正常运行,因为我没有得到 FFMPEG 应该输出的预期视频文件。但我无法在控制台上收到任何类型的错误消息。没有应用程序崩溃或任何东西......
谢谢你的帮助,

最佳答案

在代码到达 spawn block 之前有很多问题。

  • 命名函数,它似乎是匿名的。
  • var tst_loc = res.req.files.data[0].path; - 您只需要从 req 对象中获取路径。
  • outputfilename - 它没有在任何地方声明。
  • 如果出现错误,您不需要 try/catch 来生成进程,它将在“错误”处理程序上触发事件。
  • 您不需要 async 关键字,因为函数中不需要 await。
  •  const {spawn} = require('child_process');
    function funName (req,res){ //add function name

    console.log(req.files.data[0].path); // removed res object as it is only used to send the response back to the client

    var tst_loc = req.files.data[0].path;
    var the_arr = tst_loc.split('/');
    const outputfilename = 'NameThefile' // provide the filename
    the_arr.pop();
    tst_loc1 = the_arr.join('/') +"/test.avi";
    console.log("HERE");
    var cmd = 'ffmpeg';
    var tstspawn = spawn(cmd, [
    '-i', tst_loc,
    '-s', '800x400',
    '-b:v', '64k',
    '-c:v', 'avi',
    '-c:a', tst_loc1,
    '-o', outputfilename
    ]) // removed the callback
    tstspawn.stderr.on('data', function(data) {
    //Here is where the error output goes

    console.log('stderr: ' + data);

    data=data.toString();
    scriptOutput+=data;
    });



    tstspawn.stderr.on('error',function(error){
    console.log(error);
    });

    }

    funName(req, res);

    现在,任何错误都将流式传输到 spawn 中的错误事件。

    关于node.js - 使用 ffmpeg,我无法在 node.js spawn 上收到任何错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66727816/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com