Skip to content

Commit

Permalink
fix: improve convert function
Browse files Browse the repository at this point in the history
--convert is now able to be used with an actual output pattern.
  • Loading branch information
massimo-rnd committed Feb 5, 2025
1 parent 93e0267 commit 6779349
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 45 deletions.
49 changes: 10 additions & 39 deletions FFMP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Management;
using CommandLine;

class FFMP
Expand Down Expand Up @@ -223,22 +222,8 @@ static IEnumerable<string> 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)
Expand All @@ -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}";
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
}
Expand Down
5 changes: 1 addition & 4 deletions Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> FFmpegArguments { get; set; } = Enumerable.Empty<string>();
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 6779349

Please sign in to comment.