Fix for Ollama - thx gericho :)

work on #8143
This commit is contained in:
Nikolaj Olsson 2024-04-09 19:51:58 +02:00
parent d13313b3d3
commit fb7126504a
6 changed files with 82 additions and 7 deletions

View File

@ -38,6 +38,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=Nikse/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Nllb/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Nuendo/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Ollama/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Purfview/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Stbl/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Tahoma/@EntryIndexedValue">True</s:Boolean>

View File

@ -547,6 +547,7 @@ namespace Nikse.SubtitleEdit.Core.Common
ChatGptPrompt = "Translate from {0} to {1}, keep sentences in {1} as they are, do not censor the translation, give only the output without commenting on what you read:";
ChatGptModel = "gpt-3.5-turbo";
OllamaApiUrl = "http://localhost:11434/api/generate";
OllamaModel = "llama2";
AnthropicApiUrl = "https://api.anthropic.com/v1/messages";
AnthropicPrompt = "Translate from {0} to {1}, keep sentences in {1} as they are, do not censor the translation, give only the output without commenting on what you read:";
AnthropicApiModel = "claude-3-opus-20240229";

View File

@ -37,6 +37,7 @@
this.labelMsVoice = new System.Windows.Forms.Label();
this.nikseComboBoxVoice = new Nikse.SubtitleEdit.Controls.NikseComboBox();
this.nikseComboBoxEngine = new Nikse.SubtitleEdit.Controls.NikseComboBox();
this.checkBoxAddToVideoFile = new System.Windows.Forms.CheckBox();
this.groupBoxMsSettings.SuspendLayout();
this.SuspendLayout();
//
@ -169,11 +170,22 @@
this.nikseComboBoxEngine.UsePopupWindow = false;
this.nikseComboBoxEngine.SelectedIndexChanged += new System.EventHandler(this.nikseComboBoxEngine_SelectedIndexChanged);
//
// checkBoxAddToVideoFile
//
this.checkBoxAddToVideoFile.AutoSize = true;
this.checkBoxAddToVideoFile.Location = new System.Drawing.Point(15, 82);
this.checkBoxAddToVideoFile.Name = "checkBoxAddToVideoFile";
this.checkBoxAddToVideoFile.Size = new System.Drawing.Size(176, 17);
this.checkBoxAddToVideoFile.TabIndex = 16;
this.checkBoxAddToVideoFile.Text = "Add audio to video file (new file)";
this.checkBoxAddToVideoFile.UseVisualStyleBackColor = true;
//
// TextToSpeech
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(811, 442);
this.Controls.Add(this.checkBoxAddToVideoFile);
this.Controls.Add(this.groupBoxMsSettings);
this.Controls.Add(this.labelEngine);
this.Controls.Add(this.nikseComboBoxEngine);
@ -205,5 +217,6 @@
private System.Windows.Forms.GroupBox groupBoxMsSettings;
private System.Windows.Forms.Label labelMsVoice;
private Controls.NikseComboBox nikseComboBoxVoice;
private System.Windows.Forms.CheckBox checkBoxAddToVideoFile;
}
}

View File

