From 0e9ff56f00898ba8a0c1f4691e6359316bae031c Mon Sep 17 00:00:00 2001 From: niksedk Date: Tue, 17 Jan 2012 07:43:22 +0000 Subject: [PATCH] Added two new subtitle formats git-svn-id: https://subtitleedit.googlecode.com/svn/trunk@933 99eadd0c-20b8-1223-b5c4-2a2b2df33de2 --- src/Logic/SubtitleFormats/SubtitleFormat.cs | 2 + .../SubtitleFormats/UnknownSubtitle12.cs | 107 ++++++++++++++ .../SubtitleFormats/UnknownSubtitle13.cs | 137 ++++++++++++++++++ src/SubtitleEdit.csproj | 2 + 4 files changed, 248 insertions(+) create mode 100644 src/Logic/SubtitleFormats/UnknownSubtitle12.cs create mode 100644 src/Logic/SubtitleFormats/UnknownSubtitle13.cs diff --git a/src/Logic/SubtitleFormats/SubtitleFormat.cs b/src/Logic/SubtitleFormats/SubtitleFormat.cs index baf437986..7ad916659 100644 --- a/src/Logic/SubtitleFormats/SubtitleFormat.cs +++ b/src/Logic/SubtitleFormats/SubtitleFormat.cs @@ -83,6 +83,8 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats new UnknownSubtitle9(), new UnknownSubtitle10(), new UnknownSubtitle11(), + new UnknownSubtitle12(), + new UnknownSubtitle13(), new UTSubtitleXml(), new Utx(), new UtxFrames(), diff --git a/src/Logic/SubtitleFormats/UnknownSubtitle12.cs b/src/Logic/SubtitleFormats/UnknownSubtitle12.cs new file mode 100644 index 000000000..91de8aa76 --- /dev/null +++ b/src/Logic/SubtitleFormats/UnknownSubtitle12.cs @@ -0,0 +1,107 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +namespace Nikse.SubtitleEdit.Logic.SubtitleFormats +{ + /// + ///4.01 5.12 + ///Dit is de dag. + /// + public class UnknownSubtitle12 : SubtitleFormat + { + static Regex _regexTimeCode = new Regex(@"^\d+.\d\d\t\t\d+.\d\d\t*$", RegexOptions.Compiled); + + public override string Extension + { + get { return ".txt"; } + } + + public override string Name + { + get { return "Unknown 12"; } + } + + 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; + } + + private string MakeTimeCode(TimeCode tc) + { + return string.Format("{0:0.00}", tc.TotalSeconds); + } + + public override string ToText(Subtitle subtitle, string title) + { + var sb = new StringBuilder(); + foreach (Paragraph p in subtitle.Paragraphs) + { + sb.Append(MakeTimeCode(p.StartTime)); + sb.Append("\t\t"); + sb.Append(MakeTimeCode(p.EndTime)); + sb.Append("\t\t\n"); + sb.Append(p.Text.Replace(Environment.NewLine, "\n") + "\n\n"); + } + return sb.ToString().Trim(); + } + + private TimeCode DecodeTimeCode(string timeCode) + { + return new TimeCode(TimeSpan.FromSeconds(double.Parse(timeCode.Trim()))); + } + + public override void LoadSubtitle(Subtitle subtitle, List lines, string fileName) + { + _errorCount = 0; + Paragraph p = null; + var text = new StringBuilder(); + foreach (string line in lines) + { + string s = line.Trim(); + if (_regexTimeCode.IsMatch(s)) + { + try + { + if (p != null) + { + p.Text = text.ToString().Trim(); + subtitle.Paragraphs.Add(p); + } + string[] arr = s.Split("\t".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); + text = new StringBuilder(); + p = new Paragraph(DecodeTimeCode(arr[0]), DecodeTimeCode(arr[1]), ""); + } + catch + { + _errorCount++; + p = null; + } + } + else if (p != null) + { + text.AppendLine(s); + } + else + { + _errorCount++; + } + } + subtitle.Renumber(1); + } + + } +} diff --git a/src/Logic/SubtitleFormats/UnknownSubtitle13.cs b/src/Logic/SubtitleFormats/UnknownSubtitle13.cs new file mode 100644 index 000000000..5926d1ba8 --- /dev/null +++ b/src/Logic/SubtitleFormats/UnknownSubtitle13.cs @@ -0,0 +1,137 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Xml; + +namespace Nikse.SubtitleEdit.Logic.SubtitleFormats +{ + class UnknownSubtitle13 : SubtitleFormat + { + public override string Extension + { + get { return ".xml"; } + } + + public override string Name + { + get { return "Unknown 13"; } + } + + public override bool HasLineNumber + { + get { return false; } + } + + public override bool IsTimeBased + { + get { return true; } + } + + public override bool IsMine(List lines, string fileName) + { + Subtitle subtitle = new Subtitle(); + this.LoadSubtitle(subtitle, lines, fileName); + return subtitle.Paragraphs.Count > 0; + } + + public override string ToText(Subtitle subtitle, string title) + { + string xmlStructure = + "" + Environment.NewLine + + ""; + + XmlDocument xml = new XmlDocument(); + xml.LoadXml(xmlStructure); + + int id = 1; + foreach (Paragraph p in subtitle.Paragraphs) + { + XmlNode paragraph = xml.CreateElement("entry"); + + XmlAttribute duration = xml.CreateAttribute("timeOut"); + duration.InnerText = p.EndTime.ToString(); + paragraph.Attributes.Append(duration); + + XmlAttribute start = xml.CreateAttribute("timeIn"); + start.InnerText = p.StartTime.ToString(); + paragraph.Attributes.Append(start); + + XmlAttribute idAttr = xml.CreateAttribute("id"); + idAttr.InnerText = id.ToString(); + paragraph.Attributes.Append(idAttr); + + paragraph.InnerText = " lines, string fileName) + { + _errorCount = 0; + + StringBuilder sb = new StringBuilder(); + lines.ForEach(line => sb.AppendLine(line)); + + string allText = sb.ToString(); + if (!allText.Contains("") || !allText.Contains("timeIn=")) + return; + + XmlDocument xml = new XmlDocument(); + try + { + xml.LoadXml(allText); + } + catch (Exception exception) + { + System.Diagnostics.Debug.WriteLine(exception.Message); + _errorCount = 1; + return; + } + + foreach (XmlNode node in xml.DocumentElement.SelectNodes("entry")) + { + try + { + string start = node.Attributes["timeIn"].InnerText; + string end = node.Attributes["timeOut"].InnerText; + string text = node.InnerText; + if (text.StartsWith("![CDATA[")) + text = text.Remove(0, 8); + if (text.StartsWith("]]")) + text = text.Remove(text.Length-3, 2); + + subtitle.Paragraphs.Add(new Paragraph(DecodeTimeCode(start), DecodeTimeCode(end), text)); + } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine(ex.Message); + _errorCount++; + } + } + subtitle.Renumber(1); + } + + private TimeCode DecodeTimeCode(string timeCode) + { + string[] arr = timeCode.Split(":,.".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); + int hours = int.Parse(arr[0]); + int minutes = int.Parse(arr[1]); + int seconds = int.Parse(arr[2]); + int ms = int.Parse(arr[3]); + return new TimeCode(hours, minutes, seconds, ms); + } + + + } +} + + diff --git a/src/SubtitleEdit.csproj b/src/SubtitleEdit.csproj index 0ac6c4c37..41af28a54 100644 --- a/src/SubtitleEdit.csproj +++ b/src/SubtitleEdit.csproj @@ -624,6 +624,8 @@ + +