Merge pull request #3374 from ivandrofly/bridge-gaps

BridgeGaps: Update + Unit-test
This commit is contained in:
Nikolaj Olsson 2019-02-22 10:14:15 +01:00 committed by GitHub
commit 792712fc40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 84 additions and 39 deletions

View File

@ -5,48 +5,47 @@ namespace Nikse.SubtitleEdit.Core.Forms
{
public static class DurationsBridgeGaps
{
public static int BridgeGaps(Subtitle fixedSubtitle, 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)
{
int fixedCount = 0;
for (int i = 0; i < fixedSubtitle.Paragraphs.Count - 1; i++)
if (minMsBetweenLines > maxMs)
{
Paragraph cur = fixedSubtitle.Paragraphs[i];
Paragraph next = fixedSubtitle.Paragraphs[i + 1];
string before = null;
var difMs = Math.Abs(cur.EndTime.TotalMilliseconds - next.StartTime.TotalMilliseconds);
if (difMs < maxMs && difMs > minMsBetweenLines && maxMs > minMsBetweenLines)
{
before = $"{(next.StartTime.TotalMilliseconds - cur.EndTime.TotalMilliseconds) / TimeCode.BaseUnit:0.000}";
if (divideEven && next.StartTime.TotalMilliseconds > cur.EndTime.TotalMilliseconds)
{
double half = (next.StartTime.TotalMilliseconds - cur.EndTime.TotalMilliseconds) / 2.0;
next.StartTime.TotalMilliseconds -= half;
}
cur.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - minMsBetweenLines;
if (fixedIndexes != null)
{
fixedIndexes.Add(i);
fixedIndexes.Add(i + 1);
}
fixedCount++;
}
var msToNext = next.StartTime.TotalMilliseconds - cur.EndTime.TotalMilliseconds;
if (msToNext < 2000)
{
string info;
if (!string.IsNullOrEmpty(before))
{
info = $"{before} => {msToNext / TimeCode.BaseUnit:0.000}";
}
else
{
info = $"{msToNext / TimeCode.BaseUnit:0.000}";
}
dic?.Add(cur.ID, info);
}
string message = $"{nameof(DurationsBridgeGaps)}: {nameof(minMsBetweenLines)} cannot be smaller than {nameof(maxMs)}!";
SeLogger.Error(new InvalidOperationException(message), message);
return 0;
}
return fixedCount;
int count = subtitle.Paragraphs.Count - 1;
for (int i = 0; i < count; i++)
{
Paragraph cur = subtitle.Paragraphs[i];
Paragraph next = subtitle.Paragraphs[i + 1];
double currentGaps = next.StartTime.TotalMilliseconds - cur.EndTime.TotalMilliseconds;
// there shouldn't be adjustment if current gaps is shorter than minimum gaps or greater than maximum gaps
if (currentGaps < minMsBetweenLines || currentGaps > maxMs)
{
continue;
}
// next paragraph start-time will be pull to try to meet the current parragraph
if (divideEven)
{
next.StartTime.TotalMilliseconds -= currentGaps / 2.0;
}
cur.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - minMsBetweenLines;
if (fixedIndexes != null)
{
fixedIndexes.Add(i);
fixedIndexes.Add(i + 1);
}
double newGaps = next.StartTime.TotalMilliseconds - cur.EndTime.TotalMilliseconds;
dic?.Add(cur.ID, $"{currentGaps / TimeCode.BaseUnit:0.000} => {newGaps / TimeCode.BaseUnit:0.000}");
}
return fixedIndexes.Count / 2;
}
}
}

View File

@ -0,0 +1,45 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nikse.SubtitleEdit.Core;
using Nikse.SubtitleEdit.Core.Forms;
using System.Collections.Generic;
namespace Test.Logic
{
[TestClass]
public class BridgeGapsTest
{
[TestMethod]
public void InvalidMinMaxTest()
{
Assert.AreEqual(0, DurationsBridgeGaps.BridgeGaps(GetSubtitle(), 1000, true, 10, null, null));
}
[TestMethod]
public void AdjustGapsTest()
{
var stubDic = new Dictionary<string, string>();
var stubList = new List<int>();
int result = DurationsBridgeGaps.BridgeGaps(GetSubtitle(), 24, true, 100, stubList, stubDic);
Assert.AreNotEqual(0, result);
// expedtec to contains both p and p + 1 index of adjusted paragraph
Assert.AreEqual(6, stubList.Count);
// expected to contains only index of adjusted paragraph
Assert.AreEqual(3, stubDic.Count);
}
public static Subtitle GetSubtitle()
{
var paragraphs = new List<Paragraph>
{
new Paragraph("", TimeCode.ParseToMilliseconds("00:00:49,520"), TimeCode.ParseToMilliseconds("00:00:52,390")),
new Paragraph("", TimeCode.ParseToMilliseconds("00:00:52,470"), TimeCode.ParseToMilliseconds("00:00:55,100")),
new Paragraph("", TimeCode.ParseToMilliseconds("00:00:55,180"), TimeCode.ParseToMilliseconds("00:00:57,060")),
new Paragraph("", TimeCode.ParseToMilliseconds("00:00:57,140"), TimeCode.ParseToMilliseconds("00:01:01,100")),
};
return new Subtitle(paragraphs);
}
}
}

View File

@ -51,6 +51,7 @@
<Compile Include="Core\NikseBitmapTest.cs" />
<Compile Include="Core\SubtitleTest.cs" />
<Compile Include="Core\RichTextToPlainTextTest.cs" />
<Compile Include="Logic\BridgeGapsTest.cs" />
<Compile Include="Logic\NetflixQualityCheckTest.cs" />
<Compile Include="Logic\Ocr\BinaryOcrTest.cs" />
<Compile Include="Core\HtmlUtilTest.cs" />