From 988b14c075020046b1869918b22d3c637c9e72a1 Mon Sep 17 00:00:00 2001 From: Nikolaj Olsson Date: Tue, 7 Jun 2016 06:31:03 +0200 Subject: [PATCH] Minor subtitle formats refact --- libse/SubtitleFormats/Footage.cs | 2 +- libse/SubtitleFormats/GpacTtxt.cs | 1 - libse/SubtitleFormats/Lrc.cs | 3 +-- libse/SubtitleFormats/MacCaption.cs | 6 ++++-- libse/SubtitleFormats/PListCaption.cs | 3 --- libse/SubtitleFormats/PinnacleImpression.cs | 2 -- libse/SubtitleFormats/QubeMasterImport.cs | 4 ++-- libse/SubtitleFormats/QuickTimeText.cs | 6 ++---- .../ScenaristClosedCaptionsDropFrame.cs | 6 +++--- libse/SubtitleFormats/SeImageHtmlIndex.cs | 8 ++++---- libse/SubtitleFormats/SoftNiColonSub.cs | 6 ++---- libse/SubtitleFormats/SoftNiSub.cs | 6 ++---- .../SonyDVDArchitectExplicitDuration.cs | 4 ++-- libse/SubtitleFormats/Spruce.cs | 18 +++++++++--------- libse/SubtitleFormats/SpruceWithSpace.cs | 4 ++-- libse/SubtitleFormats/Spt.cs | 2 -- libse/SubtitleFormats/SubStationAlpha.cs | 1 + libse/SubtitleFormats/SubtitleFormat.cs | 1 + libse/SubtitleFormats/TSB4.cs | 2 +- libse/SubtitleFormats/Tek.cs | 3 --- libse/SubtitleFormats/Ultech130.cs | 4 ++-- libse/SubtitleFormats/UnknownSubtitle1.cs | 6 +++--- libse/SubtitleFormats/UnknownSubtitle27.cs | 6 ++---- libse/SubtitleFormats/UnknownSubtitle36.cs | 2 +- libse/SubtitleFormats/UnknownSubtitle40.cs | 2 -- libse/SubtitleFormats/UnknownSubtitle46.cs | 6 +++--- libse/SubtitleFormats/UnknownSubtitle50.cs | 3 +-- libse/SubtitleFormats/UnknownSubtitle54.cs | 4 ++-- libse/SubtitleFormats/UnknownSubtitle55.cs | 2 +- libse/SubtitleFormats/UnknownSubtitle56.cs | 2 -- libse/SubtitleFormats/UnknownSubtitle57.cs | 2 -- libse/SubtitleFormats/UnknownSubtitle62.cs | 2 +- libse/SubtitleFormats/UnknownSubtitle63.cs | 2 +- libse/SubtitleFormats/UnknownSubtitle66.cs | 2 +- libse/SubtitleFormats/UnknownSubtitle68.cs | 4 ++-- libse/SubtitleFormats/UnknownSubtitle8.cs | 3 +-- libse/SubtitleFormats/Utx.cs | 1 - libse/SubtitleFormats/Wsb.cs | 2 +- libse/SubtitleFormats/YouTubeSbv.cs | 2 +- libse/SubtitleFormats/YouTubeTranscript.cs | 2 -- 40 files changed, 60 insertions(+), 87 deletions(-) diff --git a/libse/SubtitleFormats/Footage.cs b/libse/SubtitleFormats/Footage.cs index 6cd866fa5..befb5e2d2 100644 --- a/libse/SubtitleFormats/Footage.cs +++ b/libse/SubtitleFormats/Footage.cs @@ -154,7 +154,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats { int frames = MillisecondsToFrames(time.TotalMilliseconds); int footage = frames / 16; - int rest = (int)((frames % 16) / 16.0 * 24.0); + int rest = (int)(frames % 16 / 16.0 * 24.0); return string.Format("{0:00},{1:00}", footage, rest).PadLeft(8); } diff --git a/libse/SubtitleFormats/GpacTtxt.cs b/libse/SubtitleFormats/GpacTtxt.cs index d209d3644..a34755ddf 100644 --- a/libse/SubtitleFormats/GpacTtxt.cs +++ b/libse/SubtitleFormats/GpacTtxt.cs @@ -118,7 +118,6 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats catch { _errorCount = 1; - return; } } diff --git a/libse/SubtitleFormats/Lrc.cs b/libse/SubtitleFormats/Lrc.cs index e4a14e825..1097f8734 100644 --- a/libse/SubtitleFormats/Lrc.cs +++ b/libse/SubtitleFormats/Lrc.cs @@ -90,8 +90,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats { if (line.StartsWith('[') && RegexTimeCodes.Match(line).Success) { - string s = line; - s = line.Substring(1, 8); + string s = line.Substring(1, 8); string[] parts = s.Split(splitChars, StringSplitOptions.RemoveEmptyEntries); if (parts.Length == 3) { diff --git a/libse/SubtitleFormats/MacCaption.cs b/libse/SubtitleFormats/MacCaption.cs index 5939b9b9b..12e1de575 100644 --- a/libse/SubtitleFormats/MacCaption.cs +++ b/libse/SubtitleFormats/MacCaption.cs @@ -125,7 +125,8 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats if (text == "942c 942c" || text == "942c") { - p.EndTime = new TimeCode(startTime.TotalMilliseconds); + if (p != null) + p.EndTime = new TimeCode(startTime.TotalMilliseconds); } else { @@ -139,7 +140,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats { p = subtitle.GetParagraphOrDefault(i); Paragraph next = subtitle.GetParagraphOrDefault(i + 1); - if (p != null && next != null && p.EndTime.TotalMilliseconds == p.StartTime.TotalMilliseconds) + if (p != null && next != null && Math.Abs(p.EndTime.TotalMilliseconds - p.StartTime.TotalMilliseconds) < 0.001) p.EndTime = new TimeCode(next.StartTime.TotalMilliseconds); if (next != null && string.IsNullOrEmpty(next.Text)) subtitle.Paragraphs.Remove(next); @@ -171,6 +172,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats } catch { + // ignored } } string res = sb.ToString().Replace("", string.Empty).Replace("", string.Empty); diff --git a/libse/SubtitleFormats/PListCaption.cs b/libse/SubtitleFormats/PListCaption.cs index 31722d40f..7257bb415 100644 --- a/libse/SubtitleFormats/PListCaption.cs +++ b/libse/SubtitleFormats/PListCaption.cs @@ -74,11 +74,9 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats XmlDocument xml = new XmlDocument(); xml.LoadXml(xmlStructure); XmlNode div = xml.DocumentElement.SelectSingleNode("array"); - int no = 0; foreach (Paragraph p in subtitle.Paragraphs) { XmlNode paragraph = xml.CreateElement("dict"); - string text = HtmlUtil.RemoveHtmlTags(p.Text); XmlNode keyNode = xml.CreateElement("key"); keyNode.InnerText = "in"; @@ -113,7 +111,6 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats paragraph.AppendChild(valueNode); } div.AppendChild(paragraph); - no++; } return ToUtf8XmlString(xml).Replace("", "" + Environment.NewLine + diff --git a/libse/SubtitleFormats/PinnacleImpression.cs b/libse/SubtitleFormats/PinnacleImpression.cs index a5e272d81..ddedecbf5 100644 --- a/libse/SubtitleFormats/PinnacleImpression.cs +++ b/libse/SubtitleFormats/PinnacleImpression.cs @@ -44,13 +44,11 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats sb.AppendLine(@"------------------------------------------------- #INPOINT OUTPOINT PATH -------------------------------------------------"); - int index = 0; foreach (Paragraph p in subtitle.Paragraphs) { //00:03:15:22 00:03:23:10 This is line one. //This is line two. sb.AppendLine(string.Format("{0} {1} {2}", EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), HtmlUtil.RemoveHtmlTags(p.Text))); - index++; } sb.AppendLine(@"-------------------------------------------------"); diff --git a/libse/SubtitleFormats/QubeMasterImport.cs b/libse/SubtitleFormats/QubeMasterImport.cs index dd1e276eb..56be150b7 100644 --- a/libse/SubtitleFormats/QubeMasterImport.cs +++ b/libse/SubtitleFormats/QubeMasterImport.cs @@ -102,7 +102,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats } else if (string.IsNullOrWhiteSpace(line)) { - if (p.StartTime.TotalMilliseconds == 0 && p.EndTime.TotalMilliseconds == 0) + if (Math.Abs(p.StartTime.TotalMilliseconds) < 0.001 && Math.Abs(p.EndTime.TotalMilliseconds) < 0.001) _errorCount++; else subtitle.Paragraphs.Add(p); @@ -125,7 +125,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats bool allNullEndTime = true; for (int i = 0; i < subtitle.Paragraphs.Count; i++) { - if (subtitle.Paragraphs[i].EndTime.TotalMilliseconds != 0) + if (Math.Abs(subtitle.Paragraphs[i].EndTime.TotalMilliseconds) > 0.001) allNullEndTime = false; } if (allNullEndTime) diff --git a/libse/SubtitleFormats/QuickTimeText.cs b/libse/SubtitleFormats/QuickTimeText.cs index cb46f0e40..4b610da8c 100644 --- a/libse/SubtitleFormats/QuickTimeText.cs +++ b/libse/SubtitleFormats/QuickTimeText.cs @@ -36,15 +36,14 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats public override string ToText(Subtitle subtitle, string title) { - const string Header = @"{QTtext} {font:Tahoma} + const string header = @"{QTtext} {font:Tahoma} {plain} {size:20} {timeScale:30} {width:160} {height:32} {timestamps:absolute} {language:0}"; var sb = new StringBuilder(); - sb.AppendLine(Header); - int index = 0; + sb.AppendLine(header); foreach (Paragraph p in subtitle.Paragraphs) { //[00:00:07.12] @@ -55,7 +54,6 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats //tout le temps, //[00:00:35.08] sb.AppendLine(string.Format("{0}{1}{2}", EncodeTimeCode(p.StartTime) + Environment.NewLine, HtmlUtil.RemoveHtmlTags(p.Text) + Environment.NewLine, EncodeTimeCode(p.EndTime) + Environment.NewLine)); - index++; } return sb.ToString(); } diff --git a/libse/SubtitleFormats/ScenaristClosedCaptionsDropFrame.cs b/libse/SubtitleFormats/ScenaristClosedCaptionsDropFrame.cs index 4216b6cba..0b1a390d4 100644 --- a/libse/SubtitleFormats/ScenaristClosedCaptionsDropFrame.cs +++ b/libse/SubtitleFormats/ScenaristClosedCaptionsDropFrame.cs @@ -6,9 +6,9 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats public class ScenaristClosedCaptionsDropFrame : ScenaristClosedCaptions { //00:01:00:29 9420 9420 94ae 94ae 94d0 94d0 4920 f761 7320 ... semi colon (instead of colon) before frame number is used to indicate drop frame - private const string _timeCodeRegEx = @"^\d+:\d\d:\d\d[;,]\d\d\t"; - private static readonly Regex _regex = new Regex(_timeCodeRegEx, RegexOptions.Compiled); - protected override Regex RegexTimeCodes { get { return _regex; } } + private const string TimeCodeRegEx = @"^\d+:\d\d:\d\d[;,]\d\d\t"; + private static readonly Regex Regex = new Regex(TimeCodeRegEx, RegexOptions.Compiled); + protected override Regex RegexTimeCodes { get { return Regex; } } public ScenaristClosedCaptionsDropFrame() { diff --git a/libse/SubtitleFormats/SeImageHtmlIndex.cs b/libse/SubtitleFormats/SeImageHtmlIndex.cs index 09fb0319e..f44010083 100644 --- a/libse/SubtitleFormats/SeImageHtmlIndex.cs +++ b/libse/SubtitleFormats/SeImageHtmlIndex.cs @@ -48,7 +48,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats { if (line.Contains(".png") && RegexTimeCodes.IsMatch(line)) { - int idx = line.IndexOf(" 0) { try @@ -59,9 +59,9 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats var p = new Paragraph(); p.StartTime = DecodeTimeCode(arr[0]); p.EndTime = DecodeTimeCode(arr[1]); - int start = line.IndexOf(" 0) lineSb.Append(Environment.NewLine); - tempLine = line; + var tempLine = line; // This line should be in italics (it was detected in previous line) if (nextLineInItalics) @@ -179,13 +178,12 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats var subtitleLines = text.SplitToLines(); int count = 0; lineSb.Clear(); - string tempLine = string.Empty; foreach (string subtitleLine in subtitleLines) { // Append line break in every line except the first one if (count > 0) lineSb.Append(Environment.NewLine); - tempLine = subtitleLine; + var tempLine = subtitleLine; // Close italics in every line (if next line is in italics, SoftNI will use "[" at the beginning) if (Utilities.CountTagInText(tempLine, "") > Utilities.CountTagInText(tempLine, "")) tempLine = tempLine + ""; diff --git a/libse/SubtitleFormats/SoftNiSub.cs b/libse/SubtitleFormats/SoftNiSub.cs index 205c080bb..cc9b12590 100644 --- a/libse/SubtitleFormats/SoftNiSub.cs +++ b/libse/SubtitleFormats/SoftNiSub.cs @@ -58,7 +58,6 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats var lines = text.SplitToLines(); int count = 0; lineSb.Clear(); - string tempLine = string.Empty; bool nextLineInItalics = false; foreach (string line in lines) { @@ -66,7 +65,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats if (count > 0) lineSb.Append(Environment.NewLine); - tempLine = line; + var tempLine = line; // This line should be in italics (it was detected in previous line) if (nextLineInItalics) @@ -180,13 +179,12 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats var subtitleLines = text.SplitToLines(); int count = 0; lineSb.Clear(); - string tempLine = string.Empty; foreach (string subtitleLine in subtitleLines) { // Append line break in every line except the first one if (count > 0) lineSb.Append(Environment.NewLine); - tempLine = subtitleLine; + var tempLine = subtitleLine; // Close italics in every line (if next line is in italics, SoftNI will use "[" at the beginning) if (Utilities.CountTagInText(tempLine, "") > Utilities.CountTagInText(tempLine, "")) tempLine = tempLine + ""; diff --git a/libse/SubtitleFormats/SonyDVDArchitectExplicitDuration.cs b/libse/SubtitleFormats/SonyDVDArchitectExplicitDuration.cs index 7311c0e5c..f22ce5eeb 100644 --- a/libse/SubtitleFormats/SonyDVDArchitectExplicitDuration.cs +++ b/libse/SubtitleFormats/SonyDVDArchitectExplicitDuration.cs @@ -7,7 +7,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats { public class SonyDVDArchitectExplicitDuration : SubtitleFormat { - private static readonly Regex regex = new Regex(@"^\d\d:\d\d:\d\d\.\d\d\d[ \t]+\d\d:\d\d:\d\d\.\d\d\d[ \t]+\d\d:\d\d:\d\d\.\d\d\d[ \t]+", RegexOptions.Compiled); + private static readonly Regex Regex = new Regex(@"^\d\d:\d\d:\d\d\.\d\d\d[ \t]+\d\d:\d\d:\d\d\.\d\d\d[ \t]+\d\d:\d\d:\d\d\.\d\d\d[ \t]+", RegexOptions.Compiled); public override string Extension { @@ -67,7 +67,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats if (s.Length > 26 && s.IndexOf(':') == 2) { - var match = regex.Match(s); + var match = Regex.Match(s); if (match.Success) { isTimeCode = true; diff --git a/libse/SubtitleFormats/Spruce.cs b/libse/SubtitleFormats/Spruce.cs index 3d40141e6..bc28b954e 100644 --- a/libse/SubtitleFormats/Spruce.cs +++ b/libse/SubtitleFormats/Spruce.cs @@ -38,7 +38,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats public override string ToText(Subtitle subtitle, string title) { - const string Header = @"//Font select and font size + const string header = @"//Font select and font size $FontName = Arial $FontSize = 30 @@ -76,7 +76,7 @@ $ColorIndex4 = 3 //Subtitles"; var sb = new StringBuilder(); - sb.AppendLine(Header); + sb.AppendLine(header); foreach (Paragraph p in subtitle.Paragraphs) { sb.AppendLine(string.Format("{0},{1},{2}", EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), EncodeText(p.Text))); @@ -177,18 +177,18 @@ $ColorIndex4 = 3 return text; } - private static string DecoderTextExtension(string text, string SpruceTag, string htmlOpenTag) + private static string DecoderTextExtension(string text, string spruceTag, string htmlOpenTag) { var htmlCloseTag = htmlOpenTag.Insert(1, "/"); - var idx = text.IndexOf(SpruceTag, StringComparison.Ordinal); - var c = Utilities.CountTagInText(text, SpruceTag); + var idx = text.IndexOf(spruceTag, StringComparison.Ordinal); + var c = Utilities.CountTagInText(text, spruceTag); if (c == 1) { - var l = idx + SpruceTag.Length; + var l = idx + spruceTag.Length; if (l < text.Length) { - text = text.Replace(SpruceTag, htmlOpenTag) + htmlCloseTag; + text = text.Replace(spruceTag, htmlOpenTag) + htmlCloseTag; } else if (l == text.Length) // Brillstein^I { @@ -201,9 +201,9 @@ $ColorIndex4 = 3 while (idx >= 0) { var htmlTag = isOpen ? htmlOpenTag : htmlCloseTag; - text = text.Remove(idx, SpruceTag.Length).Insert(idx, htmlTag); + text = text.Remove(idx, spruceTag.Length).Insert(idx, htmlTag); isOpen = !isOpen; - idx = text.IndexOf(SpruceTag, idx + htmlTag.Length); + idx = text.IndexOf(spruceTag, idx + htmlTag.Length, StringComparison.Ordinal); } } return text; diff --git a/libse/SubtitleFormats/SpruceWithSpace.cs b/libse/SubtitleFormats/SpruceWithSpace.cs index 67900a8c0..85e1479b6 100644 --- a/libse/SubtitleFormats/SpruceWithSpace.cs +++ b/libse/SubtitleFormats/SpruceWithSpace.cs @@ -34,7 +34,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats public override string ToText(Subtitle subtitle, string title) { - const string Header = @"$FontName = Arial + const string header = @"$FontName = Arial $FontSize = 34 $HorzAlign = Left $VertAlign = Bottom @@ -62,7 +62,7 @@ $TapeOffset = FALSE \\Colour 7 = White "; StringBuilder sb = new StringBuilder(); - sb.AppendLine(Header); + sb.AppendLine(header); foreach (Paragraph p in subtitle.Paragraphs) { sb.AppendLine(string.Format("$HorzAlign = Center\r\n{0}, {1}, {2}", EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), EncodeText(p.Text))); diff --git a/libse/SubtitleFormats/Spt.cs b/libse/SubtitleFormats/Spt.cs index 39ba4a05b..42ba9e192 100644 --- a/libse/SubtitleFormats/Spt.cs +++ b/libse/SubtitleFormats/Spt.cs @@ -35,11 +35,9 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats fs.WriteByte(0x60); // paragraphs - int number = 0; foreach (Paragraph p in subtitle.Paragraphs) { WriteParagraph(p); - number++; } // footer diff --git a/libse/SubtitleFormats/SubStationAlpha.cs b/libse/SubtitleFormats/SubStationAlpha.cs index 06022a423..4a20d57af 100644 --- a/libse/SubtitleFormats/SubStationAlpha.cs +++ b/libse/SubtitleFormats/SubStationAlpha.cs @@ -207,6 +207,7 @@ Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text } catch { + // ignored } } diff --git a/libse/SubtitleFormats/SubtitleFormat.cs b/libse/SubtitleFormats/SubtitleFormat.cs index e081f4b09..60302bbb1 100644 --- a/libse/SubtitleFormats/SubtitleFormat.cs +++ b/libse/SubtitleFormats/SubtitleFormat.cs @@ -262,6 +262,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats } catch { + // ignored } } } diff --git a/libse/SubtitleFormats/TSB4.cs b/libse/SubtitleFormats/TSB4.cs index 91c499676..b65737db6 100644 --- a/libse/SubtitleFormats/TSB4.cs +++ b/libse/SubtitleFormats/TSB4.cs @@ -24,7 +24,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats { var subtitle = new Subtitle(); LoadSubtitle(subtitle, lines, fileName); - return subtitle.Paragraphs.Count > this._errorCount; + return subtitle.Paragraphs.Count > _errorCount; } public override string ToText(Subtitle subtitle, string title) diff --git a/libse/SubtitleFormats/Tek.cs b/libse/SubtitleFormats/Tek.cs index b70ecbf8e..4180c63a2 100644 --- a/libse/SubtitleFormats/Tek.cs +++ b/libse/SubtitleFormats/Tek.cs @@ -58,13 +58,10 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats ý ý Kraj info blocka."); sb.AppendLine(); - int count = 0; - if (!subtitle.WasLoadedWithFrameNumbers) subtitle.CalculateFrameNumbersFromTimeCodes(Configuration.Settings.General.CurrentFrameRate); foreach (Paragraph p in subtitle.Paragraphs) { - count++; var text = HtmlUtil.RemoveOpenCloseTags(p.Text, HtmlUtil.TagFont); sb.AppendLine(string.Format(paragraphWriteFormat, p.StartFrame, p.EndFrame, text)); } diff --git a/libse/SubtitleFormats/Ultech130.cs b/libse/SubtitleFormats/Ultech130.cs index 31d7f2e07..e9d25306e 100644 --- a/libse/SubtitleFormats/Ultech130.cs +++ b/libse/SubtitleFormats/Ultech130.cs @@ -230,7 +230,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats int length = buffer[i + 1]; p.StartTime = DecodeTimestamp(buffer, i + 3); - if (last != null && last.EndTime.TotalMilliseconds == 0) + if (last != null && Math.Abs(last.EndTime.TotalMilliseconds) < 0.001) last.EndTime.TotalMilliseconds = p.StartTime.TotalMilliseconds - 1; if (length > 22) @@ -448,7 +448,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats } if (last != null) { - if (last.EndTime.TotalMilliseconds == 0) + if (Math.Abs(last.EndTime.TotalMilliseconds) < 0.001) last.EndTime.TotalMilliseconds = last.StartTime.TotalMilliseconds + 2500; if (last.Duration.TotalMilliseconds > Configuration.Settings.General.SubtitleMaximumDisplayMilliseconds) last.EndTime.TotalMilliseconds = last.StartTime.TotalMilliseconds + Utilities.GetOptimalDisplayMilliseconds(last.Text); diff --git a/libse/SubtitleFormats/UnknownSubtitle1.cs b/libse/SubtitleFormats/UnknownSubtitle1.cs index 0d61e7fa0..1ba52de82 100644 --- a/libse/SubtitleFormats/UnknownSubtitle1.cs +++ b/libse/SubtitleFormats/UnknownSubtitle1.cs @@ -68,13 +68,13 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats subtitle.Paragraphs.Clear(); var sb = new StringBuilder(); char[] splitChars = { '–', ' ' }; - Match match = null; foreach (string line in lines) { + Match match; if (line.Length > 11 && (match = RegexTimeCodes.Match(line)).Success) { if (p != null) - p.Text = (p.Text + Environment.NewLine + sb.ToString()).Trim(); + p.Text = (p.Text + Environment.NewLine + sb).Trim(); var parts = line.Substring(0, match.Length).Split(splitChars, StringSplitOptions.RemoveEmptyEntries); try { @@ -101,7 +101,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats return; } if (p != null) - p.Text = (p.Text + Environment.NewLine + sb.ToString()).Trim(); + p.Text = (p.Text + Environment.NewLine + sb).Trim(); subtitle.Renumber(); } diff --git a/libse/SubtitleFormats/UnknownSubtitle27.cs b/libse/SubtitleFormats/UnknownSubtitle27.cs index 0065f4c92..5483a048b 100644 --- a/libse/SubtitleFormats/UnknownSubtitle27.cs +++ b/libse/SubtitleFormats/UnknownSubtitle27.cs @@ -7,7 +7,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats { public class UnknownSubtitle27 : SubtitleFormat { - private static readonly Regex regexTimeCodes = new Regex(@"^\d\d:\d\d:\d\d: ", RegexOptions.Compiled); + private static readonly Regex RegexTimeCodes = new Regex(@"^\d\d:\d\d:\d\d: ", RegexOptions.Compiled); public override string Extension { @@ -39,10 +39,8 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats public override string ToText(Subtitle subtitle, string title) { var sb = new StringBuilder(); - int index = 0; foreach (Paragraph p in subtitle.Paragraphs) { - index++; //00:18:02: (斉藤)失礼な大人って! (悠子)何言ってんのあんた? string text = HtmlUtil.RemoveHtmlTags(p.Text); text = text.Replace(Environment.NewLine, " "); @@ -70,7 +68,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats foreach (string line in lines) { string s = line.Trim(); - if (regexTimeCodes.IsMatch(s)) + if (RegexTimeCodes.IsMatch(s)) { var temp = s.Substring(0, 8); diff --git a/libse/SubtitleFormats/UnknownSubtitle36.cs b/libse/SubtitleFormats/UnknownSubtitle36.cs index 1c9c8192b..a8f5dafd2 100644 --- a/libse/SubtitleFormats/UnknownSubtitle36.cs +++ b/libse/SubtitleFormats/UnknownSubtitle36.cs @@ -74,7 +74,6 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats _errorCount = 0; int number = 1; char[] splitChar = { ',' }; - Match match = null; foreach (string line in lines) { if (string.IsNullOrWhiteSpace(line) || string.IsNullOrWhiteSpace(line.Trim('-'))) @@ -82,6 +81,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats continue; } + Match match; if (line.Length > 57 && (match = RegexTimeCodes.Match(line)).Success) { try diff --git a/libse/SubtitleFormats/UnknownSubtitle40.cs b/libse/SubtitleFormats/UnknownSubtitle40.cs index 87edaec1a..be8af36a0 100644 --- a/libse/SubtitleFormats/UnknownSubtitle40.cs +++ b/libse/SubtitleFormats/UnknownSubtitle40.cs @@ -35,11 +35,9 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats public override string ToText(Subtitle subtitle, string title) { var sb = new StringBuilder(); - int index = 0; foreach (Paragraph p in subtitle.Paragraphs) { sb.AppendLine(string.Format("{0} – {1}{2}{3}", EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), Environment.NewLine, HtmlUtil.RemoveHtmlTags(p.Text))); - index++; } return sb.ToString(); } diff --git a/libse/SubtitleFormats/UnknownSubtitle46.cs b/libse/SubtitleFormats/UnknownSubtitle46.cs index d2cf510bf..3081c4805 100644 --- a/libse/SubtitleFormats/UnknownSubtitle46.cs +++ b/libse/SubtitleFormats/UnknownSubtitle46.cs @@ -8,8 +8,8 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats public class UnknownSubtitle46 : SubtitleFormat { //7:00:01:27AM - private static readonly Regex regexTimeCodesAM = new Regex(@"^\d\:\d\d\:\d\d\:\d\dAM", RegexOptions.Compiled); - private static readonly Regex regexTimeCodesPM = new Regex(@"^\d\:\d\d\:\d\d\:\d\dPM", RegexOptions.Compiled); + private static readonly Regex RegexTimeCodesAm = new Regex(@"^\d\:\d\d\:\d\d\:\d\dAM", RegexOptions.Compiled); + private static readonly Regex RegexTimeCodesPm = new Regex(@"^\d\:\d\d\:\d\d\:\d\dPM", RegexOptions.Compiled); public override string Extension { @@ -60,7 +60,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats string s = line.Trim(); string[] arr = line.Split(); var timeCode = arr[arr.Length - 1]; - if (regexTimeCodesAM.Match(timeCode).Success || regexTimeCodesPM.Match(timeCode).Success) + if (RegexTimeCodesAm.Match(timeCode).Success || RegexTimeCodesPm.Match(timeCode).Success) { try { diff --git a/libse/SubtitleFormats/UnknownSubtitle50.cs b/libse/SubtitleFormats/UnknownSubtitle50.cs index 238b77cf2..e82532a3e 100644 --- a/libse/SubtitleFormats/UnknownSubtitle50.cs +++ b/libse/SubtitleFormats/UnknownSubtitle50.cs @@ -90,9 +90,8 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats public override void LoadSubtitle(Subtitle subtitle, List lines, string fileName) { - ExpectingLine expecting = ExpectingLine.TimeCodes; Paragraph p = new Paragraph(); - expecting = ExpectingLine.TimeCodes; + var expecting = ExpectingLine.TimeCodes; _errorCount = 0; subtitle.Paragraphs.Clear(); diff --git a/libse/SubtitleFormats/UnknownSubtitle54.cs b/libse/SubtitleFormats/UnknownSubtitle54.cs index d670f0682..0b23a4233 100644 --- a/libse/SubtitleFormats/UnknownSubtitle54.cs +++ b/libse/SubtitleFormats/UnknownSubtitle54.cs @@ -102,7 +102,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats } else if (string.IsNullOrWhiteSpace(line)) { - if (p.StartTime.TotalMilliseconds == 0 && p.EndTime.TotalMilliseconds == 0) + if (Math.Abs(p.StartTime.TotalMilliseconds) < 0.001 && Math.Abs(p.EndTime.TotalMilliseconds) < 0.001) _errorCount++; else subtitle.Paragraphs.Add(p); @@ -125,7 +125,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats bool allNullEndTime = true; for (int i = 0; i < subtitle.Paragraphs.Count; i++) { - if (subtitle.Paragraphs[i].EndTime.TotalMilliseconds != 0) + if (Math.Abs(subtitle.Paragraphs[i].EndTime.TotalMilliseconds) > 0.001) allNullEndTime = false; } if (allNullEndTime) diff --git a/libse/SubtitleFormats/UnknownSubtitle55.cs b/libse/SubtitleFormats/UnknownSubtitle55.cs index c36d4dd62..8a407e850 100644 --- a/libse/SubtitleFormats/UnknownSubtitle55.cs +++ b/libse/SubtitleFormats/UnknownSubtitle55.cs @@ -104,7 +104,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats } else if (string.IsNullOrWhiteSpace(line)) { - if (p.StartTime.TotalMilliseconds == 0 && p.EndTime.TotalMilliseconds == 0) + if (Math.Abs(p.StartTime.TotalMilliseconds) < 0.001 && Math.Abs(p.EndTime.TotalMilliseconds) < 0.001) _errorCount++; else subtitle.Paragraphs.Add(p); diff --git a/libse/SubtitleFormats/UnknownSubtitle56.cs b/libse/SubtitleFormats/UnknownSubtitle56.cs index 7991f825f..e60105fea 100644 --- a/libse/SubtitleFormats/UnknownSubtitle56.cs +++ b/libse/SubtitleFormats/UnknownSubtitle56.cs @@ -41,13 +41,11 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats { var sb = new StringBuilder(); const string format = "{0:0000}\t{1}\t{2}"; - int count = 1; foreach (Paragraph p in subtitle.Paragraphs) { sb.AppendLine(string.Format(format, 1, EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime))); sb.AppendLine(HtmlUtil.RemoveHtmlTags(p.Text)); sb.AppendLine(); - count++; } return sb.ToString(); } diff --git a/libse/SubtitleFormats/UnknownSubtitle57.cs b/libse/SubtitleFormats/UnknownSubtitle57.cs index d383f759a..1f557865d 100644 --- a/libse/SubtitleFormats/UnknownSubtitle57.cs +++ b/libse/SubtitleFormats/UnknownSubtitle57.cs @@ -39,12 +39,10 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats public override string ToText(Subtitle subtitle, string title) { var sb = new StringBuilder(); - int index = 0; foreach (Paragraph p in subtitle.Paragraphs) { //00:00:54.08 00:00:58.06 - Saucers... - ... a dry lake bed. (newline is //) sb.AppendLine(string.Format("{0} {1} {2}", EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), HtmlUtil.RemoveHtmlTags(p.Text).Replace(Environment.NewLine, "//"))); - index++; } return sb.ToString(); } diff --git a/libse/SubtitleFormats/UnknownSubtitle62.cs b/libse/SubtitleFormats/UnknownSubtitle62.cs index 51125d3a5..30ded3c7c 100644 --- a/libse/SubtitleFormats/UnknownSubtitle62.cs +++ b/libse/SubtitleFormats/UnknownSubtitle62.cs @@ -93,7 +93,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats } else if (string.IsNullOrWhiteSpace(line)) { - if (p.StartTime.TotalMilliseconds == 0 && p.EndTime.TotalMilliseconds == 0) + if (Math.Abs(p.StartTime.TotalMilliseconds) < 0.001 && Math.Abs(p.EndTime.TotalMilliseconds) < 0.001) _errorCount++; else subtitle.Paragraphs.Add(p); diff --git a/libse/SubtitleFormats/UnknownSubtitle63.cs b/libse/SubtitleFormats/UnknownSubtitle63.cs index 95e1501bf..05523da5c 100644 --- a/libse/SubtitleFormats/UnknownSubtitle63.cs +++ b/libse/SubtitleFormats/UnknownSubtitle63.cs @@ -102,7 +102,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats } else if (string.IsNullOrWhiteSpace(line)) { - if (p.StartTime.TotalMilliseconds == 0 && p.EndTime.TotalMilliseconds == 0) + if (Math.Abs(p.StartTime.TotalMilliseconds) < 0.001 && Math.Abs(p.EndTime.TotalMilliseconds) < 0.001) _errorCount++; else subtitle.Paragraphs.Add(p); diff --git a/libse/SubtitleFormats/UnknownSubtitle66.cs b/libse/SubtitleFormats/UnknownSubtitle66.cs index f70924f72..3eecf28b3 100644 --- a/libse/SubtitleFormats/UnknownSubtitle66.cs +++ b/libse/SubtitleFormats/UnknownSubtitle66.cs @@ -106,7 +106,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats } else if (string.IsNullOrWhiteSpace(line)) { - if (p.StartTime.TotalMilliseconds == 0 && p.EndTime.TotalMilliseconds == 0) + if (Math.Abs(p.StartTime.TotalMilliseconds) < 0.001 && Math.Abs(p.EndTime.TotalMilliseconds) < 0.001) _errorCount++; else subtitle.Paragraphs.Add(p); diff --git a/libse/SubtitleFormats/UnknownSubtitle68.cs b/libse/SubtitleFormats/UnknownSubtitle68.cs index d4714af3d..b4be3f4cb 100644 --- a/libse/SubtitleFormats/UnknownSubtitle68.cs +++ b/libse/SubtitleFormats/UnknownSubtitle68.cs @@ -119,7 +119,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats try { TimeCode start = DecodeTimeCodeFrames(timeParts[0].Substring(0, 11), splitChars); - if (p != null && p.EndTime.TotalMilliseconds == 0) + if (p != null && Math.Abs(p.EndTime.TotalMilliseconds) < 0.001) p.EndTime.TotalMilliseconds = start.TotalMilliseconds - Configuration.Settings.General.MinimumMillisecondsBetweenLines; TimeCode end = new TimeCode(0, 0, 0, 0); p = MakeTextParagraph(text, p, start, end); @@ -136,7 +136,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats try { TimeCode start = DecodeTimeCodeFrames(timeParts[0].Substring(0, 11), splitChars); - if (p != null && p.EndTime.TotalMilliseconds == 0) + if (p != null && Math.Abs(p.EndTime.TotalMilliseconds) < 0.001) p.EndTime.TotalMilliseconds = start.TotalMilliseconds - Configuration.Settings.General.MinimumMillisecondsBetweenLines; TimeCode end = DecodeTimeCodeFrames(timeParts[1].Substring(0, 11), splitChars); p = MakeTextParagraph(text, p, start, end); diff --git a/libse/SubtitleFormats/UnknownSubtitle8.cs b/libse/SubtitleFormats/UnknownSubtitle8.cs index 69011d1ac..201523406 100644 --- a/libse/SubtitleFormats/UnknownSubtitle8.cs +++ b/libse/SubtitleFormats/UnknownSubtitle8.cs @@ -48,7 +48,6 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats public override void LoadSubtitle(Subtitle subtitle, List lines, string fileName) { - var paragraph = new Paragraph(); _errorCount = 0; subtitle.Paragraphs.Clear(); for (int i = 0; i < lines.Count; i++) @@ -60,7 +59,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats if (line.Contains(':') && !next.Contains(':') && RegexTimeCodes.IsMatch(line) && !RegexTimeCodes.IsMatch(next)) { - paragraph = new Paragraph(); + var paragraph = new Paragraph(); if (TryReadTimeCodesLine(line, paragraph)) { paragraph.Text = next; diff --git a/libse/SubtitleFormats/Utx.cs b/libse/SubtitleFormats/Utx.cs index 13f65ee3a..71f934e0c 100644 --- a/libse/SubtitleFormats/Utx.cs +++ b/libse/SubtitleFormats/Utx.cs @@ -46,7 +46,6 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats var sb = new StringBuilder(); foreach (Paragraph p in subtitle.Paragraphs) { - string text = p.Text; sb.AppendLine(string.Format(paragraphWriteFormat, p.Text, Environment.NewLine, EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime))); } return sb.ToString().Trim(); diff --git a/libse/SubtitleFormats/Wsb.cs b/libse/SubtitleFormats/Wsb.cs index 0a4cd9d32..339ae8580 100644 --- a/libse/SubtitleFormats/Wsb.cs +++ b/libse/SubtitleFormats/Wsb.cs @@ -82,7 +82,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats var endTime = time.Substring(8); string[] startTimeParts = { starTime.Substring(0, 2), starTime.Substring(2, 2), starTime.Substring(4, 2), starTime.Substring(6, 2) }; - string[] endTimeParts = { starTime.Substring(0, 2), starTime.Substring(2, 2), starTime.Substring(4, 2), starTime.Substring(6, 2) }; + string[] endTimeParts = { endTime.Substring(0, 2), endTime.Substring(2, 2), endTime.Substring(4, 2), endTime.Substring(6, 2) }; p = new Paragraph(DecodeTimeCodeFramesFourParts(startTimeParts), DecodeTimeCodeFramesFourParts(endTimeParts), text); subtitle.Paragraphs.Add(p); diff --git a/libse/SubtitleFormats/YouTubeSbv.cs b/libse/SubtitleFormats/YouTubeSbv.cs index 646e552a2..41d225a9f 100644 --- a/libse/SubtitleFormats/YouTubeSbv.cs +++ b/libse/SubtitleFormats/YouTubeSbv.cs @@ -89,7 +89,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats ReadLine(subtitle, line, next); } - if (!string.IsNullOrWhiteSpace(_paragraph.Text)) + if (_paragraph != null && !string.IsNullOrWhiteSpace(_paragraph.Text)) subtitle.Paragraphs.Add(_paragraph); foreach (Paragraph p in subtitle.Paragraphs) diff --git a/libse/SubtitleFormats/YouTubeTranscript.cs b/libse/SubtitleFormats/YouTubeTranscript.cs index 10e135b40..7074afd06 100644 --- a/libse/SubtitleFormats/YouTubeTranscript.cs +++ b/libse/SubtitleFormats/YouTubeTranscript.cs @@ -39,11 +39,9 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats public override string ToText(Subtitle subtitle, string title) { var sb = new StringBuilder(); - int index = 0; foreach (Paragraph p in subtitle.Paragraphs) { sb.AppendLine(string.Format("{0}" + Environment.NewLine + "{1}", EncodeTimeCode(p.StartTime), HtmlUtil.RemoveHtmlTags(p.Text.Replace(Environment.NewLine, " ")))); - index++; } return sb.ToString(); }