Added new subtitle format - thx Eddy :)

This commit is contained in:
Nikolaj Olsson 2017-04-26 18:50:20 +02:00
parent 10f7259997
commit f20645bc25
2 changed files with 92 additions and 0 deletions

View File

@ -216,6 +216,7 @@
<Compile Include="Subtitle.cs" />
<Compile Include="SubtitleFormats\AribB36.cs" />
<Compile Include="SubtitleFormats\AribB24Decoder.cs" />
<Compile Include="SubtitleFormats\JsonType11.cs" />
<Compile Include="SubtitleFormats\SmpteTt2052.cs" />
<Compile Include="SubtitleFormats\Ted20.cs" />
<Compile Include="SubtitleFormats\Csv4.cs" />

View File

@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class JsonType11 : SubtitleFormat
{
public override string Extension
{
get { return ".json"; }
}
public override string Name
{
get { return "JSON Type 11"; }
}
public override bool IsTimeBased
{
get { return true; }
}
public override bool IsMine(List<string> 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("[");
int count = 0;
foreach (Paragraph p in subtitle.Paragraphs)
{
if (count > 0)
sb.Append(',');
sb.Append("{\"milliseconds\":");
sb.Append(Json.EncodeJsonText(p.StartTime.TotalMilliseconds.ToString(System.Globalization.CultureInfo.InvariantCulture)));
sb.Append(",\"line\":\"");
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);
string allText = sb.ToString().Trim();
if (!(allText.StartsWith("[", StringComparison.Ordinal) && allText.Contains("milliseconds", StringComparison.Ordinal)))
return;
foreach (string line in allText.Split('{', '}', '[', ']'))
{
string s = line.Trim();
if (s.Length > 10)
{
string start = Json.ReadTag(s, "milliseconds");
string text = Json.ReadTag(s, "line");
if (start != null && text != null)
{
double startSeconds;
if (double.TryParse(start, System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out startSeconds))
{
var duration = Utilities.GetOptimalDisplayMilliseconds(text);
var endMilliseconds = startSeconds * TimeCode.BaseUnit + duration;
subtitle.Paragraphs.Add(new Paragraph(Json.DecodeJsonText(text), startSeconds * TimeCode.BaseUnit, endMilliseconds));
}
else
{
_errorCount++;
}
}
else
{
_errorCount++;
}
}
}
subtitle.Renumber();
}
}
}