Work on add of "merge actions" to cmd line convert

This commit is contained in:
Nikolaj Olsson 2019-12-05 05:49:35 +01:00
parent 00325148b9
commit 7abbf6145b
4 changed files with 148 additions and 58 deletions

View File

@ -198,6 +198,7 @@
<Compile Include="ImageSplitterItem.cs" />
<Compile Include="Interfaces\IRtfTextConverter.cs" />
<Compile Include="IsoCountryCodes.cs" />
<Compile Include="MergeShortLinesUtils.cs" />
<Compile Include="NetflixImsc11JapaneseToAss.cs" />
<Compile Include="PlainTextImporter.cs" />
<Compile Include="Position.cs" />

View File

@ -0,0 +1,103 @@
using System;
namespace Nikse.SubtitleEdit.Core
{
public class MergeShortLinesUtils
{
public static Subtitle MergeShortLinesInSubtitle(Subtitle subtitle, double maxMillisecondsBetweenLines, int maxCharacters, bool onlyContinuousLines)
{
string language = LanguageAutoDetect.AutoDetectGoogleLanguage(subtitle);
var mergedSubtitle = new Subtitle();
bool lastMerged = false;
Paragraph p = null;
for (int i = 1; i < subtitle.Paragraphs.Count; i++)
{
if (!lastMerged)
{
p = new Paragraph(subtitle.GetParagraphOrDefault(i - 1));
mergedSubtitle.Paragraphs.Add(p);
}
Paragraph next = subtitle.GetParagraphOrDefault(i);
if (next != null)
{
if (Utilities.QualifiesForMerge(p, next, maxMillisecondsBetweenLines, maxCharacters, onlyContinuousLines))
{
if (GetStartTag(p.Text) == GetStartTag(next.Text) &&
GetEndTag(p.Text) == GetEndTag(next.Text))
{
string s1 = p.Text.Trim();
s1 = s1.Substring(0, s1.Length - GetEndTag(s1).Length);
string s2 = next.Text.Trim();
s2 = s2.Substring(GetStartTag(s2).Length);
p.Text = Utilities.AutoBreakLine(s1 + Environment.NewLine + s2, language);
}
else
{
p.Text = Utilities.AutoBreakLine(p.Text + Environment.NewLine + next.Text, language);
}
p.EndTime = next.EndTime;
lastMerged = true;
}
else
{
lastMerged = false;
}
}
else
{
lastMerged = false;
}
}
if (!lastMerged)
{
mergedSubtitle.Paragraphs.Add(new Paragraph(subtitle.GetParagraphOrDefault(subtitle.Paragraphs.Count - 1)));
}
return mergedSubtitle;
}
public static string GetEndTag(string text)
{
if (string.IsNullOrEmpty(text))
{
return string.Empty;
}
text = text.Trim();
if (!text.EndsWith('>'))
{
return string.Empty;
}
string endTag = string.Empty;
int start = text.LastIndexOf("</", StringComparison.Ordinal);
if (start > 0 && start >= text.Length - 8)
{
endTag = text.Substring(start);
}
return endTag;
}
public static string GetStartTag(string text)
{
if (string.IsNullOrEmpty(text))
{
return string.Empty;
}
text = text.Trim();
if (!text.StartsWith('<'))
{
return string.Empty;
}
string startTag = string.Empty;
int end = text.IndexOf('>');
if (end > 0 && end < 25)
{
startTag = text.Substring(0, end + 1);
}
return startTag;
}
}
}

View File

