Add new subtitle format

This commit is contained in:
Nikolaj Olsson 2020-05-09 15:02:59 +02:00
parent 905e136a18
commit e1fdaf2c69
4 changed files with 90 additions and 1 deletions

View File

@ -980,6 +980,9 @@ namespace Nikse.SubtitleEdit.Core
return list;
}
/// <summary>
/// Get first object or value
/// </summary>
public string GetFirstObject(string content, string name)
{
Errors = new List<string>();
@ -1119,6 +1122,13 @@ namespace Nikse.SubtitleEdit.Core
return string.Empty;
}
}
var objectValue = content.Substring(i, end - i).Trim();
if (objectName == name)
{
return objectValue;
}
i += end - i + 1;
state.Pop();
if (state.Count > 0)
@ -1191,6 +1201,12 @@ namespace Nikse.SubtitleEdit.Core
sb.Append(content[i]);
i++;
}
if (objectName == name)
{
return sb.ToString();
}
state.Pop();
if (state.Count > 0)
{

View File

@ -67,7 +67,14 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
return;
}
var startIndex = allText.IndexOf('{');
var indexOfSubtitles = allText.IndexOf("subtitles", StringComparison.Ordinal);
if (indexOfSubtitles <= 0)
{
return;
}
var startText = allText.Substring(0, indexOfSubtitles);
var startIndex = startText.LastIndexOf('{');
if (startIndex < 0)
{
return;

View File

@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class JsonType17b : SubtitleFormat
{
public override string Extension => ".json";
public override string Name => "JSON Type 17b";
public override string ToText(Subtitle subtitle, string title)
{
var data = new JsonType17().ToText(subtitle, title);
var encodedData = Json.EncodeJsonText(data
.Replace(Environment.NewLine, " ")
.Replace("\t", " ")
);
var sb = new StringBuilder();
sb.AppendLine("{");
sb.AppendLine($" \"data\": \"{encodedData}\"");
sb.AppendLine("}");
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();
var indexOfMainTag = allText.IndexOf("\"timestamp_begin\"", StringComparison.Ordinal);
if (indexOfMainTag < 0 && !allText.Contains("\"data\""))
{
return;
}
var startIndex = allText.IndexOf('{');
if (startIndex < 0)
{
return;
}
var json = allText.Substring(startIndex);
var parser = new SeJsonParser();
var subtitleData = parser.GetFirstObject(json, "data");
if (string.IsNullOrEmpty(subtitleData))
{
return;
}
var jsonType17 = new JsonType17();
var innerJson = Json.DecodeJsonText(subtitleData);
jsonType17.LoadSubtitle(subtitle, new List<string> { innerJson }, null);
}
}
}

View File

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