Kill ffmpeg/ffprobe processes if parent .NET application exits
More generally, if .NET process that launched ffmpeg exits, ffmpeg does not, and there's no option to make it. Most annoying when stopping the .NET app from debugger, as the app has no chance to do anything.
Now I considered contributing this, but
- would like to consult with maintainer first on how to approach it (esp. with Instances project)
- too lazy to port this to Unix-like OSes
On Windows in a similar situation this did the trick: https://gist.github.com/lostmsu/a806fbfedf7a3dba184c4e3d38dc5110
I’m trying to do something similar— I’m building a .NET Core Worker that reads a streaming schedule, and uses this (AWESOME!!) wrapper lib to handle all the commands.
I tried to use the stoppingToken provided by ExecuteAsync as the option for CancellableThrough, but stopping my debugging doesn’t stop the ffmpeg process. This is a pain because I’m an (overly) aggressive debugger & I keep needing to kill it manually.
My guess was that VS is just ending the main process and not truly “stopping” the service, thus ffmpeg continues.. but I’m not sure. Not currently at home to investigate.
Btw side note: is Console output for the child process supported? I’d like to use the built-in Logger for worker services to monitor the process.
If you are using .NET Framework, you could perhaps hook into the AppDomain.ProcessExit, you would of course need to try to include a reference to your CancellationTokenSource in order to send the cancellation. (https://stackoverflow.com/a/1119869/2573213 may help)
If using .NET core, you could hook into the IApplicationLifetime at startup and add a handler for the OnApplicationStopping event (see here for example: https://stackoverflow.com/a/74956744/2573213). Same principal of accessing the CancellationTokenSource to send the cancellation would apply