From cb88036f97f8202d0c927897f49231e98b835f6b Mon Sep 17 00:00:00 2001 From: niksedk Date: Sat, 4 Dec 2021 20:20:49 +0100 Subject: [PATCH] Try to improve VTT parsing - thx Leon :) Wrok on #5562 --- src/libse/SubtitleFormats/WebVTT.cs | 15 +++++++++++---- .../SubtitleFormats/WebVTTFileWithLineNumber.cs | 14 ++++++++++---- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/libse/SubtitleFormats/WebVTT.cs b/src/libse/SubtitleFormats/WebVTT.cs index e6e39f7ee..0307dbe99 100644 --- a/src/libse/SubtitleFormats/WebVTT.cs +++ b/src/libse/SubtitleFormats/WebVTT.cs @@ -164,13 +164,16 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats var header = new StringBuilder(); header.AppendLine("WEBVTT"); header.AppendLine(); + for (var index = 0; index < lines.Count; index++) { - string line = lines[index]; - string next = string.Empty; + var line = lines[index]; + var next = string.Empty; + var isNextTimeCode = false; if (index < lines.Count - 1) { next = lines[index + 1]; + isNextTimeCode = next.Contains("-->"); } if (index == 0 && line.StartsWith("WEBVTT", StringComparison.Ordinal)) @@ -248,7 +251,11 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats s = "00:" + s.Replace("--> ", "--> 00:"); } - if (index == 1 && s.StartsWith("X-TIMESTAMP-MAP=", StringComparison.OrdinalIgnoreCase) && + if (isNextTimeCode && Utilities.IsNumber(s) && p?.Text.Length > 0) + { + numbers++; + } + else if (index == 1 && s.StartsWith("X-TIMESTAMP-MAP=", StringComparison.OrdinalIgnoreCase) && s.IndexOf("MPEGTS:", StringComparison.OrdinalIgnoreCase) > 0) { addSeconds = GetXTimeStampSeconds(s); @@ -316,7 +323,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats subtitle.Paragraphs.Add(p); } - if (subtitle.Paragraphs.Count > 5 && + if (subtitle.Paragraphs.Count > 3 && numbers >= subtitle.Paragraphs.Count - 1 && lines[0] == "WEBVTT FILE") { diff --git a/src/libse/SubtitleFormats/WebVTTFileWithLineNumber.cs b/src/libse/SubtitleFormats/WebVTTFileWithLineNumber.cs index b648616bc..dc775cf7e 100644 --- a/src/libse/SubtitleFormats/WebVTTFileWithLineNumber.cs +++ b/src/libse/SubtitleFormats/WebVTTFileWithLineNumber.cs @@ -63,11 +63,13 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats bool hadEmptyLine = false; for (var index = 0; index < lines.Count; index++) { - string line = lines[index]; - string next = string.Empty; + var line = lines[index]; + var next = string.Empty; + var isNextTimeCode = false; if (index < lines.Count - 1) { next = lines[index + 1]; + isNextTimeCode = next.Contains("-->"); } string s = line; @@ -82,7 +84,12 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats s = "00:" + s.Replace("--> ", "--> 00:"); } - if (isTimeCode && RegexTimeCodes.IsMatch(s)) + + if (isNextTimeCode && Utilities.IsNumber(s) && p?.Text.Length > 0) + { + // skip number + } + else if (isTimeCode && RegexTimeCodes.IsMatch(s)) { if (p != null) { @@ -164,6 +171,5 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats { new WebVTT().RemoveNativeFormatting(subtitle, newFormat); } - } }