-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
412 lines (356 loc) · 16.5 KB
/
Program.cs
File metadata and controls
412 lines (356 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
using System.CommandLine;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using nathanbutlerDEV.cascaler.Handlers;
using nathanbutlerDEV.cascaler.Infrastructure;
using nathanbutlerDEV.cascaler.Infrastructure.Options;
using nathanbutlerDEV.cascaler.Models;
using nathanbutlerDEV.cascaler.Services;
using nathanbutlerDEV.cascaler.Services.Interfaces;
namespace nathanbutlerDEV.cascaler;
/// <summary>
/// Main program entry point with dependency injection setup.
/// </summary>
internal class Program
{
private static async Task<int> Main(string[] args)
{
try
{
// Build configuration from embedded defaults and user config
var configuration = ConfigurationHelper.BuildConfiguration();
// Setup dependency injection
var services = new ServiceCollection();
ConfigureServices(services, configuration);
var serviceProvider = services.BuildServiceProvider();
// Create and configure the command-line interface
var rootCommand = CreateRootCommand(configuration, serviceProvider);
// Create a cancellation token source for handling Ctrl+C
using var cts = new CancellationTokenSource();
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
Console.CancelKeyPress += (_, e) =>
{
e.Cancel = true;
cts.Cancel();
logger.LogInformation("Cancellation requested");
};
// Set the command handler
rootCommand.SetAction(async parseResult =>
{
var options = new ProcessingOptions
{
InputPath = parseResult.GetValue<string>("input") ?? string.Empty,
OutputPath = parseResult.GetValue<string?>("--output"),
Width = parseResult.GetValue<int?>("--width"),
Height = parseResult.GetValue<int?>("--height"),
Percent = parseResult.GetValue<int?>("--percent"),
StartWidth = parseResult.GetValue<int?>("--start-width"),
StartHeight = parseResult.GetValue<int?>("--start-height"),
StartPercent = parseResult.GetValue<int?>("--start-percent"),
DeltaX = parseResult.GetValue<double>("--deltaX"),
Rigidity = parseResult.GetValue<double>("--rigidity"),
MaxThreads = parseResult.GetValue<int>("--threads"),
ShowProgress = !parseResult.GetValue<bool>("--no-progress"),
Format = parseResult.GetValue<string?>("--format"),
// TODO: Change these to support other time formats (e.g., hh:mm:ss, mm:ss, 1800ms etc.)
Start = parseResult.GetValue<double?>("--start"),
End = parseResult.GetValue<double?>("--end"),
Duration = parseResult.GetValue<double?>("--duration"),
Fps = parseResult.GetValue<int>("--fps"),
// Video encoding options
CRF = parseResult.GetValue<int?>("--crf"),
Preset = parseResult.GetValue<string?>("--preset"),
Codec = parseResult.GetValue<string?>("--codec"),
PixelFormat = parseResult.GetValue<string?>("--pixel-format"),
Vibrato = parseResult.GetValue<bool>("--vibrato"),
ScaleBack = parseResult.GetValue<bool>("--scale-back")
};
var handler = serviceProvider.GetRequiredService<CommandHandler>();
await handler.ExecuteAsync(options, cts.Token);
});
// Invoke the command
return await rootCommand.Parse(args).InvokeAsync(cancellationToken: cts.Token);
}
catch (Exception ex)
{
// Can't use logger here as DI may have failed, use Console.Error
await Console.Error.WriteLineAsync($"Fatal error: {ex.Message}");
return 1;
}
}
/// <summary>
/// Configures dependency injection services.
/// </summary>
private static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
// Register configuration
services.AddSingleton(configuration);
// Configure logging
ConfigureLogging(services, configuration);
// Register and validate Options
services.AddOptions<FFmpegOptions>()
.Configure(options => configuration.GetSection("FFmpeg").Bind(options))
.ValidateDataAnnotations()
.ValidateOnStart();
services.AddOptions<ProcessingSettings>()
.Configure(options => configuration.GetSection("Processing").Bind(options))
.ValidateDataAnnotations()
.ValidateOnStart();
services.AddOptions<VideoEncodingOptions>()
.Configure(options => configuration.GetSection("VideoEncoding").Bind(options))
.ValidateDataAnnotations()
.ValidateOnStart();
services.AddOptions<OutputOptions>()
.Configure(options => configuration.GetSection("Output").Bind(options))
.ValidateDataAnnotations()
.ValidateOnStart();
// Configuration classes
services.AddSingleton<FFmpegConfiguration>();
// Progress bar context (needed for logging integration)
services.AddSingleton<IProgressBarContext, ProgressBarContext>();
// Services
services.AddSingleton<IImageProcessingService, ImageProcessingService>();
services.AddSingleton<IVideoProcessingService, VideoProcessingService>();
services.AddSingleton<IVideoCompilationService, VideoCompilationService>();
services.AddSingleton<IMediaProcessor, MediaProcessor>();
services.AddSingleton<IDimensionInterpolator, DimensionInterpolator>();
services.AddTransient<IProgressTracker, ProgressTracker>();
// Handlers
services.AddTransient<CommandHandler>();
services.AddTransient<ConfigCommandHandler>();
}
/// <summary>
/// Configures logging with console and file providers.
/// </summary>
private static void ConfigureLogging(IServiceCollection services, IConfiguration configuration)
{
// Ensure the log directory exists and clean up old logs
ConfigurationHelper.EnsureLogDirectoryExists();
ConfigurationHelper.CleanupOldLogs(7);
services.AddLogging(builder =>
{
// Add configuration from appsettings.json
builder.AddConfiguration(configuration.GetSection("Logging"));
// Add custom progress-bar-aware console logging - show info and above
// This will coordinate with ShellProgressBar to prevent visual conflicts
builder.Services.AddSingleton<ILoggerProvider>(serviceProvider =>
{
var progressBarContext = serviceProvider.GetRequiredService<IProgressBarContext>();
return new ProgressBarAwareConsoleLoggerProvider(progressBarContext, LogLevel.Information);
});
// Add file logging - log everything (Debug and above)
var logPath = Path.Combine(
ConfigurationHelper.GetUserLogDirectory(),
$"cascaler-{DateTime.Now:yyyyMMdd}.log"
);
builder.AddProvider(new FileLoggerProvider(logPath));
// Set the minimum level to Debug (will be filtered per the provider above)
builder.SetMinimumLevel(LogLevel.Debug);
});
}
/// <summary>
/// Creates and configures the root command with all options.
/// </summary>
private static RootCommand CreateRootCommand(IConfiguration configuration, IServiceProvider serviceProvider)
{
var rootCommand = new RootCommand("cascaler - A high-performance batch liquid rescaling tool for images.");
// Get configuration sections for default values
var processingSettings = configuration.GetSection("Processing");
var outputSettings = configuration.GetSection("Output");
var videoEncodingSettings = configuration.GetSection("VideoEncoding");
// Define options
var widthOption = new Option<int?>("--width")
{
Description = "Width of the output image",
Aliases = { "-w" }
};
var heightOption = new Option<int?>("--height")
{
Description = "Height of the output image",
Aliases = { "-h" }
};
var percentOption = new Option<int?>("--percent")
{
Description = "Percent of the output image",
Aliases = { "-p" },
DefaultValueFactory = _ => processingSettings.GetValue<int?>("DefaultScalePercent")
};
var deltaXOption = new Option<double>("--deltaX")
{
Description = "Maximum seam transversal step (0 means straight seams - 1 means curved seams)",
Aliases = { "-d" },
DefaultValueFactory = _ => processingSettings.GetValue<double>("DefaultDeltaX")
};
var rigidityOption = new Option<double>("--rigidity")
{
Description = "Introduce a bias for non-straight seams (typically 0)",
Aliases = { "-r" },
DefaultValueFactory = _ => processingSettings.GetValue<double>("DefaultRigidity")
};
var threadsOption = new Option<int>("--threads")
{
Description = "Process images in parallel",
Aliases = { "-t" },
DefaultValueFactory = _ => processingSettings.GetValue<int>("MaxImageThreads")
};
var noProgressOption = new Option<bool>("--no-progress")
{
Description = "Disable progress bar output",
DefaultValueFactory = _ => false
};
var outputOption = new Option<string?>("--output")
{
Description = "Output folder (default is input + \"-cas\")",
Aliases = { "-o" }
};
// New options for gradual scaling and video features
var formatOption = new Option<string?>("--format")
{
Description = "Output image format (png, jpg, bmp, tiff) (default is same as input for images, png for video frames)",
Aliases = { "-f" }
};
var startOption = new Option<double?>("--start")
{
Description = "Start time in seconds for video trimming"
};
var endOption = new Option<double?>("--end")
{
Description = "End time in seconds for video trimming"
};
var durationOption = new Option<double?>("--duration")
{
Description = "Duration in seconds for image sequence generation or video trimming"
};
var startWidthOption = new Option<int?>("--start-width")
{
Description = "Start width for gradual scaling",
Aliases = { "-sw" }
};
var startHeightOption = new Option<int?>("--start-height")
{
Description = "Start height for gradual scaling",
Aliases = { "-sh" }
};
var startPercentOption = new Option<int?>("--start-percent")
{
Description = "Start percent for gradual scaling",
Aliases = { "-sp" },
DefaultValueFactory = _ => processingSettings.GetValue<int?>("DefaultScalePercent") // Use the same default as --percent
};
var fpsOption = new Option<int>("--fps")
{
Description = "Frame rate for image-to-sequence conversion",
DefaultValueFactory = _ => processingSettings.GetValue<int>("DefaultFps")
};
// Video encoding options
var crfOption = new Option<int?>("--crf")
{
Description = "Video encoding quality (0-51, lower is better quality, default from config: 23)"
};
var presetOption = new Option<string?>("--preset")
{
Description = "Video encoding preset (ultrafast|superfast|veryfast|faster|fast|medium|slow|slower|veryslow, default from config: medium)"
};
var codecOption = new Option<string?>("--codec")
{
Description = "Video codec (libx264|libx265, default from config: libx264)"
};
var pixelFormatOption = new Option<string?>("--pixel-format")
{
Description = "Video pixel format (yuv420p, yuv420p10le, yuv422p10le, etc., default from config or auto-detected for HDR)"
};
var vibratoOption = new Option<bool>("--vibrato")
{
Description = "Apply vibrato and tremolo audio effects (vibrato=d=1,tremolo) to video output",
DefaultValueFactory = _ => processingSettings.GetValue<bool>("DefaultVibrato")
};
var scaleBackOption = new Option<bool>("--scale-back")
{
Description = "Scale processed frames back to original 100% dimensions (ignores start/end percent values)",
DefaultValueFactory = _ => processingSettings.GetValue<bool>("DefaultScaleBack")
};
var inputArgument = new Argument<string>("input")
{
Description = "Input image file or folder path"
};
// Add options and argument to root command
rootCommand.Add(widthOption);
rootCommand.Add(heightOption);
rootCommand.Add(percentOption);
rootCommand.Add(startWidthOption);
rootCommand.Add(startHeightOption);
rootCommand.Add(startPercentOption);
rootCommand.Add(deltaXOption);
rootCommand.Add(rigidityOption);
rootCommand.Add(threadsOption);
rootCommand.Add(noProgressOption);
rootCommand.Add(outputOption);
rootCommand.Add(formatOption);
rootCommand.Add(startOption);
rootCommand.Add(endOption);
rootCommand.Add(durationOption);
rootCommand.Add(fpsOption);
rootCommand.Add(crfOption);
rootCommand.Add(presetOption);
rootCommand.Add(codecOption);
rootCommand.Add(pixelFormatOption);
rootCommand.Add(vibratoOption);
rootCommand.Add(scaleBackOption);
rootCommand.Add(inputArgument);
// Create config subcommand
var configCommand = new Command("config", "Manage configuration settings");
// config show
var showCommand = new Command("show", "Display the current effective configuration");
showCommand.SetAction(parseResult =>
{
var handler = serviceProvider.GetRequiredService<ConfigCommandHandler>();
handler.ShowConfig();
});
configCommand.Add(showCommand);
// config path
var pathCommand = new Command("path", "Show the path to the user configuration file");
pathCommand.SetAction(parseResult =>
{
var handler = serviceProvider.GetRequiredService<ConfigCommandHandler>();
handler.ShowConfigPath();
});
configCommand.Add(pathCommand);
// config init
var initCommand = new Command("init", "Create a user configuration file with current defaults");
var initDetectFFmpegOption = new Option<bool>("--detect-ffmpeg")
{
Description = "Automatically detect and populate FFmpeg library path"
};
initCommand.Add(initDetectFFmpegOption);
initCommand.SetAction(parseResult =>
{
var detectFFmpeg = parseResult.GetValue<bool>("--detect-ffmpeg");
var handler = serviceProvider.GetRequiredService<ConfigCommandHandler>();
handler.InitConfig(detectFFmpeg);
});
configCommand.Add(initCommand);
// config export
var exportCommand = new Command("export", "Export the current configuration to a file");
var exportPathArgument = new Argument<string>("path")
{
Description = "Output file path"
};
var exportDetectFFmpegOption = new Option<bool>("--detect-ffmpeg")
{
Description = "Automatically detect and populate FFmpeg library path"
};
exportCommand.Add(exportPathArgument);
exportCommand.Add(exportDetectFFmpegOption);
exportCommand.SetAction(parseResult =>
{
var path = parseResult.GetValue<string>("path") ?? "config.json";
var detectFFmpeg = parseResult.GetValue<bool>("--detect-ffmpeg");
var handler = serviceProvider.GetRequiredService<ConfigCommandHandler>();
handler.ExportConfig(path, detectFFmpeg);
});
configCommand.Add(exportCommand);
rootCommand.Add(configCommand);
return rootCommand;
}
}