diff --git a/src/Logic/SubtitleFormats/SubtitleFormat.cs b/src/Logic/SubtitleFormats/SubtitleFormat.cs index 0256a949c..53829882b 100644 --- a/src/Logic/SubtitleFormats/SubtitleFormat.cs +++ b/src/Logic/SubtitleFormats/SubtitleFormat.cs @@ -68,6 +68,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats new UnknownSubtitle5(), new UnknownSubtitle6(), new UnknownSubtitle7(), + new UnknownSubtitle8(), new UTSubtitleXml(), new YouTubeAnnotations(), new YouTubeSbv(), diff --git a/src/Logic/SubtitleFormats/UnknownSubtitle8.cs b/src/Logic/SubtitleFormats/UnknownSubtitle8.cs new file mode 100644 index 000000000..919291223 --- /dev/null +++ b/src/Logic/SubtitleFormats/UnknownSubtitle8.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +namespace Nikse.SubtitleEdit.Logic.SubtitleFormats +{ + public class UnknownSubtitle8 : SubtitleFormat + { + //00:04:04.219 + //The city council of long beach + + readonly Regex _regexTimeCodes = new Regex(@"^\d\d:\d\d:\d\d.\d\d\d$", RegexOptions.Compiled); + + public override string Extension + { + get { return ".txt"; } + } + + public override string Name + { + get { return "Unknown8"; } + } + + public override bool HasLineNumber + { + get { return false; } + } + + public override bool IsTimeBased + { + get { return true; } + } + + public override bool IsMine(List lines, string fileName) + { + var subtitle = new Subtitle(); + LoadSubtitle(subtitle, lines, fileName); + return subtitle.Paragraphs.Count > _errorCount; + } + + public override string ToText(Subtitle subtitle, string title) + { + const string paragraphWriteFormat = "{0}\r\n{1}\r\n"; + + var sb = new StringBuilder(); + foreach (Paragraph p in subtitle.Paragraphs) + { + sb.Append(string.Format(paragraphWriteFormat, p.StartTime.ToString().Replace(",","."), p.Text)); + } + return sb.ToString().Trim(); + } + + public override void LoadSubtitle(Subtitle subtitle, List lines, string fileName) + { + Paragraph paragraph = new Paragraph(); + _errorCount = 0; + subtitle.Paragraphs.Clear(); + for (int i = 0; i < lines.Count; i++) + { + string line = lines[i].TrimEnd(); + string next = string.Empty; + if (i + 1 < lines.Count) + next = lines[i + 1]; + + if (line.Contains(":") && !next.Contains(":") && _regexTimeCodes.IsMatch(line) && !_regexTimeCodes.IsMatch(next)) + { + paragraph = new Paragraph(); + if (TryReadTimeCodesLine(line, paragraph)) + { + paragraph.Text = next; + if (paragraph.Text.Trim().Length > 0) + subtitle.Paragraphs.Add(paragraph); + } + else if (line.Trim().Length > 0) + { + _errorCount++; + } + } + } + + foreach (Paragraph p in subtitle.Paragraphs) + p.Text = p.Text.Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine); + + int index = 0; + foreach (Paragraph p in subtitle.Paragraphs) + { + index++; + Paragraph nextParagraph = subtitle.GetParagraphOrDefault(index); + if (nextParagraph != null) + p.EndTime.TotalMilliseconds = nextParagraph.StartTime.TotalMilliseconds + 1; + else + p.EndTime.TotalMilliseconds = p.StartTime.TotalMilliseconds + 2500; + p.Text = p.Text.Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine); + } + + subtitle.Renumber(1); + } + + private bool TryReadTimeCodesLine(string line, Paragraph paragraph) + { + string[] parts = line.Split(':', '.'); + try + { + int startHours = int.Parse(parts[0]); + int startMinutes = int.Parse(parts[1]); + int startSeconds = int.Parse(parts[2]); + int startMilliseconds = int.Parse(parts[3]); + paragraph.StartTime = new TimeCode(startHours, startMinutes, startSeconds, startMilliseconds); + return true; + } + catch + { + return false; + } + } + } +} + diff --git a/src/SubtitleEdit.csproj b/src/SubtitleEdit.csproj index e1874e48d..a6b449ebb 100644 --- a/src/SubtitleEdit.csproj +++ b/src/SubtitleEdit.csproj @@ -597,6 +597,7 @@ +