Return edited subtitle from tts - thx cvrle77 :)

Work on #8230
This commit is contained in:
Nikolaj Olsson 2024-04-21 19:15:41 +02:00
parent bffb62a2bb
commit 83a7aece48
6 changed files with 104 additions and 51 deletions

View File

@ -36804,9 +36804,13 @@ namespace Nikse.SubtitleEdit.Forms
{
using (var form = new TextToSpeech(_subtitle, GetCurrentSubtitleFormat(), _videoFileName, _videoInfo))
{
if (form.ShowDialog(this) != DialogResult.OK)
if (form.ShowDialog(this) == DialogResult.OK)
{
return;
var idx = FirstSelectedIndex;
_subtitle = form.EditedSubtitle;
SubtitleListview1.Fill(_subtitle, _subtitleOriginal);
_subtitleListViewIndex = -1;
SubtitleListview1.SelectIndexAndEnsureVisibleFaster(idx);
}
}
}

View File

@ -7,7 +7,7 @@ namespace Nikse.SubtitleEdit.Forms.Tts
{
public partial class RegenerateAudioClip : Form
{
public string NewAudioFileName { get; set; }
public TextToSpeech.FileNameAndSpeedFactor FileNameAndSpeedFactor { get; set; }
private readonly TextToSpeech _textToSpeech;
private readonly Subtitle _subtitle;
@ -39,10 +39,10 @@ namespace Nikse.SubtitleEdit.Forms.Tts
{
Cursor = Cursors.WaitCursor;
buttonReGenerate.Enabled = false;
var fileName = await _textToSpeech.GenerateAudio(paragraph, nikseComboBoxVoice.Text);
if (!string.IsNullOrEmpty(fileName))
var fileNameAndSpeedFactor = await _textToSpeech.ReGenerateAudio(paragraph, nikseComboBoxVoice.Text);
if (fileNameAndSpeedFactor != null)
{
NewAudioFileName = fileName;
FileNameAndSpeedFactor = fileNameAndSpeedFactor;
Cursor = Cursors.Default;
DialogResult = DialogResult.OK;
}

View File

@ -40,7 +40,7 @@
this.labelParagraphInfo = new System.Windows.Forms.Label();
this.checkBoxContinuePlay = new System.Windows.Forms.CheckBox();
this.buttonStop = new System.Windows.Forms.Button();
this.buttonReGenerate = new System.Windows.Forms.Button();
this.buttonEdit = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// listView1
@ -137,7 +137,7 @@
//
this.checkBoxContinuePlay.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.checkBoxContinuePlay.AutoSize = true;
this.checkBoxContinuePlay.Location = new System.Drawing.Point(703, 353);
this.checkBoxContinuePlay.Location = new System.Drawing.Point(717, 354);
this.checkBoxContinuePlay.Name = "checkBoxContinuePlay";
this.checkBoxContinuePlay.Size = new System.Drawing.Size(92, 17);
this.checkBoxContinuePlay.TabIndex = 11;
@ -156,24 +156,24 @@
this.buttonStop.UseVisualStyleBackColor = true;
this.buttonStop.Click += new System.EventHandler(this.buttonStop_Click);
//
// buttonReGenerate
// buttonEdit
//
this.buttonReGenerate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonReGenerate.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.buttonReGenerate.Location = new System.Drawing.Point(703, 249);
this.buttonReGenerate.Name = "buttonReGenerate";
this.buttonReGenerate.Size = new System.Drawing.Size(180, 23);
this.buttonReGenerate.TabIndex = 13;
this.buttonReGenerate.Text = "Regenerate";
this.buttonReGenerate.UseVisualStyleBackColor = true;
this.buttonReGenerate.Click += new System.EventHandler(this.buttonReGenerate_Click);
this.buttonEdit.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonEdit.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.buttonEdit.Location = new System.Drawing.Point(703, 249);
this.buttonEdit.Name = "buttonEdit";
this.buttonEdit.Size = new System.Drawing.Size(180, 23);
this.buttonEdit.TabIndex = 13;
this.buttonEdit.Text = "Edit";
this.buttonEdit.UseVisualStyleBackColor = true;
this.buttonEdit.Click += new System.EventHandler(this.buttonEdit_Click);
//
// ReviewAudioClips
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(894, 448);
this.Controls.Add(this.buttonReGenerate);
this.Controls.Add(this.buttonEdit);
this.Controls.Add(this.buttonStop);
this.Controls.Add(this.checkBoxContinuePlay);
this.Controls.Add(this.labelParagraphInfo);
@ -211,6 +211,6 @@
private System.Windows.Forms.Label labelParagraphInfo;
private System.Windows.Forms.CheckBox checkBoxContinuePlay;
private System.Windows.Forms.Button buttonStop;
private System.Windows.Forms.Button buttonReGenerate;
private System.Windows.Forms.Button buttonEdit;
}
}

View File

@ -14,12 +14,12 @@ namespace Nikse.SubtitleEdit.Forms.Tts
private readonly Subtitle _subtitle;
private readonly TextToSpeech _textToSpeech;
private readonly List<string> _fileNames;
private readonly List<TextToSpeech.FileNameAndSpeedFactor> _fileNames;
private bool _abortPlay;
private LibMpvDynamic _libMpv;
private Timer _mpvDoneTimer;
public ReviewAudioClips(TextToSpeech textToSpeech, Subtitle subtitle, List<string> fileNames)
public ReviewAudioClips(TextToSpeech textToSpeech, Subtitle subtitle, List<TextToSpeech.FileNameAndSpeedFactor> fileNames)
{
UiUtil.PreInitialize(this);
InitializeComponent();
@ -90,7 +90,7 @@ namespace Nikse.SubtitleEdit.Forms.Tts
}
var idx = listView1.SelectedItems[0].Index;
var waveFileName = _fileNames[idx];
var waveFileName = _fileNames[idx].Filename;
if (_libMpv != null)
{
@ -129,7 +129,7 @@ namespace Nikse.SubtitleEdit.Forms.Tts
listView1.Items[idx + 1].Selected = true;
listView1.Items[idx + 1].Focused = true;
listView1.Items[idx + 1].EnsureVisible();
TaskDelayHelper.RunDelayed(TimeSpan.FromMilliseconds(10), () => Play());
TaskDelayHelper.RunDelayed(TimeSpan.FromMilliseconds(10), () => Play());
Application.DoEvents();
}
}
@ -192,7 +192,7 @@ namespace Nikse.SubtitleEdit.Forms.Tts
}
}
private void buttonReGenerate_Click(object sender, EventArgs e)
private void buttonEdit_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count == 0)
{
@ -205,7 +205,8 @@ namespace Nikse.SubtitleEdit.Forms.Tts
var dr = form.ShowDialog(this);
if (dr == DialogResult.OK)
{
_fileNames[idx] = form.NewAudioFileName;
_fileNames[idx].Filename = form.FileNameAndSpeedFactor.Filename;
_fileNames[idx].Factor = form.FileNameAndSpeedFactor.Factor;
listView1.Items[idx].SubItems[4].Text = _subtitle.Paragraphs[idx].Text;
Play(true);
}

View File

@ -49,6 +49,7 @@
this.TextBoxTest = new Nikse.SubtitleEdit.Controls.NikseTextBox();
this.nikseComboBoxVoice = new Nikse.SubtitleEdit.Controls.NikseComboBox();
this.nikseComboBoxEngine = new Nikse.SubtitleEdit.Controls.NikseComboBox();
this.buttonCancel = new System.Windows.Forms.Button();
this.groupBoxMsSettings.SuspendLayout();
this.SuspendLayout();
//
@ -56,7 +57,7 @@
//
this.buttonOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonOK.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.buttonOK.Location = new System.Drawing.Point(767, 456);
this.buttonOK.Location = new System.Drawing.Point(686, 456);
this.buttonOK.Name = "buttonOK";
this.buttonOK.Size = new System.Drawing.Size(75, 23);
this.buttonOK.TabIndex = 100;
@ -93,7 +94,7 @@
| System.Windows.Forms.AnchorStyles.Right)));
this.progressBar1.Location = new System.Drawing.Point(12, 456);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(749, 10);
this.progressBar1.Size = new System.Drawing.Size(665, 10);
this.progressBar1.TabIndex = 12;
//
// labelEngine
@ -302,11 +303,25 @@
this.nikseComboBoxEngine.UsePopupWindow = false;
this.nikseComboBoxEngine.SelectedIndexChanged += new System.EventHandler(this.nikseComboBoxEngine_SelectedIndexChanged);
//
// buttonCancel
//
this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.buttonCancel.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.buttonCancel.Location = new System.Drawing.Point(767, 456);
this.buttonCancel.Name = "buttonCancel";
this.buttonCancel.Size = new System.Drawing.Size(75, 23);
this.buttonCancel.TabIndex = 101;
this.buttonCancel.Text = "C&ancel";
this.buttonCancel.UseVisualStyleBackColor = true;
this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click);
//
// TextToSpeech
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(854, 491);
this.Controls.Add(this.buttonCancel);
this.Controls.Add(this.labelActors);
this.Controls.Add(this.listViewActors);
this.Controls.Add(this.groupBoxMsSettings);
@ -354,5 +369,6 @@
private System.Windows.Forms.Label labelApiKey;
private Controls.NikseTextBox nikseTextBoxApiKey;
private System.Windows.Forms.CheckBox checkBoxShowPreview;
private System.Windows.Forms.Button buttonCancel;
}
}

