From e107203fd517ec09da0dc2bbbe266d805ed6e20e Mon Sep 17 00:00:00 2001 From: Nikolaj Olsson Date: Sun, 22 Oct 2017 23:29:04 +0200 Subject: [PATCH] Add new sub format --- libse/LibSE.csproj | 1 + libse/SubtitleFormats/JsonType12.cs | 77 +++++++++++++++++++++++++ libse/SubtitleFormats/SubtitleFormat.cs | 1 + 3 files changed, 79 insertions(+) create mode 100644 libse/SubtitleFormats/JsonType12.cs diff --git a/libse/LibSE.csproj b/libse/LibSE.csproj index 6eb3a9fb3..ab9698240 100644 --- a/libse/LibSE.csproj +++ b/libse/LibSE.csproj @@ -232,6 +232,7 @@ + diff --git a/libse/SubtitleFormats/JsonType12.cs b/libse/SubtitleFormats/JsonType12.cs new file mode 100644 index 000000000..fe27abc93 --- /dev/null +++ b/libse/SubtitleFormats/JsonType12.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Nikse.SubtitleEdit.Core.SubtitleFormats +{ + public class JsonType12 : SubtitleFormat + { + public override string Extension => ".json"; + + public override string Name => "JSON Type 12"; + + public override string ToText(Subtitle subtitle, string title) + { + var sb = new StringBuilder(@"["); + int count = 0; + foreach (Paragraph p in subtitle.Paragraphs) + { + if (count > 0) + sb.Append(','); + sb.Append("{\"t1\":"); + sb.Append(p.StartTime.TotalMilliseconds.ToString(System.Globalization.CultureInfo.InvariantCulture)); + sb.Append(",\"t2\":"); + sb.Append(p.EndTime.TotalMilliseconds.ToString(System.Globalization.CultureInfo.InvariantCulture)); + sb.Append(",\"t\":\""); + sb.Append(Json.EncodeJsonText(p.Text)); + 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); + if (!sb.ToString().TrimStart().StartsWith("[{\"", StringComparison.Ordinal)) + return; + + sb = sb.Replace("}, {", "},{"); + sb = sb.Replace("} , {", "},{"); + sb = sb.Replace("} ,{", "},{"); + + foreach (string line in sb.ToString().Replace("},{", Environment.NewLine).SplitToLines()) + { + string s = line.Trim() + "}"; + string start = Json.ReadTag(s, "t1"); + string end = Json.ReadTag(s, "t2"); + string text = Json.ReadTag(s, "t"); + if (start != null && end != null && text != null) + { + double startSeconds; + double endSeconds; + if (double.TryParse(start, System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out startSeconds) && + double.TryParse(end, System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out endSeconds)) + { + subtitle.Paragraphs.Add(new Paragraph(Json.DecodeJsonText(text), startSeconds, endSeconds)); + } + else + { + _errorCount++; + } + } + else + { + _errorCount++; + } + } + subtitle.Renumber(); + } + + } +} diff --git a/libse/SubtitleFormats/SubtitleFormat.cs b/libse/SubtitleFormats/SubtitleFormat.cs index 9aa8a06f8..aed66be7c 100644 --- a/libse/SubtitleFormats/SubtitleFormat.cs +++ b/libse/SubtitleFormats/SubtitleFormat.cs @@ -94,6 +94,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats new JsonType9(), new JsonType10(), new JsonType11(), + new JsonType12(), new LambdaCap(), new Lrc(), new MacSub(),