@ -12,10 +12,10 @@ namespace Nikse.SubtitleEdit.Forms
{
public partial class MergeShortLines : PositionAndSizeForm
{
private Subtitle _subtitle;
private Subtitle _mergedSubtitle;
public int NumberOfMerges { get; private set; }
public Subtitle MergedSubtitle { get; private set; }
private Subtitle _subtitle;
public MergeShortLines()
{
@ -27,8 +27,6 @@ namespace Nikse.SubtitleEdit.Forms
SubtitleListview1.HideColumn(SubtitleListView.SubtitleColumn.WordsPerMinute);
}
public Subtitle MergedSubtitle => _mergedSubtitle;
private void MergeShortLines_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
@ -84,7 +82,7 @@ namespace Nikse.SubtitleEdit.Forms
NumberOfMerges = 0;
SubtitleListview1.Items.Clear();
SubtitleListview1.BeginUpdate();
_mergedSubtitle = MergeShortLinesInSubtitle(_subtitle, mergedIndexes, out var count, (double)numericUpDownMaxMillisecondsBetweenLines.Value, (int)numericUpDownMaxCharacters.Value, true);
MergedSubtitle = MergeShortLinesInSubtitle(_subtitle, mergedIndexes, out var count, (double)numericUpDownMaxMillisecondsBetweenLines.Value, (int)numericUpDownMaxCharacters.Value, true);
NumberOfMerges = count;
SubtitleListview1.Fill(_subtitle);
@ -141,13 +139,13 @@ namespace Nikse.SubtitleEdit.Forms
{
if (Utilities.QualifiesForMerge(p, next, maxMillisecondsBetweenLines, maxCharacters, onlyContinuousLines) && IsFixAllowed(p))
{
if (GetStartTag(p.Text) == GetStartTag(next.Text) &&
GetEndTag(p.Text) == GetEndTag(next.Text))
if (MergeShortLinesUtils.GetStartTag(p.Text) == MergeShortLinesUtils.GetStartTag(next.Text) &&
MergeShortLinesUtils.GetEndTag(p.Text) == MergeShortLinesUtils.GetEndTag(next.Text))
{
string s1 = p.Text.Trim();
s1 = s1.Substring(0, s1.Length - GetEndTag(s1).Length);
s1 = s1.Substring(0, s1.Length - MergeShortLinesUtils.GetEndTag(s1).Length);
string s2 = next.Text.Trim();
s2 = s2.Substring(GetStartTag(s2).Length);
s2 = s2.Substring(MergeShortLinesUtils.GetStartTag(s2).Length);
p.Text = Utilities.AutoBreakLine(s1 + Environment.NewLine + s2, language);
}
else
@ -205,50 +203,6 @@ namespace Nikse.SubtitleEdit.Forms
return mergedSubtitle;
}
private static string GetEndTag(string text)
{
if (string.IsNullOrEmpty(text))
{
return string.Empty;
}
text = text.Trim();
if (!text.EndsWith('>'))
{
return string.Empty;
}
string endTag = string.Empty;
int start = text.LastIndexOf("</", StringComparison.Ordinal);
if (start > 0 && start >= text.Length - 8)
{
endTag = text.Substring(start);
}
return endTag;
}
private static string GetStartTag(string text)
{
if (string.IsNullOrEmpty(text))
{
return string.Empty;
}
text = text.Trim();
if (!text.StartsWith('<'))
{
return string.Empty;
}
string startTag = string.Empty;
int end = text.IndexOf('>');
if (end > 0 && end < 25)
{
startTag = text.Substring(0, end + 1);
}
return startTag;
}
private void NumericUpDownMaxCharactersValueChanged(object sender, EventArgs e)
{
Cursor = Cursors.WaitCursor;
@ -302,7 +256,7 @@ namespace Nikse.SubtitleEdit.Forms
SubtitleListview1.Items.Clear();
SubtitleListview1.BeginUpdate();
int count;
_mergedSubtitle = MergeShortLinesInSubtitle(_subtitle, mergedIndexes, out count, (double)numericUpDownMaxMillisecondsBetweenLines.Value, (int)numericUpDownMaxCharacters.Value, false);
MergedSubtitle = MergeShortLinesInSubtitle(_subtitle, mergedIndexes, out count, (double)numericUpDownMaxMillisecondsBetweenLines.Value, (int)numericUpDownMaxCharacters.Value, false);
NumberOfMerges = count;
SubtitleListview1.Fill(_subtitle);
foreach (var index in mergedIndexes)

View File

@ -30,6 +30,8 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
internal enum BatchAction
{
FixCommonErrors,
MergeShortLines,
MergeSameTimeCodes,
RemoveTextForHI,
RemoveFormatting,
ReDoCasing,
@ -111,20 +113,22 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
_stdOutWriter.WriteLine(" /encoding:<encoding name>");
_stdOutWriter.WriteLine(" /pac-codepage:<code page>");
_stdOutWriter.WriteLine(" /track-number:<comma separated track number list>");
_stdOutWriter.WriteLine(" /resolution:<width>x<height> (or <width>,<height>)");
_stdOutWriter.WriteLine(" /resolution:<width>x<height>");
_stdOutWriter.WriteLine(" /inputfolder:<folder name>");
_stdOutWriter.WriteLine(" /outputfolder:<folder name>");
_stdOutWriter.WriteLine(" /overwrite");
_stdOutWriter.WriteLine(" /forcedonly");
_stdOutWriter.WriteLine(" /multiplereplace:<comma separated file name list> ('.' represents the default replace rules)");
_stdOutWriter.WriteLine(" /multiplereplace (equivalent to /multiplereplace:.)");
_stdOutWriter.WriteLine(" Following operations are applied in command line order");
_stdOutWriter.WriteLine(" The following operations are applied in command line order");
_stdOutWriter.WriteLine(" from left to right, and can be specified multiple times.");
_stdOutWriter.WriteLine(" /FixCommonErrors");
_stdOutWriter.WriteLine(" /ReverseRtlStartEnd");
_stdOutWriter.WriteLine(" /RemoveFormatting");
_stdOutWriter.WriteLine(" /RemoveTextForHI");
_stdOutWriter.WriteLine(" /RedoCasing");
_stdOutWriter.WriteLine(" /MergeSameTimeCodes");
_stdOutWriter.WriteLine(" /MergeShortLines");
_stdOutWriter.WriteLine();
_stdOutWriter.WriteLine(" example: SubtitleEdit /convert *.srt sami");
_stdOutWriter.WriteLine(" show this usage message: SubtitleEdit /help");
@ -971,6 +975,18 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
actions.Add(BatchAction.RemoveFormatting);
commandLineArguments.RemoveAt(i);
}
else if (argument.Equals("/mergeshortlines", StringComparison.OrdinalIgnoreCase) ||
argument.Equals("-mergeshortlines", StringComparison.OrdinalIgnoreCase))
{
actions.Add(BatchAction.MergeShortLines);
commandLineArguments.RemoveAt(i);
}
else if (argument.Equals("/mergesametimecodes", StringComparison.OrdinalIgnoreCase) ||
argument.Equals("-mergesametimecodes", StringComparison.OrdinalIgnoreCase))
{
actions.Add(BatchAction.MergeSameTimeCodes);
commandLineArguments.RemoveAt(i);
}
}
actions.Reverse();
return actions;
@ -1118,6 +1134,22 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
p.Text = Utilities.ReverseStartAndEndingForRightToLeft(p.Text);
}
break;
case BatchAction.MergeSameTimeCodes:
var mergedSameTimeCodesSub = Core.Forms.MergeLinesWithSameTimeCodes.Merge(sub, new List<int>(), out _, true, false, 1000, "en", new List<int>(), new Dictionary<int, bool>(), new Subtitle());
if (mergedSameTimeCodesSub.Paragraphs.Count != sub.Paragraphs.Count)
{
sub.Paragraphs.Clear();
sub.Paragraphs.AddRange(mergedSameTimeCodesSub.Paragraphs);
}
break;
case BatchAction.MergeShortLines:
var mergedShortLinesSub = MergeShortLinesUtils.MergeShortLinesInSubtitle(sub, 250, Configuration.Settings.General.SubtitleLineMaximumLength, true);
if (mergedShortLinesSub.Paragraphs.Count != sub.Paragraphs.Count)
{
sub.Paragraphs.Clear();
sub.Paragraphs.AddRange(mergedShortLinesSub.Paragraphs);
}
break;
}
}
}