Add new sub format - thx Lukas :)

This commit is contained in:
Nikolaj Olsson 2020-10-28 07:21:18 +01:00
parent da9fcd9ae4
commit 378f8398dd
4 changed files with 82 additions and 3 deletions

View File

@ -406,7 +406,7 @@ Note: Do check free disk space.</WaveFileMalformed>
<DisplayStandardCode>Display standard code</DisplayStandardCode>
<ColorRequiresTeletext>Colors require teletext!</ColorRequiresTeletext>
<AlignmentRequiresTeletext>Alignment requires teletext!</AlignmentRequiresTeletext>
<TeletextCharsShouldBe38>'Max# of chars per row' for Teletext should be 38!</TeletextCharsShouldBe38>
<TeletextCharsShouldBe38>'Max# of chars per row' for teletext should be 38!</TeletextCharsShouldBe38>
<CharacterCodeTable>Character table</CharacterCodeTable>
<LanguageCode>Language code</LanguageCode>
<OriginalProgramTitle>Original program title</OriginalProgramTitle>

View File

@ -10,7 +10,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
public override string Name => "JSON";
public static string EncodeJsonText(string text)
public static string EncodeJsonText(string text, string newLineCharacter = "<br />")
{
var sb = new StringBuilder(text.Length);
foreach (var c in text)
@ -28,7 +28,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
break;
}
}
return sb.ToString().Replace(Environment.NewLine, "<br />");
return sb.ToString().Replace(Environment.NewLine, newLineCharacter);
}
public static string DecodeJsonText(string text)

View File

@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class JsonType18 : SubtitleFormat
{
public override string Extension => ".json";
public override string Name => "JSON Type 18";
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder(@"[");
int count = 0;
foreach (var p in subtitle.Paragraphs)
{
if (count > 0)
{
sb.Append(',');
}
count++;
sb.AppendLine();
sb.AppendLine(" {");
sb.AppendLine($" \"s\": {p.StartTime.TotalSeconds.ToString(CultureInfo.InvariantCulture)},");
sb.AppendLine($" \"d\": {p.Duration.TotalSeconds.ToString(CultureInfo.InvariantCulture)},");
sb.AppendLine($" \"n\": \"{Json.EncodeJsonText(p.Text, "\\n")}\"");
sb.Append(" }");
}
sb.AppendLine();
sb.Append(']');
return sb.ToString().Trim();
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
_errorCount = 0;
var sb = new StringBuilder();
foreach (var s in lines)
{
sb.Append(s);
}
var text = sb.ToString().TrimStart();
if (!text.StartsWith("[", StringComparison.Ordinal))
{
return;
}
var parser = new SeJsonParser();
var elements = parser.GetArrayElements(text);
foreach (var element in elements)
{
var startTimeObject = parser.GetFirstObject(element, "s");
var durationTimeObject = parser.GetFirstObject(element, "d");
var s = parser.GetFirstObject(element, "n");
if (!string.IsNullOrEmpty(s) && !string.IsNullOrEmpty(startTimeObject) && !string.IsNullOrEmpty(durationTimeObject))
{
if (double.TryParse(startTimeObject, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var startSeconds) &&
double.TryParse(durationTimeObject, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var durationSeconds))
{
var pText = Json.DecodeJsonText(s);
var p = new Paragraph(pText, startSeconds * 1000.0, (startSeconds + durationSeconds) * 1000.0);
subtitle.Paragraphs.Add(p);
}
else
{
_errorCount++;
}
}
}
subtitle.Renumber();
}
}
}

View File

@ -120,6 +120,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
new JsonType15(),
new JsonType16(),
new JsonType17(),
new JsonType18(),
new KanopyHtml(),
new LambdaCap(),
new Lrc(),