feat: refactor audio extraction to reusable class

This commit is contained in:
Stevan Freeborn
2025-07-24 11:22:15 -05:00
parent 310f56fb84
commit 0a661d6970
14 changed files with 354 additions and 181 deletions
@@ -2,11 +2,13 @@ namespace StreamShorts.Console.Commands;
internal class DefaultCommand(
IFileSystem fileSystem,
IAnsiConsole console
IAnsiConsole console,
IAudioExtractor audioExtractor
) : AsyncCommand<DefaultCommand.Settings>
{
private readonly IFileSystem _fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
private readonly IAnsiConsole _console = console ?? throw new ArgumentNullException(nameof(console));
private readonly IAudioExtractor _audioExtractor = audioExtractor ?? throw new ArgumentNullException(nameof(audioExtractor));
internal class Settings : CommandSettings
{
@@ -37,9 +39,28 @@ internal class DefaultCommand(
return base.Validate(context, settings);
}
public override Task<int> ExecuteAsync(CommandContext context, Settings settings)
public override async Task<int> ExecuteAsync(CommandContext context, Settings settings)
{
_console.Write($"[green]Processing stream:[/] {settings.Stream}");
return Task.FromResult(0);
_console.MarkupLine($"[blue]Processing stream:[/] {settings.Stream}");
var videoStream = _fileSystem.File.OpenRead(settings.Stream);
Stream? audioStream = null;
await _console.Status()
.Spinner(Spinner.Known.Dots)
.StartAsync("Extracting audio...", async ctx =>
{
audioStream = await _audioExtractor.ExtractMp3FromMp4Async(videoStream).ConfigureAwait(false);
});
if (audioStream is null)
{
_console.MarkupLine("[red]Failed[/] to extract audio from the stream.");
return 1;
}
_console.MarkupLine($"[blue]Audio extracted[/] [green]successfully![/]");
return 0;
}
}