mirror of
https://github.com/SubtitleEdit/subtitleedit.git
synced 2024-11-22 19:22:53 +01:00
Improve unknown json importer - thx Thamy :)
This commit is contained in:
parent
8b2036e3fe
commit
6d498f9f8e
@ -29,7 +29,9 @@ namespace Nikse.SubtitleEdit.Core
|
||||
if (subTcOnAloneLines.Paragraphs.Count > subtitle.Paragraphs.Count)
|
||||
subtitle = subTcOnAloneLines;
|
||||
|
||||
if (subtitle.Paragraphs.Count < 2)
|
||||
bool isJson = IsJson(lines);
|
||||
|
||||
if (subtitle.Paragraphs.Count < 2 && !isJson)
|
||||
{
|
||||
subtitle = ImportTimeCodesInFramesOnSameSeperateLine(lines);
|
||||
if (subtitle.Paragraphs.Count < 2)
|
||||
@ -59,7 +61,7 @@ namespace Nikse.SubtitleEdit.Core
|
||||
CleanUp(subtitle);
|
||||
}
|
||||
|
||||
if (subtitle.Paragraphs.Count < 2)
|
||||
if (subtitle.Paragraphs.Count < 2 || isJson)
|
||||
{
|
||||
var jsonSubtitle = new UknownFormatImporterJson().AutoGuessImport(lines);
|
||||
if (jsonSubtitle != null && jsonSubtitle.Paragraphs.Count > 2)
|
||||
@ -79,6 +81,25 @@ namespace Nikse.SubtitleEdit.Core
|
||||
return subtitle;
|
||||
}
|
||||
|
||||
private bool IsJson(List<string> lines)
|
||||
{
|
||||
var jp = new JsonParser();
|
||||
try
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
foreach (var line in lines)
|
||||
{
|
||||
sb.AppendLine(line);
|
||||
}
|
||||
jp.Parse(sb.ToString());
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static void CleanUp(Subtitle subtitle)
|
||||
{
|
||||
foreach (Paragraph p in subtitle.Paragraphs)
|
||||
|
@ -100,7 +100,12 @@ namespace Nikse.SubtitleEdit.Core
|
||||
p.Style.Contains("\"startMillis\"") ||
|
||||
p.Style.Contains("\"start_millis\"") ||
|
||||
p.Style.Contains("\"startMilliseconds\"") ||
|
||||
p.Style.Contains("\"start_millisecondsMs\""))
|
||||
p.Style.Contains("\"start_millisecondsMs\"") ||
|
||||
p.Style.Contains("\"fromMs\"") ||
|
||||
p.Style.Contains("\"from_ms\"") ||
|
||||
p.Style.Contains("\"fromMillis\"") ||
|
||||
p.Style.Contains("\"fromMilliseconds\"") ||
|
||||
p.Style.Contains("\"from_milliseconds\""))
|
||||
{
|
||||
msFound++;
|
||||
}
|
||||
@ -190,6 +195,7 @@ namespace Nikse.SubtitleEdit.Core
|
||||
"startMillis", "start_Millis", "startmillis",
|
||||
"startMs", "start_ms", "startms",
|
||||
"startMilliseconds", "start_Millisesonds", "startmilliseconds",
|
||||
"from", "fromTime", "from_ms", "fromMilliseconds", "from_milliseconds"
|
||||
});
|
||||
}
|
||||
|
||||
@ -202,6 +208,7 @@ namespace Nikse.SubtitleEdit.Core
|
||||
"endMillis", "end_Millis", "endmillis",
|
||||
"endMs", "end_ms", "startms",
|
||||
"endMilliseconds", "end_Millisesonds", "endmilliseconds",
|
||||
"to", "toTime", "to_ms", "toMilliseconds", "to_milliseconds"
|
||||
});
|
||||
}
|
||||
|
||||
@ -217,6 +224,8 @@ namespace Nikse.SubtitleEdit.Core
|
||||
private static string ReadTextTag(string s)
|
||||
{
|
||||
var idx = s.IndexOf("\"text", StringComparison.OrdinalIgnoreCase);
|
||||
if (idx < 0)
|
||||
idx = s.IndexOf("\"content", StringComparison.OrdinalIgnoreCase);
|
||||
if (idx < 0)
|
||||
return null;
|
||||
|
||||
@ -226,9 +235,13 @@ namespace Nikse.SubtitleEdit.Core
|
||||
s = s.Substring(0, idx + 1);
|
||||
|
||||
var text = Json.ReadTag(s, "text");
|
||||
if (text == null)
|
||||
text = Json.ReadTag(s, "content");
|
||||
var textLines = Json.ReadArray(s, "text");
|
||||
if (textLines == null || textLines.Count == 0)
|
||||
textLines = Json.ReadArray(s, "content");
|
||||
bool isArray = s.Contains("[");
|
||||
if (isArray && textLines.Any(p => p == "end_time" || p == "endTime" || p == "end" || p == "endMs" || p == "endMilliseconds" || p == "end_ms"))
|
||||
if (isArray && textLines.Any(p => p == "end_time" || p == "endTime" || p == "end" || p == "endMs" || p == "endMilliseconds" || p == "end_ms" || p == "to" || p == "to_ms" || p == "from" || p == "from_ms"))
|
||||
isArray = false;
|
||||
if (!isArray && !string.IsNullOrEmpty(text))
|
||||
{
|
||||
|
@ -79,7 +79,7 @@ namespace Test.Logic
|
||||
[TestMethod]
|
||||
public void TestUnknownJsonArray()
|
||||
{
|
||||
var raw = @"{
|
||||
const string raw = @"{
|
||||
'subtitles': [
|
||||
{
|
||||
'sub_order' : 1,
|
||||
@ -144,5 +144,43 @@ namespace Test.Logic
|
||||
Assert.AreEqual(11, subtitle.Paragraphs.Count);
|
||||
Assert.AreEqual("Ford" + Environment.NewLine + "BMW" + Environment.NewLine + "Fiat", subtitle.Paragraphs[1].Text);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ImportBilibiliJson()
|
||||
{
|
||||
const string raw = @"{
|
||||
'body': [
|
||||
{
|
||||
'from': 3.7,
|
||||
'to': 7.7,
|
||||
'location': 2,
|
||||
'content': 'Line0'
|
||||
},
|
||||
{
|
||||
'from': 7.7,
|
||||
'to': 13.7,
|
||||
'location': 2,
|
||||
'content': 'Line1'
|
||||
},
|
||||
{
|
||||
'from': 13.7,
|
||||
'to': 18.7,
|
||||
'location': 2,
|
||||
'content': 'Line2'
|
||||
},
|
||||
{
|
||||
'from': 18.7,
|
||||
'to': 24.7,
|
||||
'location': 2,
|
||||
'content': 'Line3'
|
||||
}]
|
||||
}";
|
||||
|
||||
var importer = new UknownFormatImporterJson();
|
||||
var subtitle = importer.AutoGuessImport(raw.Replace('\'', '"').SplitToLines());
|
||||
Assert.AreEqual(4, subtitle.Paragraphs.Count);
|
||||
Assert.AreEqual("Line1", subtitle.Paragraphs[1].Text);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user