0

I am trying to get the errors from a spawn process on nodejs, trying to run FFMPEG. I have not run a child process before explicitly, so I'm not sure this is right, but what I have gathered from code examples online:

    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);
  }

Returns only my console logs of the path data, the console log of the "HERE", and nothing else at all. I know it didn't run properly because I do not get the expected video file that FFMPEG should have output. But I can't get any sort of error message on my console. No crashing of the application or anything...

Thanks for any help,

rickster26ter1
  • 373
  • 3
  • 10

1 Answers1

0

There are quite a few issues before the code reaches the spawn block.

  1. Name the function, it seems anonymous.

  2. var tst_loc = res.req.files.data[0].path; - You need to take path from req object only.

  3. outputfilename - it is not declared anywhere.

  4. You don't need a try/catch for spawn process if there will be an error it will fire the event on 'error' handler.

  5. You don't need async keyword as there is no await required in the function.

 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);

Now, any error will be streamed to the error event in the spawn.

Apoorva Chikara
  • 8,277
  • 3
  • 20
  • 35