Add new sub format

This commit is contained in:
Nikolaj Olsson 2017-10-22 23:29:04 +02:00
parent fd6e4d61fa
commit e107203fd5
3 changed files with 79 additions and 0 deletions

View File

@ -232,6 +232,7 @@
<Compile Include="SubtitleFormats\Cmaft.cs" />
<Compile Include="SubtitleFormats\Csv5.cs" />
<Compile Include="SubtitleFormats\DvdStudioProSpaceOneSemicolon.cs" />
<Compile Include="SubtitleFormats\JsonType12.cs" />
<Compile Include="SubtitleFormats\TimeCodesOnly2.cs" />
<Compile Include="SubtitleFormats\Rtf1.cs" />
<Compile Include="SubtitleFormats\Rtf2.cs" />

View File

@ -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<string> 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();
}
}
}

View File

@ -94,6 +94,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
new JsonType9(),
new JsonType10(),
new JsonType11(),
new JsonType12(),
new LambdaCap(),
new Lrc(),
new MacSub(),