Add frame mode for "Bridge gaps between subtitles" - thx Jan :)

This commit is contained in:
Nikolaj Olsson 2020-09-15 07:45:44 +02:00
parent b093b039ab
commit 7dea3eda1c
8 changed files with 76 additions and 12 deletions

View File

@ -359,9 +359,13 @@ Note: Do check free disk space.</WaveFileMalformed>
<Title>Bridge small gaps between subtitles</Title>
<GapsBridgedX>Number of small gaps bridged: {0}</GapsBridgedX>
<GapToNext>Gap to next in seconds</GapToNext>
<GapToNextFrames>Gap to next in frames</GapToNextFrames>
<BridgeGapsSmallerThanXPart1>Bridge gaps smaller than</BridgeGapsSmallerThanXPart1>
<BridgeGapsSmallerThanXPart2>milliseconds</BridgeGapsSmallerThanXPart2>
<BridgeGapsSmallerThanXPart1Frames>Bridge gaps smaller than</BridgeGapsSmallerThanXPart1Frames>
<BridgeGapsSmallerThanXPart2Frames>frames</BridgeGapsSmallerThanXPart2Frames>
<MinMillisecondsBetweenLines>Min. milliseconds between lines</MinMillisecondsBetweenLines>
<MinFramesBetweenLines>Min. frames between lines</MinFramesBetweenLines>
<ProlongEndTime>Previous text takes all gap time</ProlongEndTime>
<DivideEven>Texts divide gap time</DivideEven>
</DurationsBridgeGaps>

View File

@ -1,11 +1,12 @@
using System;
using System.Collections.Generic;
using Nikse.SubtitleEdit.Core.SubtitleFormats;
namespace Nikse.SubtitleEdit.Core.Forms
{
public static class DurationsBridgeGaps
{
public static int BridgeGaps(Subtitle subtitle, int minMsBetweenLines, bool divideEven, double maxMs, List<int> fixedIndexes, Dictionary<string, string> dic)
public static int BridgeGaps(Subtitle subtitle, int minMsBetweenLines, bool divideEven, double maxMs, List<int> fixedIndexes, Dictionary<string, string> dic, bool useFrames)
{
int fixedCount = 0;
if (minMsBetweenLines > maxMs)
@ -43,8 +44,15 @@ namespace Nikse.SubtitleEdit.Core.Forms
}
fixedCount++;
double newGaps = next.StartTime.TotalMilliseconds - cur.EndTime.TotalMilliseconds;
dic?.Add(cur.Id, $"{currentGap / TimeCode.BaseUnit:0.000} => {newGaps / TimeCode.BaseUnit:0.000}");
double newGap = next.StartTime.TotalMilliseconds - cur.EndTime.TotalMilliseconds;
if (useFrames)
{
dic?.Add(cur.Id, $"{SubtitleFormat.MillisecondsToFrames(currentGap)} => {SubtitleFormat.MillisecondsToFrames(newGap)}");
}
else
{
dic?.Add(cur.Id, $"{currentGap / TimeCode.BaseUnit:0.000} => {newGap / TimeCode.BaseUnit:0.000}");
}
}
return fixedCount;

View File

@ -529,9 +529,13 @@ namespace Nikse.SubtitleEdit.Core
Title = "Bridge small gaps between subtitles",
GapsBridgedX = "Number of small gaps bridged: {0}",
GapToNext = "Gap to next in seconds",
GapToNextFrames = "Gap to next in frames",
BridgeGapsSmallerThanXPart1 = "Bridge gaps smaller than",
BridgeGapsSmallerThanXPart2 = "milliseconds",
BridgeGapsSmallerThanXPart1Frames = "Bridge gaps smaller than",
BridgeGapsSmallerThanXPart2Frames = "frames",
MinMillisecondsBetweenLines = "Min. milliseconds between lines",
MinFramesBetweenLines = "Min. frames between lines",
ProlongEndTime = "Previous text takes all gap time",
DivideEven = "Texts divide gap time",
};

View File

@ -931,15 +931,27 @@ namespace Nikse.SubtitleEdit.Core
case "DurationsBridgeGaps/GapToNext":
language.DurationsBridgeGaps.GapToNext = reader.Value;
break;
case "DurationsBridgeGaps/GapToNextFrames":
language.DurationsBridgeGaps.GapToNextFrames = reader.Value;
break;
case "DurationsBridgeGaps/BridgeGapsSmallerThanXPart1":
language.DurationsBridgeGaps.BridgeGapsSmallerThanXPart1 = reader.Value;
break;
case "DurationsBridgeGaps/BridgeGapsSmallerThanXPart2":
language.DurationsBridgeGaps.BridgeGapsSmallerThanXPart2 = reader.Value;
break;
case "DurationsBridgeGaps/BridgeGapsSmallerThanXPart1Frames":
language.DurationsBridgeGaps.BridgeGapsSmallerThanXPart1Frames = reader.Value;
break;
case "DurationsBridgeGaps/BridgeGapsSmallerThanXPart2Frames":
language.DurationsBridgeGaps.BridgeGapsSmallerThanXPart2Frames = reader.Value;
break;
case "DurationsBridgeGaps/MinMillisecondsBetweenLines":
language.DurationsBridgeGaps.MinMillisecondsBetweenLines = reader.Value;
break;
case "DurationsBridgeGaps/MinFramesBetweenLines":
language.DurationsBridgeGaps.MinFramesBetweenLines = reader.Value;
break;
case "DurationsBridgeGaps/ProlongEndTime":
language.DurationsBridgeGaps.ProlongEndTime = reader.Value;
break;

