mirror of
https://github.com/SubtitleEdit/subtitleedit.git
synced 2024-11-23 11:42:36 +01:00
Add new sub format
This commit is contained in:
parent
fd6e4d61fa
commit
e107203fd5
@ -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" />
|
||||
|
77
libse/SubtitleFormats/JsonType12.cs
Normal file
77
libse/SubtitleFormats/JsonType12.cs
Normal 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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -94,6 +94,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
|
||||
new JsonType9(),
|
||||
new JsonType10(),
|
||||
new JsonType11(),
|
||||
new JsonType12(),
|
||||
new LambdaCap(),
|
||||
new Lrc(),
|
||||
new MacSub(),
|
||||
|
Loading…
Reference in New Issue
Block a user