Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: killing ffmpeg spawned by ffmp on windows #38

Merged
merged 1 commit into from
Feb 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 8 additions & 42 deletions FFMP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
};
Expand All @@ -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);
});
}
Expand All @@ -108,7 +79,7 @@ static void Main(string[] args)
}
}

private static readonly ConcurrentBag<Process> TrackedProcesses = new ConcurrentBag<Process>();
private static readonly ConcurrentBag<int> TrackedProcessIds = new ConcurrentBag<int>();

static async Task Run(Options options, string[] ffmpegArgs)
{
Expand Down Expand Up @@ -219,8 +190,7 @@ static IEnumerable<string> GetInputFiles(Options options)
return Enumerable.Empty<string>();
}

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);

Expand All @@ -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}\"";

Expand Down Expand Up @@ -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(() =>
Expand All @@ -290,7 +259,6 @@ static async Task ProcessFile(string inputFile, Options options, string[] ffmpeg
}
else
{
// Wait silently for process to finish
await process.WaitForExitAsync();
}

Expand All @@ -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)
{
Expand Down