From 738c5cbd3d5198095e9af29dcd0bd31374617a55 Mon Sep 17 00:00:00 2001 From: druffko Date: Sat, 8 Feb 2025 16:14:06 +0100 Subject: [PATCH] fix: killing ffmpeg spawned by ffmp on windows --- FFMP.cs | 50 ++++++++------------------------------------------ 1 file changed, 8 insertions(+), 42 deletions(-) diff --git a/FFMP.cs b/FFMP.cs index b1a4565..eca5257 100644 --- a/FFMP.cs +++ b/FFMP.cs @@ -17,7 +17,6 @@ static void Main(string[] args) { Console.WriteLine("Starting application..."); - // Split args into application and FFmpeg arguments var appArgs = args.TakeWhile(arg => arg != "--").ToArray(); var ffmpegArgs = args.SkipWhile(arg => arg != "--").Skip(1).ToArray(); @@ -30,47 +29,22 @@ static void Main(string[] args) Console.CancelKeyPress += (sender, e) => { Console.WriteLine("Terminating processes..."); - foreach (var process in TrackedProcesses.ToList()) + foreach (var processId in TrackedProcessIds.ToList()) { try { + var process = Process.GetProcessById(processId); if (process != null && !process.HasExited) { process.Kill(); Console.WriteLine($"Terminated FFmpeg process with ID {process.Id}"); } } - catch (InvalidOperationException ex) - { - Console.WriteLine($"Process already terminated or invalid: {ex.Message}"); - } - } - - // Additional fix for Windows: Kill all FFmpeg processes by name - if (Environment.OSVersion.Platform == PlatformID.Win32NT) - { - try - { - var ffmpegProcesses = Process.GetProcessesByName("ffmpeg"); - foreach (var ffmpegProcess in ffmpegProcesses) - { - try - { - ffmpegProcess.Kill(); - Console.WriteLine($"Force-killed FFmpeg process with ID {ffmpegProcess.Id}"); - } - catch (Exception ex) - { - Console.WriteLine($"Error killing FFmpeg process {ffmpegProcess.Id}: {ex.Message}"); - } - } - } catch (Exception ex) { - Console.WriteLine($"Error retrieving FFmpeg processes: {ex.Message}"); + Console.WriteLine($"Error terminating process {processId}: {ex.Message}"); } } - e.Cancel = true; Environment.Exit(0); }; @@ -89,15 +63,12 @@ static void Main(string[] args) }) .WithNotParsed(errors => { - // Check if the user requested help/version information if (errors.Any(error => error is CommandLine.HelpRequestedError || error is CommandLine.VersionRequestedError)) { - Environment.Exit(0); // Exit without error message + Environment.Exit(0); } - // Otherwise, print an actual error message Console.WriteLine("Error: Invalid arguments provided."); - Environment.Exit(1); }); } @@ -108,7 +79,7 @@ static void Main(string[] args) } } - private static readonly ConcurrentBag TrackedProcesses = new ConcurrentBag(); + private static readonly ConcurrentBag TrackedProcessIds = new ConcurrentBag(); static async Task Run(Options options, string[] ffmpegArgs) { @@ -219,8 +190,7 @@ static IEnumerable GetInputFiles(Options options) return Enumerable.Empty(); } - static async Task ProcessFile(string inputFile, Options options, string[] ffmpegArgs, ProgressBar progress, - int totalFiles) + static async Task ProcessFile(string inputFile, Options options, string[] ffmpegArgs, ProgressBar progress, int totalFiles) { string outputFile = GenerateOutputFilePath(inputFile, options.OutputPattern); @@ -232,7 +202,6 @@ static async Task ProcessFile(string inputFile, Options options, string[] ffmpeg return; } - // Build FFmpeg arguments var arguments = options.Overwrite ? "-y " : ""; arguments += $"-i \"{Path.GetFullPath(inputFile)}\" \"{outputFile}\""; @@ -266,8 +235,8 @@ static async Task ProcessFile(string inputFile, Options options, string[] ffmpeg try { process.Start(); - - // Capture and display output in verbose mode + TrackedProcessIds.Add(process.Id); + if (options.Verbose) { var outputTask = Task.Run(() => @@ -290,7 +259,6 @@ static async Task ProcessFile(string inputFile, Options options, string[] ffmpeg } else { - // Wait silently for process to finish await process.WaitForExitAsync(); } @@ -303,8 +271,6 @@ static async Task ProcessFile(string inputFile, Options options, string[] ffmpeg { Console.WriteLine($"FFmpeg process exited with code {process.ExitCode} for file: {inputFile}"); } - - await process.WaitForExitAsync(); } catch (Exception ex) {