From b5f4539b733a73cfc699eb5aad492ca49c3ea0ee Mon Sep 17 00:00:00 2001
From: azhuge233 <17763056+azhuge233@users.noreply.github.com>
Date: Thu, 22 Jun 2023 10:39:23 +0800
Subject: [PATCH] 1.0.1 - Auto download FFmpeg to program directory.
---
ConsoleWhisper/ConsoleWhisper.csproj | 3 +-
ConsoleWhisper/Module/FileHelper.cs | 20 ++++++---
ConsoleWhisper/Program.cs | 48 +--------------------
ConsoleWhisper/Runner.cs | 63 ++++++++++++++++++++++++++++
4 files changed, 82 insertions(+), 52 deletions(-)
create mode 100644 ConsoleWhisper/Runner.cs
diff --git a/ConsoleWhisper/ConsoleWhisper.csproj b/ConsoleWhisper/ConsoleWhisper.csproj
index 44e9fee..59db273 100644
--- a/ConsoleWhisper/ConsoleWhisper.csproj
+++ b/ConsoleWhisper/ConsoleWhisper.csproj
@@ -11,7 +11,7 @@
ConsoleWhisper
- 1.0.0
+ 1.0.1
azhuge233
@@ -21,6 +21,7 @@
+
diff --git a/ConsoleWhisper/Module/FileHelper.cs b/ConsoleWhisper/Module/FileHelper.cs
index 4b40c67..710135b 100644
--- a/ConsoleWhisper/Module/FileHelper.cs
+++ b/ConsoleWhisper/Module/FileHelper.cs
@@ -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 ExpandFilePaths(IEnumerable paths) {
try {
@@ -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();
@@ -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";
diff --git a/ConsoleWhisper/Program.cs b/ConsoleWhisper/Program.cs
index 38148c2..9c1c72e 100644
--- a/ConsoleWhisper/Program.cs
+++ b/ConsoleWhisper/Program.cs
@@ -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;
@@ -16,8 +14,8 @@ internal class Program {
.ParseArguments(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;
@@ -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(ParserResult result, List 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());
- }
}
}
\ No newline at end of file
diff --git a/ConsoleWhisper/Runner.cs b/ConsoleWhisper/Runner.cs
new file mode 100644
index 0000000..7fc500b
--- /dev/null
+++ b/ConsoleWhisper/Runner.cs
@@ -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(ParserResult result, List 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());
+ }
+ }
+}