View File

@ -400,9 +400,14 @@
public string Title { get; set; }
public string GapsBridgedX { get; set; }
public string GapToNext { get; set; }
public string GapToNextFrames { get; set; }
public string BridgeGapsSmallerThanXPart1 { get; set; }
public string BridgeGapsSmallerThanXPart2 { get; set; }
public string BridgeGapsSmallerThanXPart1Frames { get; set; }
public string BridgeGapsSmallerThanXPart2Frames { get; set; }
public string MinMillisecondsBetweenLines { get; set; }
public string MinFramesBetweenLines { get; set; }
public string ProlongEndTime { get; set; }
public string DivideEven { get; set; }
}

View File

@ -1340,7 +1340,7 @@ namespace Nikse.SubtitleEdit.Forms
{
if (IsActionEnabled(CommandLineConverter.BatchAction.BridgeGaps))
{
Core.Forms.DurationsBridgeGaps.BridgeGaps(sub, _bridgeGaps.MinMsBetweenLines, !_bridgeGaps.PreviousSubtitleTakesAllTime, Configuration.Settings.Tools.BridgeGapMilliseconds, null, null);
Core.Forms.DurationsBridgeGaps.BridgeGaps(sub, _bridgeGaps.MinMsBetweenLines, !_bridgeGaps.PreviousSubtitleTakesAllTime, Configuration.Settings.Tools.BridgeGapMilliseconds, null, null, false);
}
if (IsActionEnabled(CommandLineConverter.BatchAction.ApplyDurationLimits))

View File

@ -5,6 +5,7 @@ using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Nikse.SubtitleEdit.Core.SubtitleFormats;
namespace Nikse.SubtitleEdit.Forms
{
@ -39,15 +40,12 @@ namespace Nikse.SubtitleEdit.Forms
buttonCancel.Text = Configuration.Settings.Language.General.Cancel;
SubtitleListview1.InitializeLanguage(Configuration.Settings.Language.General, Configuration.Settings);
UiUtil.InitializeSubtitleFont(SubtitleListview1);
SubtitleListview1.ShowExtraColumn(Configuration.Settings.Language.DurationsBridgeGaps.GapToNext);
SubtitleListview1.ShowExtraColumn(Configuration.Settings.General.UseTimeFormatHHMMSSFF ? Configuration.Settings.Language.DurationsBridgeGaps.GapToNextFrames : Configuration.Settings.Language.DurationsBridgeGaps.GapToNext);
SubtitleListview1.AutoSizeAllColumns(this);
labelBridgePart1.Text = Configuration.Settings.Language.DurationsBridgeGaps.BridgeGapsSmallerThanXPart1;
numericUpDownMaxMs.Left = labelBridgePart1.Left + labelBridgePart1.Width + 4;
labelMilliseconds.Text = Configuration.Settings.Language.DurationsBridgeGaps.BridgeGapsSmallerThanXPart2;
labelMilliseconds.Left = numericUpDownMaxMs.Left + numericUpDownMaxMs.Width + 4;
labelMinMsBetweenLines.Text = Configuration.Settings.Language.DurationsBridgeGaps.MinMillisecondsBetweenLines;
numericUpDownMinMsBetweenLines.Left = labelMinMsBetweenLines.Left + labelMinMsBetweenLines.Width + 4;
radioButtonProlongEndTime.Text = Configuration.Settings.Language.DurationsBridgeGaps.ProlongEndTime;
radioButtonDivideEven.Text = Configuration.Settings.Language.DurationsBridgeGaps.DivideEven;
groupBoxLinesFound.Text = string.Empty;
@ -66,6 +64,23 @@ namespace Nikse.SubtitleEdit.Forms
numericUpDownMinMsBetweenLines.Value = Configuration.Settings.General.MinimumMillisecondsBetweenLines;
}
if (Configuration.Settings.General.UseTimeFormatHHMMSSFF)
{
labelBridgePart1.Text = Configuration.Settings.Language.DurationsBridgeGaps.BridgeGapsSmallerThanXPart1Frames;
labelMilliseconds.Text = Configuration.Settings.Language.DurationsBridgeGaps.BridgeGapsSmallerThanXPart2Frames;
labelMinMsBetweenLines.Text = Configuration.Settings.Language.DurationsBridgeGaps.MinFramesBetweenLines;
numericUpDownMaxMs.Value = SubtitleFormat.MillisecondsToFrames((double)numericUpDownMaxMs.Value);
numericUpDownMaxMs.Increment = 1;
numericUpDownMinMsBetweenLines.Value = SubtitleFormat.MillisecondsToFrames((double)numericUpDownMinMsBetweenLines.Value);
numericUpDownMinMsBetweenLines.Increment = 1;
}
numericUpDownMinMsBetweenLines.Left = labelMinMsBetweenLines.Left + labelMinMsBetweenLines.Width + 4;
numericUpDownMaxMs.Left = labelBridgePart1.Left + labelBridgePart1.Width + 4;
labelMilliseconds.Left = numericUpDownMaxMs.Left + numericUpDownMaxMs.Width + 4;
if (subtitle != null)
{
_refreshTimer.Interval = 400;
@ -89,6 +104,11 @@ namespace Nikse.SubtitleEdit.Forms
private void buttonOK_Click(object sender, EventArgs e)
{
Configuration.Settings.Tools.BridgeGapMilliseconds = (int)numericUpDownMaxMs.Value;
if (Configuration.Settings.General.UseTimeFormatHHMMSSFF)
{
Configuration.Settings.Tools.BridgeGapMilliseconds = SubtitleFormat.FramesToMilliseconds((double)numericUpDownMaxMs.Value);
}
DialogResult = DialogResult.OK;
}
@ -121,11 +141,18 @@ namespace Nikse.SubtitleEdit.Forms
_dic = new Dictionary<string, string>();
var fixedIndexes = new List<int>(FixedSubtitle.Paragraphs.Count);
var minMsBetweenLines = (int)numericUpDownMinMsBetweenLines.Value;
FixedCount = Core.Forms.DurationsBridgeGaps.BridgeGaps(FixedSubtitle, minMsBetweenLines, radioButtonDivideEven.Checked, (double)numericUpDownMaxMs.Value, fixedIndexes, _dic);
var maxMs = (double)numericUpDownMaxMs.Value;
if (Configuration.Settings.General.UseTimeFormatHHMMSSFF)
{
minMsBetweenLines = SubtitleFormat.FramesToMilliseconds(minMsBetweenLines);
maxMs = SubtitleFormat.FramesToMilliseconds(maxMs);
}
FixedCount = Core.Forms.DurationsBridgeGaps.BridgeGaps(FixedSubtitle, minMsBetweenLines, radioButtonDivideEven.Checked, maxMs, fixedIndexes, _dic, Configuration.Settings.General.UseTimeFormatHHMMSSFF);
SubtitleListview1.Fill(FixedSubtitle);
for (int i = 0; i < FixedSubtitle.Paragraphs.Count; i++)
{
Paragraph cur = FixedSubtitle.Paragraphs[i];
var cur = FixedSubtitle.Paragraphs[i];
if (_dic.ContainsKey(cur.Id))
{
SubtitleListview1.SetExtraText(i, _dic[cur.Id], SubtitleListview1.ForeColor);
@ -138,6 +165,10 @@ namespace Nikse.SubtitleEdit.Forms
{
var gap = next.StartTime.TotalMilliseconds - cur.EndTime.TotalMilliseconds;
info = $"{ gap / TimeCode.BaseUnit:0.000}";
if (Configuration.Settings.General.UseTimeFormatHHMMSSFF)
{
info = $"{ SubtitleFormat.MillisecondsToFrames(gap)}";
}
}
SubtitleListview1.SetExtraText(i, info, SubtitleListview1.ForeColor);
}

View File

@ -11,7 +11,7 @@ namespace Test.Logic
[TestMethod]
public void InvalidMinMaxTest()
{
Assert.AreEqual(0, DurationsBridgeGaps.BridgeGaps(GetSubtitle(), 1000, true, 10, null, null));
Assert.AreEqual(0, DurationsBridgeGaps.BridgeGaps(GetSubtitle(), 1000, true, 10, null, null, false));
}
[TestMethod]
@ -20,7 +20,7 @@ namespace Test.Logic
var stubDic = new Dictionary<string, string>();
var stubList = new List<int>();
int result = DurationsBridgeGaps.BridgeGaps(GetSubtitle(), 24, true, 100, stubList, stubDic);
int result = DurationsBridgeGaps.BridgeGaps(GetSubtitle(), 24, true, 100, stubList, stubDic, false);
Assert.AreNotEqual(0, result);
// expedtec to contains both p and p + 1 index of adjusted paragraph