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.
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.
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.
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.
var cp = require('child_process');
var cmd = 'ffmpeg...'
var child = cp.exec(cmd, function(err, stdout, stderr) {})
child.stdin.write('q')
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With