feat: initial whipser transcriber implementation

This commit is contained in:
Stevan Freeborn
2025-07-29 18:00:09 -05:00
parent 5ee040f2d0
commit ef45a4098a
19 changed files with 234 additions and 89 deletions
@@ -4,13 +4,13 @@ internal class DefaultCommand(
IFileSystem fileSystem,
IAnsiConsole console,
IAudioExtractor audioExtractor,
IAudioConverter audioConverter
ITranscriber transcriber
) : 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));
private readonly ITranscriber _transcriber = transcriber ?? throw new ArgumentNullException(nameof(transcriber));
internal class Settings : CommandSettings
{
@@ -63,22 +63,21 @@ internal class DefaultCommand(
_console.MarkupLine($"[blue]Audio extracted[/] [green]successfully![/]");
Stream? wavStream = null;
List<TranscriptionSegment> transcriptionSegments = [];
_console.Status()
await _console.Status()
.Spinner(Spinner.Known.Dots)
.Start("Converting audio...", ctx =>
.StartAsync("Transcribing audio...", async ctx =>
{
wavStream = _audioConverter.ConvertMp3ToWav16(audioStream);
await foreach (var segment in _transcriber.TranscribeAsync(audioStream))
{
transcriptionSegments.Add(segment);
var segmentTimeText = $"[{segment.StartTime:hh\\:mm\\:ss} - {segment.EndTime:hh\\:mm\\:ss}]";
ctx.Status($"Transcribed segment {segmentTimeText.EscapeMarkup()}");
}
});
if (wavStream is null)
{
_console.MarkupLine("[red]Failed[/] to convert audio to WAV format.");
return 2;
}
_console.MarkupLine($"[blue]Audio converted[/] [green]successfully![/]");
_console.MarkupLine($"[blue]Transcription completed[/] [green]successfully![/]");
return 0;
}
+2 -18
View File
@@ -1,20 +1,4 @@
using System.Globalization;
using System.Resources;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using FFMpegCore;
using FFMpegCore.Enums;
using FFMpegCore.Pipes;
using NAudio.Wave;
using Whisper.net;
using Whisper.net.Ggml;
Log.Logger = new LoggerConfiguration()
Log.Logger = new LoggerConfiguration()
.WriteTo.File(
formatter: new CompactJsonFormatter(),
path: Path.Combine(AppContext.BaseDirectory, "logs", "log.jsonl"),
@@ -37,7 +21,7 @@ try
services.AddSingleton(AnsiConsole.Console);
services.AddSingleton<IFileSystem, FileSystem>();
services.AddSingleton<IAudioExtractor, AudioExtractor>();
services.AddSingleton<IAudioConverter, AudioConverter>();
services.AddSingleton<ITranscriber, WhisperTranscriber>();
})
.BuildApp()
.RunAsync(args);
@@ -5,11 +5,9 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FFMpegCore" />
<PackageReference Include="Microsoft.Extensions.Configuration" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" />
<PackageReference Include="Microsoft.Extensions.Hosting" />
<PackageReference Include="NAudio" />
<PackageReference Include="Serilog" />
<PackageReference Include="Serilog.Extensions.Hosting" />
<PackageReference Include="Serilog.Formatting.Compact" />
+1
View File
@@ -16,3 +16,4 @@ global using Spectre.Console.Cli;
global using StreamShorts.Console.Commands;
global using StreamShorts.Console.Hosting;
global using StreamShorts.Library.Media.Audio;
global using StreamShorts.Library.Transcription;