From 6779349b62162de12317f9c9516ff4c853f3420f Mon Sep 17 00:00:00 2001 From: druffko Date: Wed, 5 Feb 2025 21:55:00 +0100 Subject: [PATCH] fix: improve convert function --convert is now able to be used with an actual output pattern. --- FFMP.cs | 49 ++++++++++--------------------------------------- Options.cs | 5 +---- README.md | 4 ++-- 3 files changed, 13 insertions(+), 45 deletions(-) diff --git a/FFMP.cs b/FFMP.cs index 453fbfa..b1a4565 100644 --- a/FFMP.cs +++ b/FFMP.cs @@ -7,7 +7,6 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; -using System.Management; using CommandLine; class FFMP @@ -223,22 +222,8 @@ static IEnumerable GetInputFiles(Options options) static async Task ProcessFile(string inputFile, Options options, string[] ffmpegArgs, ProgressBar progress, int totalFiles) { - string outputFile; + string outputFile = GenerateOutputFilePath(inputFile, options.OutputPattern); - // Determine output file for conversion - if (options.Convert) - { - var targetExtension = options.OutputFormat ?? ".mkv"; // Default to MKV if not provided - var baseFileName = Path.Combine(Path.GetDirectoryName(inputFile)!, - Path.GetFileNameWithoutExtension(inputFile)); - outputFile = $"{baseFileName}{targetExtension}"; - } - else - { - outputFile = GenerateOutputFilePath(inputFile, options.OutputPattern); - } - - // Ensure output directory exists Directory.CreateDirectory(Path.GetDirectoryName(outputFile)!); if (File.Exists(outputFile) && !options.Overwrite) @@ -249,29 +234,13 @@ static async Task ProcessFile(string inputFile, Options options, string[] ffmpeg // Build FFmpeg arguments var arguments = options.Overwrite ? "-y " : ""; - arguments += $"-i \"{Path.GetFullPath(inputFile)}\""; - - if (options.Convert) - { - arguments += $" \"{outputFile}\""; - } - else - { - arguments += $" -c:v {options.Codec}"; - if (!string.IsNullOrEmpty(options.Preset)) - { - arguments += $" -preset {options.Preset}"; - } - - if (ffmpegArgs.Any()) - { - arguments += $" {string.Join(" ", ffmpegArgs)}"; - } + arguments += $"-i \"{Path.GetFullPath(inputFile)}\" \"{outputFile}\""; - arguments += $" \"{Path.GetFullPath(outputFile)}\""; + if (ffmpegArgs.Any()) + { + arguments += $" {string.Join(" ", ffmpegArgs)}"; } - // Set log level based on verbose mode if (options.Verbose) { arguments = $"-loglevel verbose {arguments}"; @@ -297,7 +266,7 @@ static async Task ProcessFile(string inputFile, Options options, string[] ffmpeg try { process.Start(); - + // Capture and display output in verbose mode if (options.Verbose) { @@ -334,6 +303,8 @@ 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) { @@ -367,9 +338,9 @@ private static bool ValidateOptions(Options options) if (options.Convert) { // Ensure required fields for conversion - if (string.IsNullOrWhiteSpace(options.OutputFormat)) + if (string.IsNullOrWhiteSpace(options.OutputPattern)) { - Console.WriteLine("Error: The 'output-format' argument is required when using '--convert'."); + Console.WriteLine("Error: The 'output-pattern' argument is required when using '--convert'."); isValid = false; } } diff --git a/Options.cs b/Options.cs index d9dcf46..c785337 100644 --- a/Options.cs +++ b/Options.cs @@ -34,10 +34,7 @@ public class Options [Option("convert", Default = false, HelpText = "Enable conversion mode.")] public bool Convert { get; set; } - - [Option("output-format", HelpText = "Target output format (e.g., '.mkv').")] - public string? OutputFormat { get; set; } - + [Value(0, HelpText = "Arguments to pass directly to FFmpeg after '--'.")] public IEnumerable FFmpegArguments { get; set; } = Enumerable.Empty(); } \ No newline at end of file diff --git a/README.md b/README.md index eff5965..0de4e15 100644 --- a/README.md +++ b/README.md @@ -65,12 +65,12 @@ Not only can you provide directories or txt-files as sources, multiple videos ar **Converting using a directory** ```bash -dotnet path/to/FFMP.dll --convert -d "/path/to/videos/directory" --output-format .mkv +dotnet path/to/FFMP.dll --convert -d "/path/to/videos/directory" --output-pattern "/path/to/output/files/{{name}}_compressed.mkv" ``` **Converting using a txt-file** ```bash -dotnet path/to/FFMP.dll --convert -d "/path/to/videos.txt" --output-format .mkv +dotnet path/to/FFMP.dll --convert -d "/path/to/videos.txt" --output-pattern "/path/to/output/files/{{name}}_compressed.mkv" ``` If you want to see all of FFMPEGs output, just use the `--verbose` flag.