How to kill ffmpeg process in node.js

I am using node.js code that converts Axis Ipcamera live stream to mp4 using FFMPEG

 var childProcess=require('child_process');
 var childArguments = [];
var child=[];
var cmd='ffmpeg -i rtsp://172.24.22.117:554/axis-media/media.amp -vcodec libx264 -pix_fmt yuv420p -profile:v baseline -preset slower -crf 18 -vf "scale=trunc(in_w/2)*2:trunc(in_h/2)*2"'+' '+__dirname+'/uploads/ouput.mp4';

  child=childProcess.exec(       
        cmd,
        childArguments,
        {            
            env: process.env,
            silent:true
        },  function (err, stdout, stderr) {
            if (err) {
                throw err;
            }
            console.log(stdout);

        });     

        //    here generate events for listen child process (works properly)
    // Listen for incoming(stdout) data
    child.stdout.on('data', function (data) {
        console.log("Got data from child: " + data);
    });

    // Listen for any errors:
    child.stderr.on('data', function (data) {
        console.log('There was an error: ' + data);
    });

    // Listen for exit event
    child.on('exit', function(code) {
        console.log('Child process exited with exit code ' + code);
        child.stdout.pause();
        child.kill();
    });

my above code works fine. It gives the result as I want, but I cannot kill (stop) the ffmpeg command. I use the following code to stop the process, but in the background it is still ongoing.

child.kill("SIGTERM"); 

I also used the following commands: child.kill ('SIGUSR1'); child.kill ("SIGHUP"); child.kill ("SIGINT"); child.kill ('SIGUSR2'); for killing this process, but it does not work.

I am currently forcibly killing the node application in order to stop the ffmpeg command and generate the mp4 file. I do not want this. But I need commands that stop the ffmpeg process and generate the mp4 file without killing the node application.

+4
2

child.kill() child.on('exit'), , . child.on('exit') , .

, , , . , child.kill() .

0
var cp = require('child_process');
var cmd = 'ffmpeg...'
var child = cp.exec(cmd, function(err, stdout, stderr) {})
child.stdin.write('q')
0

All Articles