[英]How to kill spawned process in node.js
I have a function that needs to kill the previous ffmpeg process before spawning a new one however I'm not sure how to do this because I will understandably get ffmpeg is not defined if I attempt the code below: 我有一个函数,需要在生成一个新的ffmpeg进程之前杀死它,但是我不知道如何做到这一点,因为如果我尝试下面的代码,我会理解为没有定义ffmpeg:
var ffmpeg_started = false;
function start_ffmpeg(file) {
if (ffmpeg_started)
ffmpeg.kill();
var command = [
'-i', file,
'-f', 'mpegts',
'-codec:v', 'mpeg1video',
'http://server.com/'
];
}
const ffmpeg = spawn('ffmpeg', command);
ffmpeg_started = true;
}
An easy/simple solution is to just move the ffmpeg
declaration into the parent scope. 一个简单/简单的解决方案是将
ffmpeg
声明移动到父作用域中。 Then you could do away with the boolean flag and just check ffmpeg
's value to tell if it's still running. 然后你可以取消布尔标志,只需检查
ffmpeg
的值来判断它是否仍在运行。 For example: 例如:
var ffmpeg;
function start_ffmpeg(file) {
if (ffmpeg)
ffmpeg.kill();
ffmpeg = spawn('ffmpeg', ...);
}
If you need to anything more complex than that, you might consider using a queue or similar to handle invocations of ffmpeg more elegantly. 如果您需要比这更复杂的东西,您可以考虑使用队列或类似方法来更优雅地处理ffmpeg的调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.