mirror of
https://github.com/SubtitleEdit/subtitleedit.git
synced 2024-10-28 06:52:35 +01:00
Added new subtitle format
This commit is contained in:
parent
485f2926be
commit
5b3f636832
@ -120,7 +120,6 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
|
||||
double.TryParse(end, System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out endSeconds) &&
|
||||
text != null)
|
||||
{
|
||||
|
||||
subtitle.Paragraphs.Add(new Paragraph(DecodeJsonText(text), startSeconds * 1000.0, endSeconds * 1000.0));
|
||||
}
|
||||
else
|
||||
@ -185,5 +184,81 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
|
||||
}
|
||||
}
|
||||
|
||||
public static List<string> ReadArray(string s, string tag)
|
||||
{
|
||||
var list = new List<string>();
|
||||
|
||||
int startIndex = s.IndexOf("\"" + tag + "\"");
|
||||
if (startIndex == -1)
|
||||
startIndex = s.IndexOf("'" + tag + "'");
|
||||
if (startIndex == -1)
|
||||
return list;
|
||||
|
||||
startIndex += tag.Length + 4;
|
||||
|
||||
string res = s.Substring(startIndex);
|
||||
|
||||
int tagLevel = 1;
|
||||
int nextTag = 0;
|
||||
int oldStart = 0;
|
||||
while (tagLevel >= 1 && nextTag >= 0 && nextTag+1 < res.Length)
|
||||
{
|
||||
if (res.Substring(oldStart, 1) == "\"")
|
||||
{
|
||||
nextTag = res.IndexOf('"', oldStart + 1);
|
||||
|
||||
while (nextTag > 0 && nextTag + 1 < res.Length && res.Substring(nextTag - 1, 1) == "\\")
|
||||
nextTag = res.IndexOf('"', nextTag + 1);
|
||||
|
||||
if (nextTag > 0)
|
||||
{
|
||||
string newValue = res.Substring(oldStart, nextTag - oldStart);
|
||||
list.Add(newValue.Remove(0, 1));
|
||||
oldStart = nextTag + 2;
|
||||
}
|
||||
}
|
||||
else if (res.Substring(oldStart, 1) != "[" && res.Substring(oldStart, 1) != "]")
|
||||
{
|
||||
nextTag = res.IndexOf(',', oldStart + 1);
|
||||
if (nextTag > 0)
|
||||
{
|
||||
string newValue = res.Substring(oldStart, nextTag - oldStart);
|
||||
if (newValue.EndsWith("]"))
|
||||
{
|
||||
newValue = newValue.TrimEnd(']');
|
||||
tagLevel = -10; // return
|
||||
}
|
||||
list.Add(newValue.Trim());
|
||||
oldStart = nextTag + 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int nextBegin = res.IndexOf("[", nextTag);
|
||||
int nextEnd = res.IndexOf("]", nextTag);
|
||||
if (nextBegin < nextEnd && nextBegin != -1)
|
||||
{
|
||||
nextTag = nextBegin + 1;
|
||||
tagLevel++;
|
||||
}
|
||||
else
|
||||
{
|
||||
nextTag = nextEnd + 1;
|
||||
tagLevel--;
|
||||
if (tagLevel == 1)
|
||||
{
|
||||
string newValue = res.Substring(oldStart, nextTag - oldStart);
|
||||
list.Add(newValue);
|
||||
if (res.Substring(nextTag, 1) == "]")
|
||||
tagLevel--;
|
||||
oldStart = nextTag + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
163
src/Logic/SubtitleFormats/JsonType5.cs
Normal file
163
src/Logic/SubtitleFormats/JsonType5.cs
Normal file
@ -0,0 +1,163 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
|
||||
{
|
||||
public class JsonType5 : SubtitleFormat
|
||||
{
|
||||
public override string Extension
|
||||
{
|
||||
get { return ".json"; }
|
||||
}
|
||||
|
||||
public override string Name
|
||||
{
|
||||
get { return "JSON Type 5"; }
|
||||
}
|
||||
|
||||
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();
|
||||
sb.Append("{\"text_tees\":[");
|
||||
for (int i = 0; i < subtitle.Paragraphs.Count; i++)
|
||||
{
|
||||
Paragraph p = subtitle.Paragraphs[i];
|
||||
sb.Append(p.StartTime.TotalMilliseconds);
|
||||
sb.Append(",");
|
||||
sb.Append(p.EndTime.TotalMilliseconds);
|
||||
if (i < subtitle.Paragraphs.Count-1)
|
||||
sb.Append(",");
|
||||
}
|
||||
sb.Append("],");
|
||||
|
||||
sb.Append("\"text_target\":[");
|
||||
for (int i = 0; i < subtitle.Paragraphs.Count; i++)
|
||||
{
|
||||
sb.Append("[\"w1\",\"w3\"],[\"w1\",\"w3\"]");
|
||||
if (i < subtitle.Paragraphs.Count - 1)
|
||||
sb.Append(",");
|
||||
}
|
||||
sb.Append("],");
|
||||
|
||||
sb.Append("\"text_content\":[");
|
||||
for (int i = 0; i < subtitle.Paragraphs.Count; i++)
|
||||
{
|
||||
sb.Append("[");
|
||||
Paragraph p = subtitle.Paragraphs[i];
|
||||
var lines = p.Text.Replace(Environment.NewLine, "\n").Split('\n');
|
||||
for (int j = 0; j < lines.Length; j++)
|
||||
{
|
||||
sb.Append("\"");
|
||||
sb.Append(Json.EncodeJsonText(lines[j]));
|
||||
sb.Append("\"");
|
||||
if (j < lines.Length - 1)
|
||||
sb.Append(",");
|
||||
}
|
||||
sb.Append("],");
|
||||
if (i < subtitle.Paragraphs.Count - 1)
|
||||
sb.Append("[\"\",\"\"],");
|
||||
else
|
||||
sb.Append("[\"\",\"\"]");
|
||||
}
|
||||
sb.Append("],");
|
||||
|
||||
sb.Append("\"text_styles\":[");
|
||||
for (int i = 0; i < subtitle.Paragraphs.Count; i++)
|
||||
{
|
||||
sb.Append("[\"s1\",\"s2\"],[\"s1\",\"s2\"]");
|
||||
if (i < subtitle.Paragraphs.Count - 1)
|
||||
sb.Append(",");
|
||||
}
|
||||
sb.Append("],");
|
||||
|
||||
sb.Append("\"timerange\":[");
|
||||
Paragraph timerageP = subtitle.GetParagraphOrDefault(0);
|
||||
if (timerageP == null)
|
||||
sb.Append("0");
|
||||
else
|
||||
sb.Append(timerageP.StartTime.TotalMilliseconds);
|
||||
sb.Append(",");
|
||||
timerageP = subtitle.GetParagraphOrDefault(subtitle.Paragraphs.Count-1);
|
||||
if (timerageP == null)
|
||||
sb.Append("0");
|
||||
else
|
||||
sb.Append(timerageP.EndTime.TotalMilliseconds);
|
||||
sb.Append("]");
|
||||
|
||||
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);
|
||||
|
||||
var times = Json.ReadArray(sb.ToString(), "text_tees");
|
||||
var texts = Json.ReadArray(sb.ToString(), "text_content");
|
||||
var targets = Json.ReadArray(sb.ToString(), "text_target");
|
||||
|
||||
for (int i = 0; i < Math.Min(times.Count, texts.Count); i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
string text = texts[i];
|
||||
if (text.StartsWith("["))
|
||||
{
|
||||
var textLines = Json.ReadArray("{\"text\":" + texts[i] + "}", "text");
|
||||
var textSb = new StringBuilder();
|
||||
foreach (string line in textLines)
|
||||
{
|
||||
string t = Json.DecodeJsonText(line);
|
||||
if (t.StartsWith("[\"") && t.EndsWith("\"]"))
|
||||
{
|
||||
var innerSb = new StringBuilder();
|
||||
var innerTextLines = Json.ReadArray("{\"text\":" + t + "}", "text");
|
||||
foreach (string innerLine in innerTextLines)
|
||||
{
|
||||
innerSb.Append(" " + innerLine);
|
||||
}
|
||||
textSb.AppendLine(innerSb.ToString().Trim());
|
||||
}
|
||||
else
|
||||
{
|
||||
textSb.AppendLine(t);
|
||||
}
|
||||
}
|
||||
text = textSb.ToString().Trim();
|
||||
text = text.Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine);
|
||||
}
|
||||
Paragraph p = new Paragraph(text, int.Parse(times[i]), 0);
|
||||
if (i + 1 < times.Count)
|
||||
p.EndTime.TotalMilliseconds = int.Parse(times[i + 1]);
|
||||
subtitle.Paragraphs.Add(p);
|
||||
}
|
||||
catch
|
||||
{
|
||||
_errorCount++;
|
||||
}
|
||||
}
|
||||
subtitle.RemoveEmptyLines();
|
||||
subtitle.Renumber(1);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -77,6 +77,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
|
||||
new JsonType2(),
|
||||
new JsonType3(),
|
||||
new JsonType4(),
|
||||
new JsonType5(),
|
||||
new Lrc(),
|
||||
new MicroDvd(),
|
||||
new MidwayInscriberCGX(),
|
||||
|
@ -877,6 +877,7 @@
|
||||
<Compile Include="Logic\SubtitleFormats\AdobeEncore.cs" />
|
||||
<Compile Include="Logic\SubtitleFormats\DvdStudioProSpaceOne.cs" />
|
||||
<Compile Include="Logic\SubtitleFormats\FinalCutProXml14.cs" />
|
||||
<Compile Include="Logic\SubtitleFormats\JsonType5.cs" />
|
||||
<Compile Include="Logic\SubtitleFormats\SatBoxPng.cs" />
|
||||
<Compile Include="Logic\SubtitleFormats\UnknownSubtitle73.cs" />
|
||||
<Compile Include="Logic\SubtitleFormats\AdobeEncoreLineTabNewLine.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user