@ -54,6 +54,10 @@ namespace Nikse.SubtitleEdit.Forms
{
GenerateParagraphAudioTortoiseTts();
}
else if (nikseComboBoxEngine.SelectedIndex == 2)
{
GenerateParagraphAudioMimic3();
}
var fileNames = FixParagraphAudioSpeed();
@ -64,9 +68,31 @@ namespace Nikse.SubtitleEdit.Forms
Cleanup(_waveFolder, resultAudioFileName);
if (checkBoxAddToVideoFile.Checked)
{
AddAudioToVideoFile(resultAudioFileName);
}
UiUtil.OpenFolder(_waveFolder);
}
private void AddAudioToVideoFile(string audioFileName)
{
var videoExt = ".mkv";
if (_videoFileName.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase))
{
videoExt = ".mp4";
}
labelProgress.Text = "Add audtio to video file...";
var outputFileName = Path.Combine(_waveFolder, Path.GetFileNameWithoutExtension(audioFileName) + videoExt);
Process addAudioProcess = VideoPreviewGenerator.AddAudioTrack(_videoFileName, audioFileName, outputFileName);
addAudioProcess.Start();
addAudioProcess.WaitForExit();
labelProgress.Text = string.Empty;
}
private static void Cleanup(string waveFolder, string resultAudioFile)
{
foreach (var fileName in Directory.GetFiles(waveFolder, "*.wav"))
@ -97,18 +123,22 @@ namespace Nikse.SubtitleEdit.Forms
var next = _subtitle.GetParagraphOrDefault(index + 1);
var pFileName = Path.Combine(_waveFolder, index + ".wav");
var addDuration = 0d;
if (next != null && p.EndTime.TotalMilliseconds < next.StartTime.TotalMilliseconds)
{
var diff = next.StartTime.TotalMilliseconds - p.EndTime.TotalMilliseconds;
addDuration = Math.Max(1000, diff);
}
var outputFileName1 = Path.Combine(_waveFolder, index + "_u.wav");
var trimProcess = VideoPreviewGenerator.TrimSilenceStartAndEnd(pFileName, outputFileName1);
trimProcess.Start();
trimProcess.WaitForExit();
var addDuration = 0d;
if (next != null && p.EndTime.TotalMilliseconds < next.StartTime.TotalMilliseconds)
{
var diff = next.StartTime.TotalMilliseconds - p.EndTime.TotalMilliseconds;
addDuration = Math.Min(1000, diff);
if (addDuration < 0)
{
addDuration = 0;
}
}
var waveInfo = UiUtil.GetVideoInfo(outputFileName1);
if (waveInfo.TotalMilliseconds <= p.DurationTotalMilliseconds + addDuration)
{
@ -148,6 +178,12 @@ namespace Nikse.SubtitleEdit.Forms
labelProgress.Text = $"Merging audio track: {index + 1} / {_subtitle.Paragraphs.Count}...";
var p = _subtitle.Paragraphs[index];
var pFileName = fileNames[index];
if (!File.Exists(pFileName))
{
SeLogger.Error($"TextToSpeech: File not found (skipping): {pFileName}");
continue;
}
outputFileName = Path.Combine(_waveFolder, $"silence{index}.wav");
var mergeProcess = VideoPreviewGenerator.MergeAudioTracks(inputFileName, pFileName, outputFileName, (float)p.StartTime.TotalSeconds);
inputFileName = outputFileName;
@ -276,6 +312,11 @@ namespace Nikse.SubtitleEdit.Forms
return true;
}
private void GenerateParagraphAudioMimic3()
{
throw new NotImplementedException();
}
private void buttonOK_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;

View File

@ -341,6 +341,7 @@ namespace Nikse.SubtitleEdit.Forms.Translate
labelFormality.Text = LanguageSettings.Current.AudioToText.Model;
labelFormality.Visible = true;
comboBoxFormality.Enabled = true;
comboBoxFormality.Left = labelFormality.Right + 3;
comboBoxFormality.Visible = true;
comboBoxFormality.DropDownStyle = ComboBoxStyle.DropDown;

View File

@ -622,5 +622,23 @@ namespace Nikse.SubtitleEdit.Logic
return processMakeVideo;
}
public static Process AddAudioTrack(string inputFileName, string audioFileName, string outputFileName, DataReceivedEventHandler dataReceivedHandler = null)
{
var processMakeVideo = new Process
{
StartInfo =
{
FileName = GetFfmpegLocation(),
Arguments = $"-i \"{inputFileName}\" -i \"{audioFileName}\" -c copy -map 0:v:0 -map 1:a:0 \"{outputFileName}\"",
UseShellExecute = false,
CreateNoWindow = true
}
};
SetupDataReceiveHandler(dataReceivedHandler, processMakeVideo);
return processMakeVideo;
}
}
}