feat: extract audio conversion to separate class and refine audio extraction
This commit is contained in:
@@ -3,12 +3,14 @@ namespace StreamShorts.Console.Commands;
|
||||
internal class DefaultCommand(
|
||||
IFileSystem fileSystem,
|
||||
IAnsiConsole console,
|
||||
IAudioExtractor audioExtractor
|
||||
IAudioExtractor audioExtractor,
|
||||
IAudioConverter audioConverter
|
||||
) : 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));
|
||||
private readonly IAudioConverter _audioConverter = audioConverter ?? throw new ArgumentNullException(nameof(audioConverter));
|
||||
|
||||
internal class Settings : CommandSettings
|
||||
{
|
||||
@@ -61,6 +63,23 @@ internal class DefaultCommand(
|
||||
|
||||
_console.MarkupLine($"[blue]Audio extracted[/] [green]successfully![/]");
|
||||
|
||||
Stream? wavStream = null;
|
||||
|
||||
_console.Status()
|
||||
.Spinner(Spinner.Known.Dots)
|
||||
.Start("Converting audio...", ctx =>
|
||||
{
|
||||
wavStream = _audioConverter.ConvertMp3ToWav16(audioStream);
|
||||
});
|
||||
|
||||
if (wavStream is null)
|
||||
{
|
||||
_console.MarkupLine("[red]Failed[/] to convert audio to WAV format.");
|
||||
return 2;
|
||||
}
|
||||
|
||||
_console.MarkupLine($"[blue]Audio converted[/] [green]successfully![/]");
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,7 @@ try
|
||||
services.AddSingleton(AnsiConsole.Console);
|
||||
services.AddSingleton<IFileSystem, FileSystem>();
|
||||
services.AddSingleton<IAudioExtractor, AudioExtractor>();
|
||||
services.AddSingleton<IAudioConverter, AudioConverter>();
|
||||
})
|
||||
.BuildApp()
|
||||
.RunAsync(args);
|
||||
@@ -53,25 +54,6 @@ finally
|
||||
await Log.CloseAndFlushAsync();
|
||||
}
|
||||
|
||||
// using var mp3Stream = new MemoryStream();
|
||||
// using var mp4Stream = new FileStream(args[0], FileMode.Open, FileAccess.Read);
|
||||
|
||||
// var wasExtracted = await FFMpegArguments
|
||||
// .FromPipeInput(new StreamPipeSource(mp4Stream))
|
||||
// .OutputToPipe(
|
||||
// new StreamPipeSink(mp3Stream),
|
||||
// o => o.DisableChannel(Channel.Video).ForceFormat("mp3")
|
||||
// )
|
||||
// .ProcessAsynchronously();
|
||||
|
||||
// // Step 2: Convert MP3 stream to 16khz wave format
|
||||
// mp3Stream.Position = 0;
|
||||
// using var reader = new Mp3FileReader(mp3Stream);
|
||||
// var outFormat = new WaveFormat(16000, reader.WaveFormat.Channels);
|
||||
// using var resampler = new MediaFoundationResampler(reader, outFormat);
|
||||
// using var waveStream = new MemoryStream();
|
||||
// WaveFileWriter.WriteWavFileToStream(waveStream, resampler);
|
||||
|
||||
// // Step 3: Split the wave stream into 2 minute segments
|
||||
// waveStream.Position = 0;
|
||||
// var segmentDuration = TimeSpan.FromMinutes(2);
|
||||
|
||||
Reference in New Issue
Block a user