From a3155236e5105d748dc07b0488a1b7fc5353d6d7 Mon Sep 17 00:00:00 2001 From: niksedk Date: Tue, 25 Apr 2023 21:14:25 +0200 Subject: [PATCH] Allow only whisper-post-processing via context menu - thx Udoachim :) Somewhat fix #6870 --- .../WhisperAudioToText.Designer.cs | 20 ++++++- .../Forms/AudioToText/WhisperAudioToText.cs | 57 +++++++++++++++++-- src/ui/Forms/Main.cs | 2 +- src/ui/Logic/Language.cs | 1 + src/ui/Logic/LanguageStructure.cs | 1 + 5 files changed, 75 insertions(+), 6 deletions(-) diff --git a/src/ui/Forms/AudioToText/WhisperAudioToText.Designer.cs b/src/ui/Forms/AudioToText/WhisperAudioToText.Designer.cs index 2b86c905f..dd963f7d2 100644 --- a/src/ui/Forms/AudioToText/WhisperAudioToText.Designer.cs +++ b/src/ui/Forms/AudioToText/WhisperAudioToText.Designer.cs @@ -64,6 +64,8 @@ this.comboBoxCharsPerSub = new System.Windows.Forms.ComboBox(); this.comboBoxWhisperEngine = new System.Windows.Forms.ComboBox(); this.labelEngine = new System.Windows.Forms.Label(); + this.runOnlyPostProcessingToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.toolStripSeparatorRunOnlyPostprocessing = new System.Windows.Forms.ToolStripSeparator(); this.groupBoxModels.SuspendLayout(); this.groupBoxInputFiles.SuspendLayout(); this.contextMenuStripWhisperAdvanced.SuspendLayout(); @@ -367,10 +369,12 @@ // contextMenuStripWhisperAdvanced // this.contextMenuStripWhisperAdvanced.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.runOnlyPostProcessingToolStripMenuItem, + this.toolStripSeparatorRunOnlyPostprocessing, this.setCPPConstmeModelsFolderToolStripMenuItem, this.removeTemporaryFilesToolStripMenuItem}); this.contextMenuStripWhisperAdvanced.Name = "contextMenuStripWhisperAdvanced"; - this.contextMenuStripWhisperAdvanced.Size = new System.Drawing.Size(259, 48); + this.contextMenuStripWhisperAdvanced.Size = new System.Drawing.Size(259, 98); this.contextMenuStripWhisperAdvanced.Opening += new System.ComponentModel.CancelEventHandler(this.contextMenuStripWhisperAdvanced_Opening); // // setCPPConstmeModelsFolderToolStripMenuItem @@ -436,6 +440,18 @@ this.labelEngine.TabIndex = 27; this.labelEngine.Text = "Engine"; // + // runOnlyPostProcessingToolStripMenuItem + // + this.runOnlyPostProcessingToolStripMenuItem.Name = "runOnlyPostProcessingToolStripMenuItem"; + this.runOnlyPostProcessingToolStripMenuItem.Size = new System.Drawing.Size(258, 22); + this.runOnlyPostProcessingToolStripMenuItem.Text = "Run only post processing"; + this.runOnlyPostProcessingToolStripMenuItem.Click += new System.EventHandler(this.runOnlyPostProcessingToolStripMenuItem_Click); + // + // toolStripSeparatorRunOnlyPostprocessing + // + this.toolStripSeparatorRunOnlyPostprocessing.Name = "toolStripSeparatorRunOnlyPostprocessing"; + this.toolStripSeparatorRunOnlyPostprocessing.Size = new System.Drawing.Size(255, 6); + // // WhisperAudioToText // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -519,5 +535,7 @@ private System.Windows.Forms.ComboBox comboBoxWhisperEngine; private System.Windows.Forms.Label labelEngine; private System.Windows.Forms.ToolStripMenuItem setCPPConstmeModelsFolderToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem runOnlyPostProcessingToolStripMenuItem; + private System.Windows.Forms.ToolStripSeparator toolStripSeparatorRunOnlyPostprocessing; } } \ No newline at end of file diff --git a/src/ui/Forms/AudioToText/WhisperAudioToText.cs b/src/ui/Forms/AudioToText/WhisperAudioToText.cs index 17449feda..0c0475e2e 100644 --- a/src/ui/Forms/AudioToText/WhisperAudioToText.cs +++ b/src/ui/Forms/AudioToText/WhisperAudioToText.cs @@ -20,6 +20,7 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText public sealed partial class WhisperAudioToText : Form { private readonly string _videoFileName; + private Subtitle _subtitle; private readonly int _audioTrackNumber; private bool _cancel; private bool _batchMode; @@ -46,13 +47,14 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText public Subtitle TranscribedSubtitle { get; private set; } - public WhisperAudioToText(string videoFileName, int audioTrackNumber, Form parentForm, WavePeakData wavePeaks) + public WhisperAudioToText(string videoFileName, Subtitle subtitle, int audioTrackNumber, Form parentForm, WavePeakData wavePeaks) { UiUtil.PreInitialize(this); InitializeComponent(); UiUtil.FixFonts(this); UiUtil.FixLargeFonts(this, buttonGenerate); _videoFileName = videoFileName; + _subtitle = subtitle; _audioTrackNumber = audioTrackNumber; _parentForm = parentForm; _wavePeaks = wavePeaks; @@ -75,7 +77,8 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText buttonAddFile.Text = LanguageSettings.Current.DvdSubRip.Add; buttonRemoveFile.Text = LanguageSettings.Current.DvdSubRip.Remove; buttonClear.Text = LanguageSettings.Current.DvdSubRip.Clear; - setCPPConstmeModelsFolderToolStripMenuItem.Text = LanguageSettings.Current.AudioToText.ChooseLanguage; + runOnlyPostProcessingToolStripMenuItem.Text = LanguageSettings.Current.AudioToText.OnlyRunPostProcessing; + setCPPConstmeModelsFolderToolStripMenuItem.Text = LanguageSettings.Current.AudioToText.SetCppConstMeFolder; removeTemporaryFilesToolStripMenuItem.Text = LanguageSettings.Current.AudioToText.RemoveTemporaryFiles; columnHeaderFileName.Text = LanguageSettings.Current.JoinSubtitles.FileName; @@ -95,6 +98,17 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText listViewInputFiles.Items.Add(videoFileName); } + if (_subtitle == null || _subtitle.Paragraphs.Count == 0) + { + runOnlyPostProcessingToolStripMenuItem.Visible = false; + toolStripSeparatorRunOnlyPostprocessing.Visible = false; + } + else + { + runOnlyPostProcessingToolStripMenuItem.Visible = true; + toolStripSeparatorRunOnlyPostprocessing.Visible = true; + } + textBoxLog.Visible = false; textBoxLog.Dock = DockStyle.Fill; labelProgress.Text = string.Empty; @@ -1491,11 +1505,46 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText if (!string.IsNullOrEmpty(Configuration.Settings.Tools.WhisperCppModelLocation) && Directory.Exists(Configuration.Settings.Tools.WhisperCppModelLocation)) { - setCPPConstmeModelsFolderToolStripMenuItem.Text = $"Set CPP/Const-me models folder... [{Configuration.Settings.Tools.WhisperCppModelLocation}]"; + setCPPConstmeModelsFolderToolStripMenuItem.Text = $"{LanguageSettings.Current.AudioToText.SetCppConstMeFolder} [{Configuration.Settings.Tools.WhisperCppModelLocation}]"; } else { - setCPPConstmeModelsFolderToolStripMenuItem.Text = "Set CPP/Const-me models folder..."; + setCPPConstmeModelsFolderToolStripMenuItem.Text = LanguageSettings.Current.AudioToText.SetCppConstMeFolder; + } + } + + private void runOnlyPostProcessingToolStripMenuItem_Click(object sender, EventArgs e) + { + try + { + _languageCode = LanguageAutoDetect.AutoDetectGoogleLanguage(_subtitle); + var postProcessor = new AudioToTextPostProcessor(_languageCode) + { + ParagraphMaxChars = Configuration.Settings.General.SubtitleLineMaximumLength * 2, + }; + + WavePeakData wavePeaks = null; + if (checkBoxAutoAdjustTimings.Checked) + { + wavePeaks = _wavePeaks ?? MakeWavePeaks(); + } + + if (checkBoxAutoAdjustTimings.Checked && wavePeaks != null) + { + _subtitle = WhisperTimingFixer.ShortenLongTexts(_subtitle); + _subtitle = WhisperTimingFixer.ShortenViaWavePeaks(_subtitle, wavePeaks); + } + + TranscribedSubtitle = postProcessor.Fix(AudioToTextPostProcessor.Engine.Whisper, _subtitle, true, true, true, true, true, true); + DialogResult = DialogResult.OK; + } + finally + { + buttonGenerate.Enabled = true; + buttonDownload.Enabled = true; + buttonBatchMode.Enabled = true; + comboBoxLanguages.Enabled = true; + comboBoxModels.Enabled = true; } } } diff --git a/src/ui/Forms/Main.cs b/src/ui/Forms/Main.cs index ee61313c0..f60387d01 100644 --- a/src/ui/Forms/Main.cs +++ b/src/ui/Forms/Main.cs @@ -34813,7 +34813,7 @@ namespace Nikse.SubtitleEdit.Forms CloseVideoToolStripMenuItemClick(sender, e); } - using (var form = new WhisperAudioToText(oldVideoFileName, _videoAudioTrackNumber, this, audioVisualizer?.WavePeaks)) + using (var form = new WhisperAudioToText(oldVideoFileName, _subtitle, _videoAudioTrackNumber, this, audioVisualizer?.WavePeaks)) { var result = form.ShowDialog(this); diff --git a/src/ui/Logic/Language.cs b/src/ui/Logic/Language.cs index 7d81a1007..92e204dbe 100644 --- a/src/ui/Logic/Language.cs +++ b/src/ui/Logic/Language.cs @@ -350,6 +350,7 @@ namespace Nikse.SubtitleEdit.Logic MaxCharsPerSubtitle = "Max. chars per subtitle line", RemoveTemporaryFiles = "Remove temporary files", SetCppConstMeFolder = "Set CPP/Const-me models folder...", + OnlyRunPostProcessing = "", }; AssaAttachments = new LanguageStructure.AssaAttachments diff --git a/src/ui/Logic/LanguageStructure.cs b/src/ui/Logic/LanguageStructure.cs index 8ac9528d8..dc69ecb33 100644 --- a/src/ui/Logic/LanguageStructure.cs +++ b/src/ui/Logic/LanguageStructure.cs @@ -207,6 +207,7 @@ namespace Nikse.SubtitleEdit.Logic public string MaxCharsPerSubtitle { get; set; } public string RemoveTemporaryFiles { get; set; } public string SetCppConstMeFolder { get; set; } + public string OnlyRunPostProcessing { get; set; } } public class AssaAttachments