View File

@ -21,6 +21,8 @@ namespace Nikse.SubtitleEdit.Forms.Tts
{
public sealed partial class TextToSpeech : Form
{
public Subtitle EditedSubtitle { get; set; }
private readonly Subtitle _subtitle;
private readonly string _videoFileName;
private readonly VideoInfo _videoInfo;
@ -41,6 +43,12 @@ namespace Nikse.SubtitleEdit.Forms.Tts
public int VoiceIndex { get; set; }
}
public class FileNameAndSpeedFactor
{
public string Filename { get; set; }
public decimal Factor { get; set; }
}
public class TextToSpeechEngine
{
public TextToSpeechEngineId Id { get; set; }
@ -89,6 +97,7 @@ namespace Nikse.SubtitleEdit.Forms.Tts
checkBoxAddToVideoFile.Text = LanguageSettings.Current.TextToSpeech.AddAudioToVideo;
buttonGenerateTTS.Text = LanguageSettings.Current.TextToSpeech.GenerateSpeech;
buttonOK.Text = LanguageSettings.Current.General.Ok;
buttonCancel.Text = LanguageSettings.Current.General.Cancel;
UiUtil.FixLargeFonts(this, buttonOK);
progressBar1.Visible = false;
@ -193,7 +202,7 @@ namespace Nikse.SubtitleEdit.Forms.Tts
return;
}
var fileNames = FixParagraphAudioSpeed();
var fileNameAndSpeedFactors = FixParagraphAudioSpeed(_subtitle, null);
if (_abort)
{
HandleAbort();
@ -202,7 +211,7 @@ namespace Nikse.SubtitleEdit.Forms.Tts
if (checkBoxShowPreview.Checked)
{
using (var form = new ReviewAudioClips(this, _subtitle, fileNames))
using (var form = new ReviewAudioClips(this, _subtitle, fileNameAndSpeedFactors))
{
var dr = form.ShowDialog(this);
if (dr != DialogResult.OK)
@ -214,12 +223,12 @@ namespace Nikse.SubtitleEdit.Forms.Tts
foreach (var idx in form.SkipIndices)
{
fileNames[idx] = null; // skip these files
fileNameAndSpeedFactors[idx] = null; // skip these files
}
}
}
var tempAudioFile = MergeAudioParagraphs(fileNames);
var tempAudioFile = MergeAudioParagraphs(fileNameAndSpeedFactors);
if (_abort)
{
HandleAbort();
@ -330,37 +339,47 @@ namespace Nikse.SubtitleEdit.Forms.Tts
}
}
private List<string> FixParagraphAudioSpeed()
private List<FileNameAndSpeedFactor> FixParagraphAudioSpeed(Subtitle subtitle, string overrideFileName)
{
var fileNames = new List<string>(_subtitle.Paragraphs.Count);
var fileNames = new List<FileNameAndSpeedFactor>(subtitle.Paragraphs.Count);
labelProgress.Text = string.Empty;
labelProgress.Refresh();
Application.DoEvents();
progressBar1.Value = 0;
progressBar1.Maximum = _subtitle.Paragraphs.Count;
progressBar1.Maximum = subtitle.Paragraphs.Count;
progressBar1.Visible = true;
for (var index = 0; index < _subtitle.Paragraphs.Count; index++)
for (var index = 0; index < subtitle.Paragraphs.Count; index++)
{
progressBar1.Value = index + 1;
labelProgress.Text = string.Format(LanguageSettings.Current.TextToSpeech.AdjustingSpeedXOfY, index + 1, _subtitle.Paragraphs.Count);
var p = _subtitle.Paragraphs[index];
var next = _subtitle.GetParagraphOrDefault(index + 1);
labelProgress.Text = string.Format(LanguageSettings.Current.TextToSpeech.AdjustingSpeedXOfY, index + 1, subtitle.Paragraphs.Count);
var p = subtitle.Paragraphs[index];
var next = subtitle.GetParagraphOrDefault(index + 1);
var pFileName = Path.Combine(_waveFolder, index + ".wav");
if (!string.IsNullOrEmpty(overrideFileName) && File.Exists(Path.Combine(_waveFolder, overrideFileName)))
{
pFileName = Path.Combine(_waveFolder, overrideFileName);
}
if (!File.Exists(pFileName))
{
pFileName = Path.Combine(_waveFolder, index + ".mp3");
}
var outputFileName1 = Path.Combine(_waveFolder, index + "_u.wav");
if (!string.IsNullOrEmpty(overrideFileName) && File.Exists(Path.Combine(_waveFolder, overrideFileName)))
{
outputFileName1 = Path.Combine(_waveFolder, Path.GetFileNameWithoutExtension(overrideFileName) + "_u.wav");
}
var trimProcess = VideoPreviewGenerator.TrimSilenceStartAndEnd(pFileName, outputFileName1);
trimProcess.Start();
while (!trimProcess.HasExited)
{
if (_abort)
{
return new List<string>();
return new List<FileNameAndSpeedFactor>();
}
}
@ -377,19 +396,24 @@ namespace Nikse.SubtitleEdit.Forms.Tts
if (_abort)
{
return new List<string>();
return new List<FileNameAndSpeedFactor>();
}
var waveInfo = UiUtil.GetVideoInfo(outputFileName1);
if (waveInfo.TotalMilliseconds <= p.DurationTotalMilliseconds + addDuration)
{
fileNames.Add(outputFileName1);
fileNames.Add(new FileNameAndSpeedFactor { Filename = outputFileName1, Factor = 1 });
continue;
}
var factor = waveInfo.TotalMilliseconds / (p.DurationTotalMilliseconds + addDuration);
var factor = (decimal)waveInfo.TotalMilliseconds / (decimal)(p.DurationTotalMilliseconds + addDuration);
var outputFileName2 = Path.Combine(_waveFolder, index + "_t.wav");
fileNames.Add(outputFileName2);
if (!string.IsNullOrEmpty(overrideFileName) && File.Exists(Path.Combine(_waveFolder, overrideFileName)))
{
outputFileName2 = Path.Combine(_waveFolder, Path.GetFileNameWithoutExtension(overrideFileName) + "_t.wav");
}
fileNames.Add(new FileNameAndSpeedFactor { Filename = outputFileName2, Factor = factor });
var mergeProcess = VideoPreviewGenerator.ChangeSpeed(outputFileName1, outputFileName2, (float)factor);
mergeProcess.Start();
@ -398,7 +422,7 @@ namespace Nikse.SubtitleEdit.Forms.Tts
Application.DoEvents();
if (_abort)
{
return new List<string>();
return new List<FileNameAndSpeedFactor>();
}
}
}
@ -406,7 +430,7 @@ namespace Nikse.SubtitleEdit.Forms.Tts
return fileNames;
}
private string MergeAudioParagraphs(List<string> fileNames)
private string MergeAudioParagraphs(List<FileNameAndSpeedFactor> fileNames)
{
labelProgress.Text = string.Empty;
labelProgress.Refresh();
@ -427,14 +451,14 @@ namespace Nikse.SubtitleEdit.Forms.Tts
labelProgress.Text = string.Format(LanguageSettings.Current.TextToSpeech.MergingAudioTrackXOfY, index + 1, _subtitle.Paragraphs.Count);
var p = _subtitle.Paragraphs[index];
var pFileName = fileNames[index];
if (!File.Exists(pFileName))
if (!File.Exists(pFileName.Filename))
{
SeLogger.Error($"TextToSpeech: File not found (skipping): {pFileName}");
SeLogger.Error($"TextToSpeech: File not found (skipping): {pFileName.Filename}");
continue;
}
outputFileName = Path.Combine(_waveFolder, $"silence{index}.wav");
var mergeProcess = VideoPreviewGenerator.MergeAudioTracks(inputFileName, pFileName, outputFileName, (float)p.StartTime.TotalSeconds);
var mergeProcess = VideoPreviewGenerator.MergeAudioTracks(inputFileName, pFileName.Filename, outputFileName, (float)p.StartTime.TotalSeconds);
inputFileName = outputFileName;
mergeProcess.Start();
@ -864,6 +888,7 @@ namespace Nikse.SubtitleEdit.Forms.Tts
private void buttonOK_Click(object sender, EventArgs e)
{
EditedSubtitle = _subtitle;
DialogResult = DialogResult.OK;
}
@ -1174,7 +1199,7 @@ namespace Nikse.SubtitleEdit.Forms.Tts
listViewActors.AutoSizeLastColumn();
}
public async Task<string> GenerateAudio(Paragraph p, string voice)
public async Task<FileNameAndSpeedFactor> ReGenerateAudio(Paragraph p, string voice)
{
nikseComboBoxVoice.Text = voice;
var sub = new Subtitle();
@ -1186,7 +1211,9 @@ namespace Nikse.SubtitleEdit.Forms.Tts
return null;
}
return Path.Combine(_waveFolder, waveFileNameOnly);
var fileNameAndSpeedFactors = FixParagraphAudioSpeed(sub, waveFileNameOnly);
return fileNameAndSpeedFactors.First();
}
private async void buttonTestVoice_Click(object sender, EventArgs e)
@ -1326,5 +1353,10 @@ namespace Nikse.SubtitleEdit.Forms.Tts
nikseComboBox.SelectedIndex = nikseComboBoxVoice.SelectedIndex;
}
private void buttonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
}
}
}