From 540ebfcf7286f33671e4cefac500fe2f09998be9 Mon Sep 17 00:00:00 2001 From: niksedk Date: Mon, 14 Dec 2015 12:26:04 +0100 Subject: [PATCH] Added new subtitle format --- libse/LibSE.csproj | 1 + libse/SubtitleFormats/SubtitleFormat.cs | 1 + libse/SubtitleFormats/UnknownSubtitle80.cs | 134 +++++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 libse/SubtitleFormats/UnknownSubtitle80.cs diff --git a/libse/LibSE.csproj b/libse/LibSE.csproj index d7e4db4f2..b0231c321 100644 --- a/libse/LibSE.csproj +++ b/libse/LibSE.csproj @@ -358,6 +358,7 @@ + diff --git a/libse/SubtitleFormats/SubtitleFormat.cs b/libse/SubtitleFormats/SubtitleFormat.cs index 3fd106cc3..e12db6cc6 100644 --- a/libse/SubtitleFormats/SubtitleFormat.cs +++ b/libse/SubtitleFormats/SubtitleFormat.cs @@ -231,6 +231,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats new UnknownSubtitle77(), new UnknownSubtitle78(), new UnknownSubtitle79(), + new UnknownSubtitle80(), }; string path = Configuration.PluginsDirectory; diff --git a/libse/SubtitleFormats/UnknownSubtitle80.cs b/libse/SubtitleFormats/UnknownSubtitle80.cs new file mode 100644 index 000000000..7e34a5432 --- /dev/null +++ b/libse/SubtitleFormats/UnknownSubtitle80.cs @@ -0,0 +1,134 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +namespace Nikse.SubtitleEdit.Core.SubtitleFormats +{ + public class UnknownSubtitle80 : SubtitleFormat + { + + // 1 01033902/01034028 xxx + private static readonly Regex RegexTimeCode = new Regex(@"^\d+\t\d+\/\d+\t", RegexOptions.Compiled); + + public override string Extension + { + get { return ".cap"; } + } + + public override string Name + { + get { return "Unknown 80"; } + } + + 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}\t{1}/{2}\t{3}"; + var sb = new StringBuilder(); + sb.AppendLine("Lambda字幕V4 DF1+1 SCENE\"和文標準\""); + sb.AppendLine(); + int count = 1; + foreach (Paragraph p in subtitle.Paragraphs) + { + var text = HtmlUtil.RemoveHtmlTags(p.Text.Trim()).Replace(Environment.NewLine, Environment.NewLine + "\t\t\t\t"); + sb.AppendLine(string.Format(paragraphWriteFormat, count, EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), text)); + count++; + } + return sb.ToString(); + } + + public override void LoadSubtitle(Subtitle subtitle, List lines, string fileName) + { + Paragraph paragraph = null; + _errorCount = 0; + subtitle.Paragraphs.Clear(); + var text = new StringBuilder(); + foreach (string line in lines) + { + string s = line.Trim(); + if (s.Length > 19 && RegexTimeCode.IsMatch(s)) + { + var lineParts = s.Split('\t'); + var parts = lineParts[1].Split('/'); + if (parts.Length == 2) + { + if (text.Length > 0 && paragraph != null) + { + paragraph.Text = text.ToString().Trim(); + } + try + { + paragraph = new Paragraph { StartTime = DecodeTimeCode(parts[0]), EndTime = DecodeTimeCode(parts[1]) }; + subtitle.Paragraphs.Add(paragraph); + text = new StringBuilder(); + s = s.Remove(0, 18 + lineParts[0].Length).Trim(); + var idxA = s.IndexOf("@"); + if (idxA > 0) + { + s = s.Substring(0, idxA - 1).Trim(); + } + text.Append(s); + } + catch (Exception) + { + _errorCount++; + } + } + else + { + _errorCount++; + } + } + else if (paragraph != null && text.Length < 150) + { + var idxA = s.IndexOf("@"); + if (idxA > 0) + { + s = s.Substring(0, idxA - 1).Trim(); + } + text.Append(Environment.NewLine + s); + } + else + { + _errorCount++; + if (_errorCount > 10) + return; + } + } + if (text.Length > 0 && paragraph != null) + { + paragraph.Text = text.ToString().Trim(); + } + subtitle.RemoveEmptyLines(); + subtitle.Renumber(); + } + + private static string EncodeTimeCode(TimeCode time) + { + return time.ToHHMMSSFF().Replace(":", string.Empty); + } + + private static TimeCode DecodeTimeCode(string part) + { + part = part.Trim(); + string hour = part.Substring(0,2); + string minutes = part.Substring(2, 2); + string seconds = part.Substring(4, 2); + string frames = part.Substring(6, 2); + return new TimeCode(int.Parse(hour), int.Parse(minutes), int.Parse(seconds), FramesToMillisecondsMax999(int.Parse(frames))); + } + + } +} \ No newline at end of file