Skip to content

Commit

Permalink
1.0.1
Browse files Browse the repository at this point in the history
- Auto download FFmpeg to program directory.
  • Loading branch information
azhuge233 committed Jun 22, 2023
1 parent 5244505 commit b5f4539
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 52 deletions.
3 changes: 2 additions & 1 deletion ConsoleWhisper/ConsoleWhisper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<PropertyGroup>
<AssemblyName>ConsoleWhisper</AssemblyName>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
<Authors>azhuge233</Authors>
</PropertyGroup>

Expand All @@ -21,6 +21,7 @@
<PackageReference Include="Whisper.net" Version="1.4.5" />
<PackageReference Include="Whisper.net.Runtime" Version="1.4.5" />
<PackageReference Include="Xabe.FFmpeg" Version="5.2.6" />
<PackageReference Include="Xabe.FFmpeg.Downloader" Version="5.2.6" />
</ItemGroup>

<Target Name="CreateModelDirectoryAfterBuild" AfterTargets="Build">
Expand Down
20 changes: 15 additions & 5 deletions ConsoleWhisper/Module/FileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

namespace ConsoleWhisper.Module {
internal static class FileHelper {
internal static readonly string ModelDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Model");
internal static readonly string AppDirectory = AppDomain.CurrentDomain.BaseDirectory;
internal static readonly string FFmpegLocation = Path.Combine(AppDirectory, "ffmpeg.exe");
internal static readonly string FFprobeLocation = Path.Combine(AppDirectory, "ffprobe.exe");
internal static readonly string ModelDirectory = Path.Combine(AppDirectory, "Model");

internal static IEnumerable<string> ExpandFilePaths(IEnumerable<string> paths) {
try {
Expand Down Expand Up @@ -49,6 +52,16 @@ internal static class FileHelper {
return Path.Combine(outputDir, transcriptName);
}

#region Check if necessary file exists
internal static bool ModelExists(string modelFilename) {
return File.Exists(Path.Combine(ModelDirectory, modelFilename));
}

internal static bool FFmpegExists() {
return File.Exists(FFmpegLocation) && File.Exists(FFprobeLocation);
}
#endregion

#region Get temp file name
internal static string GetTempFile() {
return Path.GetTempFileName();
Expand Down Expand Up @@ -76,10 +89,7 @@ internal static class FileHelper {
}
#endregion

#region Whisper model related operations
internal static bool ModelExists(string modelFilename) {
return File.Exists(Path.Combine(ModelDirectory, modelFilename));
}
#region Whisper model related

internal static string GetModelName(string modelType) {
return $"ggml-{modelType}.bin";
Expand Down
48 changes: 2 additions & 46 deletions ConsoleWhisper/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using CommandLine;
using CommandLine.Text;
using ConsoleWhisper.Model;
using ConsoleWhisper.Module;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
Expand All @@ -16,8 +14,8 @@ internal class Program {
.ParseArguments<Argument>(args);

try {
await parseResult.WithParsedAsync(Run);
parseResult.WithNotParsed(errs => Error(parseResult, errs.ToList()));
await parseResult.WithParsedAsync(Runner.Run);
parseResult.WithNotParsed(errs => Runner.Error(parseResult, errs.ToList()));
} catch (ArgumentException ex) {
if (!string.IsNullOrEmpty(ex.Message)) Output.Error($"Argument Error: {ex.Message}");
return;
Expand All @@ -32,47 +30,5 @@ internal class Program {
return;
}
}

static async Task Run(Argument arg) {
try {
arg.Validate();

arg.ModelType = FileHelper.GetModelName(arg.ModelType);
arg.Files = FileHelper.ExpandFilePaths(arg.Files);

if (!FileHelper.ModelExists(arg.ModelType)) {
Output.Warn($"{arg.ModelType} is not found in Models directory, start downloading.");
await WhisperHelper.DownloadModel(arg.ModelType);
}

int cnt = 1;
foreach (var file in arg.Files) {
var mediaFilename = Path.GetFileName(file);
var wavFilename = await AudioHelper.Extract(arg.OutputDir, file, arg.OnlyExtract);

if (!arg.OnlyExtract) {
Output.Info($"Start transcribing file #{cnt++}: {mediaFilename}");
await WhisperHelper.Transcribe(arg.ModelType, wavFilename, mediaFilename, arg.OutputDir, arg.Language);
FileHelper.DelFile(wavFilename);
}
}


} catch (Exception) {
throw;
}
}

static void Error<T>(ParserResult<T> result, List<Error> errs) {
var helpText = HelpText.AutoBuild(result, options => {
options.AdditionalNewLineAfterOption = false;
options.MaximumDisplayWidth = Console.WindowWidth;
options.AutoHelp = true;
options.AutoVersion = true;
return HelpText.DefaultParsingErrorsHandler(result, options);
}, e => e);

Output.Help(helpText, errs.IsHelp(), errs.IsVersion());
}
}
}
63 changes: 63 additions & 0 deletions ConsoleWhisper/Runner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using CommandLine.Text;
using CommandLine;
using ConsoleWhisper.Model;
using ConsoleWhisper.Module;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System;
using Xabe.FFmpeg.Downloader;
using Xabe.FFmpeg;

namespace ConsoleWhisper {
internal static class Runner {
internal static async Task Run(Argument arg) {
try {
arg.Validate();

arg.ModelType = FileHelper.GetModelName(arg.ModelType);
arg.Files = FileHelper.ExpandFilePaths(arg.Files);

FFmpeg.SetExecutablesPath(FileHelper.AppDirectory);

if (!FileHelper.FFmpegExists()) {
Output.Warn($"FFmpeg not found, start downloading.");
await FFmpegDownloader.GetLatestVersion(FFmpegVersion.Official);
}

if (!FileHelper.ModelExists(arg.ModelType)) {
Output.Warn($"{arg.ModelType} is not found in Models directory, start downloading.");
await WhisperHelper.DownloadModel(arg.ModelType);
}

int cnt = 1;
foreach (var file in arg.Files) {
var mediaFilename = Path.GetFileName(file);
var wavFilename = await AudioHelper.Extract(arg.OutputDir, file, arg.OnlyExtract);

if (!arg.OnlyExtract) {
Output.Info($"Start transcribing file #{cnt++}: {mediaFilename}");
await WhisperHelper.Transcribe(arg.ModelType, wavFilename, mediaFilename, arg.OutputDir, arg.Language);
FileHelper.DelFile(wavFilename);
}
}


} catch (Exception) {
throw;
}
}

internal static void Error<T>(ParserResult<T> result, List<Error> errs) {
var helpText = HelpText.AutoBuild(result, options => {
options.AdditionalNewLineAfterOption = false;
options.MaximumDisplayWidth = Console.WindowWidth;
options.AutoHelp = true;
options.AutoVersion = true;
return HelpText.DefaultParsingErrorsHandler(result, options);
}, e => e);

Output.Help(helpText, errs.IsHelp(), errs.IsVersion());
}
}
}

0 comments on commit b5f4539

Please sign in to comment.