Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Kill ffmpeg process in node.js

I am using node.js code that convert Axis Ipcamera live stream into 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 perfectly. It gives the output as I want, but I am not able to kill(stop) the ffmpeg command. I am using the code below for stopping the process, but in background it still continues.

child.kill("SIGTERM"); 

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

Currently I forcefully kill the node application to stop ffmpeg command and generate mp4 file. I do not want this. But I want commands that stop ffmpeg process and generate mp4 file, without killing the node application.

like image 838
Sanjay Avatar asked Feb 17 '15 13:02

Sanjay


People also ask

How do I stop ffmpeg process?

Newer versions of ffmpeg don't use 'q' anymore, at least on Ubuntu Oneiric, instead they say to press Ctrl+C to stop them. So with a newer version you can simply use 'killall -INT' to send them SIGINT instead of SIGTERM, and they should exit cleanly.

How do you kill a process in node JS?

kill() Method. The process. kill( pid[,signal] ) is an inbuilt method of node. js which sends a signal to the process, pid (which is the process id) and signal is in the string format that is the signal to send.

Which key is used to kill a process in node JS?

exit function exits from the current Node. js process. It takes an exit code, which is an integer. The process object is a global variable that lets us manage the current Node.


2 Answers

var cp = require('child_process');
var cmd = 'ffmpeg...'
var child = cp.exec(cmd, function(err, stdout, stderr) {})
child.stdin.write('q')
like image 82
Sreca Jovanov Avatar answered Oct 20 '22 20:10

Sreca Jovanov


This ended up being the solution for me:

child.stdin.end()

It works fine cross-platform. In my specific case, FFmpeg was reading from STDIN for stream data as well. SIGTERM wasn't working. Windows was able to kill with SIGTERM, but only because it doesn't actually use these signals and was forcefully killing the process.

So, you might try just ending STDIN and see if that solves it for your usage as well!

like image 1
Brad Avatar answered Oct 20 '22 19:10

Brad