From 38f38e631ffe096f1e1815a6db15f0ce8ce50186 Mon Sep 17 00:00:00 2001 From: niksedk Date: Sat, 30 Jun 2012 07:38:15 +0000 Subject: [PATCH] Added two new subtitle formats + fixed bug in SSA/ASS style manager git-svn-id: https://subtitleedit.googlecode.com/svn/trunk@1278 99eadd0c-20b8-1223-b5c4-2a2b2df33de2 --- src/Forms/SubStationAlphaStyles.cs | 2 +- src/Logic/SubtitleFormats/JsonType3.cs | 105 +++++++++++++ src/Logic/SubtitleFormats/SubtitleFormat.cs | 1 + .../SubtitleFormats/UnknownSubtitle19.cs | 143 ++++++++++++++++++ src/SubtitleEdit.csproj | 2 + 5 files changed, 252 insertions(+), 1 deletion(-) create mode 100644 src/Logic/SubtitleFormats/JsonType3.cs create mode 100644 src/Logic/SubtitleFormats/UnknownSubtitle19.cs diff --git a/src/Forms/SubStationAlphaStyles.cs b/src/Forms/SubStationAlphaStyles.cs index 48f2c5703..7071dc73f 100644 --- a/src/Forms/SubStationAlphaStyles.cs +++ b/src/Forms/SubStationAlphaStyles.cs @@ -333,7 +333,7 @@ namespace Nikse.SubtitleEdit.Forms { if (string.IsNullOrEmpty(p.Extra) && ssaStyle.Name.TrimStart('*') == "Default") count++; - else if (ssaStyle.Name.TrimStart('*').ToLower() == p.Extra.TrimStart('*').ToLower()) + else if (p.Extra != null && ssaStyle.Name.TrimStart('*').ToLower() == p.Extra.TrimStart('*').ToLower()) count++; } subItem = new ListViewItem.ListViewSubItem(item, count.ToString()); diff --git a/src/Logic/SubtitleFormats/JsonType3.cs b/src/Logic/SubtitleFormats/JsonType3.cs new file mode 100644 index 000000000..0e265e8b7 --- /dev/null +++ b/src/Logic/SubtitleFormats/JsonType3.cs @@ -0,0 +1,105 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Nikse.SubtitleEdit.Logic.SubtitleFormats +{ + class JsonType3 : SubtitleFormat + { + public override string Extension + { + get { return ".json"; } + } + + public override string Name + { + get { return "JSON Type 3"; } + } + + 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) + { + var sb = new StringBuilder(); + sb.Append("["); + int count = 0; + foreach (Paragraph p in subtitle.Paragraphs) + { + if (count > 0) + sb.Append(","); + sb.Append("{\"duration\":"); + sb.Append(p.Duration.TotalMilliseconds.ToString(System.Globalization.CultureInfo.InvariantCulture)); + sb.Append(",\"content\":\""); + sb.Append(p.Text.Replace("\\", string.Empty).Replace("{", string.Empty).Replace("{", string.Empty).Replace("\"", "\\\"").Replace(Environment.NewLine, "\\n") + "\""); + sb.Append(",\"startOfParagraph\":false"); + sb.Append(",\"startTime\":"); + sb.Append(p.StartTime.TotalMilliseconds.ToString(System.Globalization.CultureInfo.InvariantCulture)); + sb.Append("}"); + count++; + } + sb.Append("]"); + return sb.ToString().Trim(); + } + + public override void LoadSubtitle(Subtitle subtitle, List lines, string fileName) + { + _errorCount = 0; + + var sb = new StringBuilder(); + foreach (string s in lines) + sb.Append(s); + int startIndex = sb.ToString().IndexOf("[{\"duration"); + if (startIndex == -1) + return; + + string text = sb.ToString().Substring(startIndex); + foreach (string line in text.Replace("},{", Environment.NewLine).Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)) + { + string s = line.Trim() + "}"; + string start = Json.ReadTag(s, "startTime"); + string duration = Json.ReadTag(s, "duration"); + string content = Json.ReadTag(s, "content"); + if (start != null && duration != null && content != null) + { + double startSeconds; + double durationSeconds; + if (double.TryParse(start, System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out startSeconds) && + double.TryParse(duration, System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out durationSeconds) && + content != null) + { + content = content.Replace("
", Environment.NewLine); + content = content.Replace("
", Environment.NewLine); + content = content.Replace("
", Environment.NewLine); + content = content.Replace("\\n", Environment.NewLine); + subtitle.Paragraphs.Add(new Paragraph(content, startSeconds, startSeconds + durationSeconds)); + } + else + { + _errorCount++; + } + } + else + { + _errorCount++; + } + } + subtitle.Renumber(1); + } + + } +} diff --git a/src/Logic/SubtitleFormats/SubtitleFormat.cs b/src/Logic/SubtitleFormats/SubtitleFormat.cs index 756427747..e1e1e9541 100644 --- a/src/Logic/SubtitleFormats/SubtitleFormat.cs +++ b/src/Logic/SubtitleFormats/SubtitleFormat.cs @@ -47,6 +47,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats new GpacTtxt(), new Json(), new JsonType2(), + new JsonType3(), new MicroDvd(), new MidwayInscriberCGX(), new MPlayer2(), diff --git a/src/Logic/SubtitleFormats/UnknownSubtitle19.cs b/src/Logic/SubtitleFormats/UnknownSubtitle19.cs new file mode 100644 index 000000000..82fe8adc3 --- /dev/null +++ b/src/Logic/SubtitleFormats/UnknownSubtitle19.cs @@ -0,0 +1,143 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Xml; +using System.IO; + +namespace Nikse.SubtitleEdit.Logic.SubtitleFormats +{ + class UnknownSubtitle19 : SubtitleFormat + { + public override string Extension + { + get { return ".xml"; } + } + + public override string Name + { + get { return "Unknown19"; } + } + + 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 > 0; + } + + private string ToTimeCode(TimeCode time) + { + return string.Format("{0:0.0}", time.TotalSeconds); + } + + private TimeCode DecodeTimeCode(string s) + { + return new TimeCode(TimeSpan.FromSeconds(double.Parse(s))); + } + + public override string ToText(Subtitle subtitle, string title) + { + // + // + // + string xmlStructure = + "" + Environment.NewLine + + ""; + + var xml = new XmlDocument(); + xml.LoadXml(xmlStructure); + + // int count = 1; + foreach (Paragraph p in subtitle.Paragraphs) + { + XmlNode paragraph = xml.CreateElement("Clip"); + + XmlAttribute attr = xml.CreateAttribute("start"); + attr.InnerText = ToTimeCode(p.StartTime); + paragraph.Attributes.Append(attr); + + attr = xml.CreateAttribute("end"); + attr.InnerText = ToTimeCode(p.EndTime); + paragraph.Attributes.Append(attr); + + //attr = xml.CreateAttribute("fileName"); + //attr.InnerText = "ee_disc1_subtitle_1/Subtitle_" + count + ".png"; + //paragraph.Attributes.Append(attr); + + attr = xml.CreateAttribute("text"); + attr.InnerText = Utilities.RemoveHtmlTags(p.Text); + paragraph.Attributes.Append(attr); + + xml.DocumentElement.AppendChild(paragraph); + // count++; + } + + var ms = new MemoryStream(); + var writer = new XmlTextWriter(ms, Encoding.UTF8) { Formatting = Formatting.Indented }; + xml.Save(writer); + return Encoding.UTF8.GetString(ms.ToArray()).Trim(); + } + + public override void LoadSubtitle(Subtitle subtitle, List lines, string fileName) + { + _errorCount = 0; + + var sb = new StringBuilder(); + lines.ForEach(line => sb.AppendLine(line)); + + string allText = sb.ToString(); + if (!allText.Contains("") || !allText.Contains(" + @@ -732,6 +733,7 @@ +