Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions src/libse/Common/AssaResampler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,28 @@ private static string FixDrawing(decimal sourceWidth, decimal targetWidth, decim
return sb.ToString().TrimEnd() + s;
}

// The tag regexes below allow whitespace ("\pos (10,11)"), so the parameters cannot be cut
// at a fixed offset from the tag name - take everything between the parentheses instead.
private static string GetTagParameters(string matchValue)
{
var open = matchValue.IndexOf('(');
var close = matchValue.LastIndexOf(')');
if (open < 0 || close <= open)
{
return string.Empty;
}

return matchValue.Substring(open + 1, close - open - 1).RemoveChar(' ');
}

private static string FixMethodFourParameters(decimal sourceWidth, decimal targetWidth, decimal sourceHeight, decimal targetHeight, string input, string tag)
{
var regex = GetCachedRegex("\\\\" + tag + "\\s*\\(\\s*[-+]?\\d+[\\.\\d+]*\\s*,\\s*[-+]?\\d+[\\.\\d+]*\\s*,\\s*[-+]?\\d+[\\.\\d+]*\\s*,\\s*[-+]?\\d+[\\.\\d+]*\\s*\\)");
var s = input;
var match = regex.Match(s);
while (match.Success)
{
var value = match.Value.Substring(tag.Length + 2, match.Value.Length - tag.Length - 3).RemoveChar(' ');
var value = GetTagParameters(match.Value);
var arr = value.Split(',');
if (arr.Length == 4 &&
decimal.TryParse(arr[0], NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var x1) &&
Expand All @@ -212,7 +226,8 @@ private static string FixMethodFourParameters(decimal sourceWidth, decimal targe
}
else
{
break;
// Skip this tag rather than abandoning every remaining tag on the line.
match = regex.Match(s, match.Index + match.Value.Length);
}
}

Expand All @@ -226,7 +241,7 @@ private static string FixMethodSixParametersFourActive(decimal sourceWidth, deci
var match = regex.Match(s);
while (match.Success)
{
var value = match.Value.Substring(tag.Length + 2, match.Value.Length - tag.Length - 3).RemoveChar(' ');
var value = GetTagParameters(match.Value);
var arr = value.Split(',');
if (arr.Length == 6 &&
decimal.TryParse(arr[0], NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var x1) &&
Expand All @@ -252,7 +267,8 @@ private static string FixMethodSixParametersFourActive(decimal sourceWidth, deci
}
else
{
break;
// Skip this tag rather than abandoning every remaining tag on the line.
match = regex.Match(s, match.Index + match.Value.Length);
}
}

Expand All @@ -266,7 +282,7 @@ private static string FixMethodTwoParameters(decimal sourceWidth, decimal target
var match = regex.Match(s);
while (match.Success)
{
var value = match.Value.Substring(tag.Length + 2, match.Value.Length - tag.Length - 3).RemoveChar(' ');
var value = GetTagParameters(match.Value);
var arr = value.Split(',');
if (arr.Length == 2 &&
decimal.TryParse(arr[0], NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var x) &&
Expand All @@ -282,7 +298,8 @@ private static string FixMethodTwoParameters(decimal sourceWidth, decimal target
}
else
{
break;
// Skip this tag rather than abandoning every remaining tag on the line.
match = regex.Match(s, match.Index + match.Value.Length);
}
}

Expand All @@ -306,7 +323,8 @@ private static string FixTagWithNumber(decimal sourceHeight, decimal targetHeigh
}
else
{
break;
// Skip this tag rather than abandoning every remaining tag on the line.
match = regex.Match(s, match.Index + match.Value.Length);
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/libse/Common/Paragraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ public double WordsPerMinute
return 0;
}

// Same guard as GetCharactersPerSecond: a zero or negative duration would give
// infinity/a negative rate, which poisons the min/max/average of every statistics
// report that sums this up.
if (DurationTotalMilliseconds < 1)
{
return 999;
}

return 60.0 / DurationTotalSeconds * Text.CountWords();
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/libse/Common/SsaStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,6 @@ public string ToRawAss(string styleFormat = DefaultAssStyleFormat)
{
sb.Append('1');
}
else if (f == "strikeout")
{
sb.Append('0');
}
else if (f == "scalex")
{
sb.Append(ScaleX.ToString(CultureInfo.InvariantCulture));
Expand Down
5 changes: 3 additions & 2 deletions src/libse/SubtitleFormats/AdvancedSubStationAlpha.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2302,7 +2302,8 @@ public static string CheckForErrors(string header)
{
if (line.Length > 10)
{
var format = line.Substring(8).ToLowerInvariant().Split(',');
// cut the trimmed line - leading whitespace shifted every field name
var format = s.Substring(8).Split(',');
styleCount = format.Length;
for (int i = 0; i < format.Length; i++)
{
Expand Down Expand Up @@ -2403,7 +2404,7 @@ public static string CheckForErrors(string header)
if (line.Length > 10)
{
string rawLine = line;
var format = line.Substring(6).Split(',');
var format = line.Trim().Substring(6).Split(',');

if (format.Length != styleCount)
{
Expand Down
4 changes: 3 additions & 1 deletion src/libse/SubtitleFormats/Csv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string
{
if (CsvLine.IsMatch(line))
{
var parts = line.Split(Separator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
// Only split off the three leading numeric fields - the text is the rest of the
// line and may contain the separator itself (and may be empty).
var parts = line.Split(Separator.ToCharArray(), 4, StringSplitOptions.None);
if (parts.Length == 4)
{
try
Expand Down
9 changes: 6 additions & 3 deletions src/libse/SubtitleFormats/DvdStudioProSpace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,18 @@ public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string
{
if (RegexTimeCodes.IsMatch(line))
{
string[] toPart = line.Substring(0, 25).Split(new[] { " ," }, StringSplitOptions.None);
// Split on the separator instead of fixed offsets - the time code parts are
// "\d+" in the regex above, so they are not always two digits wide, and the
// text may itself contain " , ".
string[] toPart = line.Split(new[] { " , " }, 3, StringSplitOptions.None);
Paragraph p = new Paragraph();
if (toPart.Length == 2 &&
if (toPart.Length == 3 &&
DvdStudioPro.GetTimeCode(p.StartTime, toPart[0]) &&
DvdStudioPro.GetTimeCode(p.EndTime, toPart[1]))
{
number++;
p.Number = number;
string text = line.Substring(27).Trim();
string text = toPart[2].Trim();
p.Text = text.Replace(" | ", Environment.NewLine).Replace("|", Environment.NewLine);
p.Text = DvdStudioPro.DecodeStyles(p.Text);
if (p.Text.Trim().StartsWith("<<Graphic>>"))
Expand Down
9 changes: 6 additions & 3 deletions src/libse/SubtitleFormats/DvdStudioProSpaceGraphic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,18 @@ public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string
{
if (RegexTimeCodes.IsMatch(line))
{
string[] toPart = line.Substring(0, 25).Split(new[] { " ," }, StringSplitOptions.None);
// Split on the separator instead of fixed offsets - the time code parts are
// "\d+" in the regex above, so they are not always two digits wide, and the
// text may itself contain " , ".
string[] toPart = line.Split(new[] { " , " }, 3, StringSplitOptions.None);
Paragraph p = new Paragraph();
if (toPart.Length == 2 &&
if (toPart.Length == 3 &&
DvdStudioPro.GetTimeCode(p.StartTime, toPart[0]) &&
DvdStudioPro.GetTimeCode(p.EndTime, toPart[1]))
{
number++;
p.Number = number;
string text = line.Substring(27).Trim();
string text = toPart[2].Trim();
p.Text = text.Replace(" | ", Environment.NewLine).Replace("|", Environment.NewLine);
p.Text = DvdStudioPro.DecodeStyles(p.Text);
p.Text = DvdStudioPro.GetAlignment(verticalAlign, horizontalAlign) + p.Text;
Expand Down
9 changes: 6 additions & 3 deletions src/libse/SubtitleFormats/DvdStudioProSpaceOne.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,18 @@ public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string
{
if (RegexTimeCodes.IsMatch(line))
{
string[] toPart = line.Substring(0, 24).Trim(',').Split(',');
// Split on the separator instead of fixed offsets - the time code parts are
// "\d+" in the regex above, so they are not always two digits wide, and the
// text may itself contain ",".
string[] toPart = line.Split(new[] { ',' }, 3);
var p = new Paragraph();
if (toPart.Length == 2 &&
if (toPart.Length == 3 &&
DvdStudioPro.GetTimeCode(p.StartTime, toPart[0]) &&
DvdStudioPro.GetTimeCode(p.EndTime, toPart[1]))
{
number++;
p.Number = number;
string text = line.Substring(25).Trim();
string text = toPart[2].Trim();
p.Text = text.Replace(" | ", Environment.NewLine).Replace("|", Environment.NewLine);
p.Text = DvdStudioPro.DecodeStyles(p.Text);
if (italicOn && !p.Text.Contains("<i>"))
Expand Down
9 changes: 6 additions & 3 deletions src/libse/SubtitleFormats/DvdStudioProSpaceOneSemicolon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,18 @@ public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string
{
if (RegexTimeCodes.IsMatch(line))
{
string[] toPart = line.Substring(0, 24).Trim(',').Split(',');
// Split on the separator instead of fixed offsets - the time code parts are
// "\d+" in the regex above, so they are not always two digits wide, and the
// text may itself contain ",".
string[] toPart = line.Split(new[] { ',' }, 3);
var p = new Paragraph();
if (toPart.Length == 2 &&
if (toPart.Length == 3 &&
DvdStudioPro.GetTimeCode(p.StartTime, toPart[0]) &&
DvdStudioPro.GetTimeCode(p.EndTime, toPart[1]))
{
number++;
p.Number = number;
string text = line.Substring(25).Trim();
string text = toPart[2].Trim();
p.Text = text.Replace(" | ", Environment.NewLine).Replace("|", Environment.NewLine);
p.Text = DvdStudioPro.DecodeStyles(p.Text);
if (italicOn && !p.Text.Contains("<i>"))
Expand Down
12 changes: 11 additions & 1 deletion src/libse/SubtitleFormats/WebVttThumbnail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string
continue;
}

if (p.Text.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase))
{
continue;
}

if (p.Text.Contains(".png#xywh=", StringComparison.OrdinalIgnoreCase))
{
continue;
Expand All @@ -57,6 +62,11 @@ public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string
continue;
}

if (p.Text.Contains(".jpeg#xywh=", StringComparison.OrdinalIgnoreCase))
{
continue;
}

return;
}

Expand Down Expand Up @@ -108,7 +118,7 @@ public SKBitmap GetBitmap(string fileName, Subtitle subtitle, int index)
var posJpeg = imageFileName.IndexOf(".jpeg#xywh=", StringComparison.OrdinalIgnoreCase);
if (posJpeg >= 0)
{
string spec = imageFileName.Substring(posJpg + 11); // after ".jepg#xywh="
string spec = imageFileName.Substring(posJpeg + 11); // after ".jpeg#xywh="
imageFileName = imageFileName.Substring(0, posJpeg + 5); // keep ".jpeg"
ParseSpriteSpec(spec, out x, out y, out w, out h);
useSprite = true;
Expand Down
10 changes: 9 additions & 1 deletion src/ui/Features/Video/VideoOcr/VideoOcrFrameGrouper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ public static List<VideoOcrFrameGroup> Group(
var thumbnail = MakeThumbnail(frameFileNames[index], brightnessMinimum);
if (thumbnail == null)
{
continue; // unreadable frame - treat as part of the current group
// Unreadable frame - keep it inside the current group. Skipping it outright left
// the group's EndFrame behind, which shortens the subtitle's end time.
if (current != null)
{
current.EndFrame = index;
currentFileList.Add(frameFileNames[index]);
}

continue;
}

var isBlank = brightnessMinimum > 0 && IsBlank(thumbnail);
Expand Down
14 changes: 13 additions & 1 deletion src/ui/Logic/NetflixQualityCheck/NetflixCheckMaxLineLength.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@ public void Check(Subtitle subtitle, NetflixQualityController controller)
{
foreach (var p in subtitle.Paragraphs)
{
// One record per paragraph - the fix below re-breaks the whole paragraph, so a
// second over-long line would add a duplicate record with the identical fix.
var reported = false;
foreach (var line in p.Text.SplitToLines())
{
if (reported)
{
break;
}

if (controller.Language == "ja")
{
var vertical = p.Text.Contains("{\\an7", StringComparison.Ordinal) || p.Text.Contains("{\\an9", StringComparison.Ordinal);
Expand All @@ -36,6 +44,7 @@ public void Check(Subtitle subtitle, NetflixQualityController controller)
{
var comment = Se.Language.Tools.NetflixCheckAndFix.SingleVerticalLineLengthMax11;
controller.AddRecord(p, p.StartTime.ToHHMMSSFF(), line.Length.ToString(CultureInfo.InvariantCulture), comment, false);
reported = true;
}
}
else // Horizontal subtitles - Maximum 13 full-width characters per line
Expand All @@ -44,6 +53,7 @@ public void Check(Subtitle subtitle, NetflixQualityController controller)
{
var comment = Se.Language.Tools.NetflixCheckAndFix.SingleHorizontalLineLengthMax13;
controller.AddRecord(p, p.StartTime.ToHHMMSSFF(), line.Length.ToString(CultureInfo.InvariantCulture), comment);
reported = true;
}
}
}
Expand All @@ -53,13 +63,15 @@ public void Check(Subtitle subtitle, NetflixQualityController controller)
fixedParagraph.Text = Utilities.AutoBreakLine(fixedParagraph.Text, controller.SingleLineMaxLength, controller.SingleLineMaxLength - 3, controller.Language);
var comment = string.Format(Se.Language.Tools.NetflixCheckAndFix.SingleLineLengthExceedsX, controller.SingleLineMaxLength);
controller.AddRecord(p, fixedParagraph, comment, line.CountCharacters(nameof(CalcCjk), false).ToString(CultureInfo.InvariantCulture), true);
reported = true;
}
else if (line.CountCharacters(false) > controller.SingleLineMaxLength)
{
var fixedParagraph = new Paragraph(p, false);
fixedParagraph.Text = Utilities.AutoBreakLine(fixedParagraph.Text, controller.SingleLineMaxLength, controller.SingleLineMaxLength - 3, controller.Language);
var comment = string.Format(Se.Language.Tools.NetflixCheckAndFix.SingleLineLengthExceedsX, controller.SingleLineMaxLength);
controller.AddRecord(p, fixedParagraph, comment, line.Length.ToString(CultureInfo.InvariantCulture), true );
controller.AddRecord(p, fixedParagraph, comment, line.Length.ToString(CultureInfo.InvariantCulture), true);
reported = true;
}
}
}
Expand Down
Loading
Loading