From 72464d0bbf7558f8475c63429ad88bb419f265dc Mon Sep 17 00:00:00 2001 From: niksedk Date: Fri, 5 May 2023 07:48:46 +0200 Subject: [PATCH] Test stable-ts --- LanguageBaseEnglish.xml | 2 +- src/libse/AudioToText/WhisperChoice.cs | 1 + src/libse/AudioToText/WhisperHelper.cs | 52 ++++++++++++++++++- src/libse/Common/Settings.cs | 8 +++ .../Forms/AudioToText/WhisperAudioToText.cs | 45 +++++++++++++++- .../WhisperAudioToTextSelectedLines.cs | 37 +++++++++++++ 6 files changed, 141 insertions(+), 4 deletions(-) diff --git a/LanguageBaseEnglish.xml b/LanguageBaseEnglish.xml index ff13a575b..85cac5029 100644 --- a/LanguageBaseEnglish.xml +++ b/LanguageBaseEnglish.xml @@ -1497,7 +1497,7 @@ To use an API key, go to "Options -> Settings -> Tools" to enter your Goog Auto continue in one second Auto continue in {0} seconds Still typing... auto continue stopped - &Insert new subtitle at video pos + Insert new subtitle at video pos Insert new subtitle at video pos (no text box focus) Auto Play from just before text diff --git a/src/libse/AudioToText/WhisperChoice.cs b/src/libse/AudioToText/WhisperChoice.cs index b4734e2e5..7a81e2453 100644 --- a/src/libse/AudioToText/WhisperChoice.cs +++ b/src/libse/AudioToText/WhisperChoice.cs @@ -7,5 +7,6 @@ public const string WhisperX = "WhisperX"; public const string ConstMe = "Const-me"; public const string CTranslate2 = "CTranslate2"; + public const string StableTs = "stable-ts"; } } diff --git a/src/libse/AudioToText/WhisperHelper.cs b/src/libse/AudioToText/WhisperHelper.cs index 6ade308b0..fa04fcf2a 100644 --- a/src/libse/AudioToText/WhisperHelper.cs +++ b/src/libse/AudioToText/WhisperHelper.cs @@ -60,6 +60,11 @@ namespace Nikse.SubtitleEdit.Core.AudioToText return "https://github.com/jordimas/whisper-ctranslate2"; } + if (Configuration.Settings.Tools.WhisperChoice == WhisperChoice.StableTs) + { + return "https://github.com/jianfch/stable-ts"; + } + return "https://github.com/openai/whisper"; } @@ -141,6 +146,19 @@ namespace Nikse.SubtitleEdit.Core.AudioToText } } + if (Configuration.Settings.Tools.WhisperChoice == WhisperChoice.StableTs && !string.IsNullOrEmpty(Configuration.Settings.Tools.WhisperStableTsLocation)) + { + if (Configuration.Settings.Tools.WhisperStableTsLocation.EndsWith("stable-ts.exe", StringComparison.InvariantCultureIgnoreCase) && File.Exists(Configuration.Settings.Tools.WhisperStableTsLocation)) + { + return Path.GetDirectoryName(Configuration.Settings.Tools.WhisperStableTsLocation); + } + + if (Directory.Exists(Configuration.Settings.Tools.WhisperStableTsLocation) && File.Exists(Path.Combine(Configuration.Settings.Tools.WhisperStableTsLocation, "stable-ts.exe"))) + { + return Configuration.Settings.Tools.WhisperStableTsLocation; + } + } + if (Configuration.Settings.Tools.WhisperChoice == WhisperChoice.ConstMe) { var path = Path.Combine(Configuration.DataDirectory, "Whisper", "Const-me"); @@ -181,6 +199,17 @@ namespace Nikse.SubtitleEdit.Core.AudioToText return null; } + if (Configuration.Settings.Tools.WhisperChoice == WhisperChoice.StableTs) + { + var whisperCTranslate2FullPath = Path.Combine(dir, "Scripts", "stable-ts.exe"); + if (File.Exists(whisperCTranslate2FullPath)) + { + return Path.Combine(dir, "Scripts"); + } + + return null; + } + var whisperFullPath = Path.Combine(dir, "Scripts", "whisper.exe"); if (File.Exists(whisperFullPath)) { @@ -223,6 +252,11 @@ namespace Nikse.SubtitleEdit.Core.AudioToText return "main"; } + if (Configuration.Settings.Tools.WhisperChoice == WhisperChoice.StableTs) + { + return "stable-ts"; + } + return "whisper"; } @@ -252,6 +286,14 @@ namespace Nikse.SubtitleEdit.Core.AudioToText return f; } } + else if (Configuration.Settings.Tools.WhisperChoice == WhisperChoice.StableTs) + { + var f = Path.Combine(whisperFolder, "stable-ts.exe"); + if (File.Exists(f)) + { + return f; + } + } else if (Configuration.Settings.Tools.WhisperChoice == WhisperChoice.ConstMe) { var f = Path.Combine(whisperFolder, "main.exe"); @@ -295,8 +337,14 @@ namespace Nikse.SubtitleEdit.Core.AudioToText { return f; } - - + } + else if (Configuration.Settings.Tools.WhisperChoice == WhisperChoice.StableTs) + { + var f = Path.Combine(whisperFolder, "stable-ts"); + if (File.Exists(f)) + { + return f; + } } return "whisper"; diff --git a/src/libse/Common/Settings.cs b/src/libse/Common/Settings.cs index 08c6378b6..fb20bd4d1 100644 --- a/src/libse/Common/Settings.cs +++ b/src/libse/Common/Settings.cs @@ -434,6 +434,7 @@ namespace Nikse.SubtitleEdit.Core.Common public string WhisperLocation { get; set; } public string WhisperCtranslate2Location { get; set; } public string WhisperXLocation { get; set; } + public string WhisperStableTsLocation { get; set; } public string WhisperCppModelLocation { get; set; } public string WhisperExtraSettings { get; set; } public bool WhisperAutoAdjustTimings { get; set; } @@ -6367,6 +6368,12 @@ $HorzAlign = Center settings.Tools.WhisperXLocation = subNode.InnerText; } + subNode = node.SelectSingleNode("WhisperStableTsLocation"); + if (subNode != null) + { + settings.Tools.WhisperStableTsLocation = subNode.InnerText; + } + subNode = node.SelectSingleNode("WhisperCppModelLocation"); if (subNode != null) { @@ -10767,6 +10774,7 @@ $HorzAlign = Center textWriter.WriteElementString("WhisperLocation", settings.Tools.WhisperLocation); textWriter.WriteElementString("WhisperCtranslate2Location", settings.Tools.WhisperCtranslate2Location); textWriter.WriteElementString("WhisperXLocation", settings.Tools.WhisperXLocation); + textWriter.WriteElementString("WhisperStableTsLocation", settings.Tools.WhisperStableTsLocation); textWriter.WriteElementString("WhisperCppModelLocation", settings.Tools.WhisperCppModelLocation); textWriter.WriteElementString("WhisperExtraSettings", settings.Tools.WhisperExtraSettings); textWriter.WriteElementString("WhisperLanguageCode", settings.Tools.WhisperLanguageCode); diff --git a/src/ui/Forms/AudioToText/WhisperAudioToText.cs b/src/ui/Forms/AudioToText/WhisperAudioToText.cs index 1ac54faaf..bcfd9aa88 100644 --- a/src/ui/Forms/AudioToText/WhisperAudioToText.cs +++ b/src/ui/Forms/AudioToText/WhisperAudioToText.cs @@ -162,6 +162,7 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText engines.Add(WhisperChoice.ConstMe); } engines.Add(WhisperChoice.CTranslate2); + engines.Add(WhisperChoice.StableTs); engines.Add(WhisperChoice.WhisperX); foreach (var engine in engines) @@ -960,6 +961,7 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText } var outputSrt = string.Empty; + var postParams = string.Empty; if (Configuration.Settings.Tools.WhisperChoice == WhisperChoice.Cpp || Configuration.Settings.Tools.WhisperChoice == WhisperChoice.ConstMe) { @@ -971,10 +973,15 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText Configuration.Settings.Tools.WhisperExtraSettings = string.Empty; } } + else if (Configuration.Settings.Tools.WhisperChoice == WhisperChoice.StableTs) + { + var srtFileName = Path.GetFileNameWithoutExtension(waveFileName); + postParams = $" -o {srtFileName}.srt"; + } var w = WhisperHelper.GetWhisperPathAndFileName(); var m = WhisperHelper.GetWhisperModelForCmdLine(model); - var parameters = $"--language {language} --model \"{m}\" {outputSrt}{translateToEnglish}{Configuration.Settings.Tools.WhisperExtraSettings} \"{waveFileName}\""; + var parameters = $"--language {language} --model \"{m}\" {outputSrt}{translateToEnglish}{Configuration.Settings.Tools.WhisperExtraSettings} \"{waveFileName}\"{postParams}"; SeLogger.Error($"{w} {parameters}"); @@ -1495,6 +1502,38 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText Init(); } + private void WhisperEngineStableTs() + { + Configuration.Settings.Tools.WhisperChoice = WhisperChoice.StableTs; + + if (Configuration.IsRunningOnWindows) + { + var path = WhisperHelper.GetWhisperFolder(); + if (string.IsNullOrEmpty(path)) + { + using (var openFileDialog1 = new OpenFileDialog()) + { + openFileDialog1.Title = "Locate stable-ts.exe (Python version)"; + openFileDialog1.FileName = string.Empty; + openFileDialog1.Filter = "stable-ts.exe|stable-ts.exe"; + + if (openFileDialog1.ShowDialog() != DialogResult.OK + || !openFileDialog1.FileName.EndsWith("stable-ts.exe", StringComparison.OrdinalIgnoreCase)) + { + Configuration.Settings.Tools.WhisperChoice = WhisperChoice.Cpp; + comboBoxWhisperEngine.Text = WhisperChoice.Cpp; + } + else + { + Configuration.Settings.Tools.WhisperStableTsLocation = openFileDialog1.FileName; + } + } + } + } + + Init(); + } + private void comboBoxWhisperEngine_SelectedIndexChanged(object sender, EventArgs e) { if (comboBoxWhisperEngine.Text == Configuration.Settings.Tools.WhisperChoice) @@ -1523,6 +1562,10 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText { WhisperEngineWhisperX(); } + else if (comboBoxWhisperEngine.Text == WhisperChoice.StableTs) + { + WhisperEngineStableTs(); + } } private void setCPPConstMeModelsFolderToolStripMenuItem_Click(object sender, EventArgs e) diff --git a/src/ui/Forms/AudioToText/WhisperAudioToTextSelectedLines.cs b/src/ui/Forms/AudioToText/WhisperAudioToTextSelectedLines.cs index e2a7da322..c1f28a5b5 100644 --- a/src/ui/Forms/AudioToText/WhisperAudioToTextSelectedLines.cs +++ b/src/ui/Forms/AudioToText/WhisperAudioToTextSelectedLines.cs @@ -483,6 +483,39 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText Init(); } + + private void WhisperEngineStableTs() + { + Configuration.Settings.Tools.WhisperChoice = WhisperChoice.StableTs; + + if (Configuration.IsRunningOnWindows) + { + var path = WhisperHelper.GetWhisperFolder(); + if (string.IsNullOrEmpty(path)) + { + using (var openFileDialog1 = new OpenFileDialog()) + { + openFileDialog1.Title = "Locate stable-ts.exe (Python version)"; + openFileDialog1.FileName = string.Empty; + openFileDialog1.Filter = "stable-ts.exe|stable-ts.exe"; + + if (openFileDialog1.ShowDialog() != DialogResult.OK + || !openFileDialog1.FileName.EndsWith("stable-ts.exe", StringComparison.OrdinalIgnoreCase)) + { + Configuration.Settings.Tools.WhisperChoice = WhisperChoice.Cpp; + comboBoxWhisperEngine.Text = WhisperChoice.Cpp; + } + else + { + Configuration.Settings.Tools.WhisperStableTsLocation = openFileDialog1.FileName; + } + } + } + } + + Init(); + } + private void removeTemporaryFilesToolStripMenuItem_Click(object sender, EventArgs e) { Configuration.Settings.Tools.WhisperDeleteTempFiles = !Configuration.Settings.Tools.WhisperDeleteTempFiles; @@ -580,6 +613,10 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText { WhisperEngineWhisperX(); } + else if (comboBoxWhisperEngine.Text == WhisperChoice.StableTs) + { + WhisperEngineStableTs(); + } } private void setCPPConstMeModelsFolderToolStripMenuItem_Click(object sender, EventArgs e)