private static List <ResultText> ParseJsonToResult(string result) { var list = new List <ResultText>(); var jsonParser = new SeJsonParser(); var root = jsonParser.GetArrayElementsByName(result, "result"); foreach (var item in root) { var conf = jsonParser.GetFirstObject(item, "conf"); var start = jsonParser.GetFirstObject(item, "start"); var end = jsonParser.GetFirstObject(item, "end"); var word = jsonParser.GetFirstObject(item, "word"); if (!string.IsNullOrWhiteSpace(word) && decimal.TryParse(conf, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var confidence) && decimal.TryParse(start, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var startSeconds) && decimal.TryParse(end, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var endSeconds)) { var rt = new ResultText { Confidence = confidence, Text = word, Start = startSeconds, End = endSeconds }; list.Add(rt); } } return(list); }
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.Contains("\"segs\"", StringComparison.Ordinal) || !text.Contains('[')) { return; } var parser = new SeJsonParser(); var elements = parser.GetArrayElements(text.Substring(text.IndexOf('['))); foreach (var element in elements) { var startTimeObject = parser.GetFirstObject(element, "tStartMs"); var durationTimeObject = parser.GetFirstObject(element, "dDurationMs"); var idx = element.IndexOf('['); var s = string.Empty; if (idx > 0) { sb = new StringBuilder(); var arr = parser.GetArrayElements(element.Substring(idx)); foreach (var line in arr) { var content = parser.GetFirstObject(line, "utf8"); if (content != null) { sb.Append(content.Replace("\\n", Environment.NewLine) + " "); } } s = sb.ToString().Trim(); if (!string.IsNullOrEmpty(s) && !string.IsNullOrEmpty(startTimeObject) && !string.IsNullOrEmpty(durationTimeObject)) { if (double.TryParse(startTimeObject, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var startMs) && double.TryParse(durationTimeObject, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var durationMs)) { var pText = Json.DecodeJsonText(s); var p = new Paragraph(pText, startMs, startMs + durationMs); subtitle.Paragraphs.Add(p); } else { _errorCount++; } } } } subtitle.Renumber(); }
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 text = sb.ToString().TrimStart(); if (!text.StartsWith("[", StringComparison.Ordinal)) { var tag = "\"subtitles\":"; var startTag = text.IndexOf(tag, StringComparison.Ordinal); if (startTag >= 0) { text = text.Remove(0, startTag + tag.Length).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, "startTime"); var endTimeObject = parser.GetFirstObject(element, "endTime"); var texts = parser.GetAllTagsByNameAsStrings(element, "text"); if (texts.Count == 1 && !string.IsNullOrEmpty(startTimeObject) && !string.IsNullOrEmpty(endTimeObject)) { var startTime = GetTimeCode(parser, startTimeObject); var endTime = GetTimeCode(parser, endTimeObject); if (Math.Abs(startTime - double.MinValue) < 0.01 || Math.Abs(endTime - double.MinValue) < 0.01) { _errorCount++; } else { var p = new Paragraph(texts[0], startTime, endTime); subtitle.Paragraphs.Add(p); } } } subtitle.Renumber(); }
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 text = sb.ToString().TrimStart(); if (!text.Contains("\"body\"")) { return; } var parser = new SeJsonParser(); var elements = parser.GetArrayElementsByName(text, "body"); foreach (var element in elements) { var startTimeObject = parser.GetFirstObject(element, "from"); var endTimeObject = parser.GetFirstObject(element, "to"); var texts = parser.GetAllTagsByNameAsStrings(element, "content"); if (texts.Count == 1 && !string.IsNullOrEmpty(startTimeObject) && !string.IsNullOrEmpty(endTimeObject)) { var startTime = GetTimeCode(startTimeObject); var endTime = GetTimeCode(endTimeObject); if (Math.Abs(startTime - double.MinValue) < 0.01 || Math.Abs(endTime - double.MinValue) < 0.01) { _errorCount++; } else { var pText = texts[0].Replace("\\n", Environment.NewLine); var p = new Paragraph(pText, startTime, endTime); subtitle.Paragraphs.Add(p); } } } subtitle.Renumber(); }
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.IndexOf("atpjfile_info", StringComparison.Ordinal) < 0) { return; } subtitle.Paragraphs.Clear(); var parser = new SeJsonParser(); var elements = parser.GetArrayElementsByName(text, "BLOCKS"); foreach (var element in elements) { var startTimeObject = parser.GetFirstObject(element, "time_start"); var endTimeObject = parser.GetFirstObject(element, "time_end"); var textObject = parser.GetFirstObject(element, "text"); if (!string.IsNullOrEmpty(textObject) && !string.IsNullOrEmpty(startTimeObject) && !string.IsNullOrEmpty(endTimeObject)) { if (double.TryParse(startTimeObject, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var startMs) && double.TryParse(endTimeObject, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var endMs)) { var s = Utilities.UrlDecode(Json.DecodeJsonText(textObject)); var p = new Paragraph(s, startMs, endMs); subtitle.Paragraphs.Add(p); } else { _errorCount++; } } } subtitle.Renumber(); }
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(); }
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 allText = sb.ToString(); if (!allText.Contains("\"lines\"")) { return; } var jp = new SeJsonParser(); var fragments = jp.GetArrayElementsByName(allText, "fragments"); for (var i = 0; i < fragments.Count; i++) { var fragment = fragments[i]; var startTime = jp.GetFirstObject(fragment, "begin"); var endTime = jp.GetFirstObject(fragment, "end"); var texts = jp.GetArrayElementsByName(fragment, "lines"); if (decimal.TryParse(startTime, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var startSeconds) && decimal.TryParse(endTime, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var endSeconds)) { var text = string.Join(Environment.NewLine, texts); var p = new Paragraph(text, (double)startSeconds * 1000.0, (double)endSeconds * 1000.0); subtitle.Paragraphs.Add(p); } else { _errorCount++; } } subtitle.RemoveEmptyLines(); subtitle.Renumber(); }
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.Contains("\"alternatives\"", StringComparison.Ordinal) || !text.Contains("\"words\"", StringComparison.Ordinal)) { return; } var parser = new SeJsonParser(); var words = parser.GetArrayElementsByName(text, "words"); foreach (var word in words) { var startTimeObject = parser.GetFirstObject(word, "startTime"); var endTimeObject = parser.GetFirstObject(word, "endTime"); var s = parser.GetFirstObject(word, "word"); if (!string.IsNullOrEmpty(s) && !string.IsNullOrEmpty(startTimeObject) && !string.IsNullOrEmpty(endTimeObject)) { if (double.TryParse(startTimeObject.TrimEnd('s'), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var startSecs) && double.TryParse(endTimeObject.TrimEnd('s'), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var endSecs)) { var pText = Json.DecodeJsonText(s); var p = new Paragraph(pText, startSecs * 1000.0, endSecs * 1000.0); subtitle.Paragraphs.Add(p); } else { _errorCount++; } } } subtitle.Renumber(); }
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.IndexOf("captiondata", StringComparison.Ordinal) < 0) { return; } subtitle.Paragraphs.Clear(); var parser = new SeJsonParser(); var captionDataAr = parser.GetArrayElementsByName(text, "captiondata"); foreach (var captionData in captionDataAr) { var texts = parser.GetArrayElementsByName(captionData, "data"); foreach (var textElement in texts) { var start = parser.GetFirstObject(textElement, "fromms"); var end = parser.GetFirstObject(textElement, "toms"); var caption = parser.GetFirstObject(textElement, "caption"); if (!string.IsNullOrEmpty(caption) && double.TryParse(start, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var startMs) && double.TryParse(end, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var endMs)) { caption = Json.DecodeJsonText(caption); caption = caption.Replace("<br />", Environment.NewLine); var p = new Paragraph(caption, startMs, endMs); subtitle.Paragraphs.Add(p); } } } subtitle.Renumber(); }
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 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, "start"); var endTimeObject = parser.GetFirstObject(element, "end"); var texts = parser.GetAllTagsByNameAsStrings(element, "text"); if (texts.Count > 0 && !string.IsNullOrEmpty(startTimeObject) && !string.IsNullOrEmpty(endTimeObject)) { if (long.TryParse(startTimeObject, out var startMs) && long.TryParse(endTimeObject, out var endMs)) { var p = new Paragraph(string.Join(Environment.NewLine, texts), startMs, endMs); subtitle.Paragraphs.Add(p); } else { _errorCount++; } } } subtitle.Renumber(); }
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 format = new TwentyThreeJson(); var innerJson = Json.DecodeJsonText(subtitleData); format.LoadSubtitle(subtitle, new List <string> { innerJson }, null); }
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.IndexOf("canvas_config", StringComparison.Ordinal) < 0) { return; } subtitle.Paragraphs.Clear(); var parser = new SeJsonParser(); var textDictionary = new Dictionary <string, string>(); var texts = parser.GetArrayElementsByName(text, "texts"); foreach (var textElement in texts) { var id = parser.GetFirstObject(textElement, "id"); var content = parser.GetFirstObject(textElement, "content"); if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(content)) { textDictionary.Add(id, content); } } var timeDictionary = new Dictionary <string, Paragraph>(); var tracks = parser.GetArrayElementsByName(text, "tracks"); foreach (var track in tracks) { var trackType = parser.GetFirstObject(track, "type"); if (trackType == "text") { var segments = parser.GetArrayElementsByName(track, "segments"); foreach (var segment in segments) { var id = parser.GetFirstObject(segment, "material_id"); var targetTimeRage = parser.GetFirstObject(segment, "target_timerange"); if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(targetTimeRage)) { var start = parser.GetFirstObject(targetTimeRage, "start"); var duration = parser.GetFirstObject(targetTimeRage, "duration"); if (double.TryParse(start, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var startNumber) && double.TryParse(duration, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out var durationNumber)) { var startMs = startNumber / 1_000; var durationMs = durationNumber / 1_000; var endMs = startMs + durationMs; var p = new Paragraph(string.Empty, startMs, endMs); timeDictionary.Add(id, p); } } } } } foreach (var kvp in textDictionary) { if (timeDictionary.TryGetValue(kvp.Key, out var p)) { p.Text = kvp.Value; subtitle.Paragraphs.Add(p); } } subtitle.Sort(Enums.SubtitleSortCriteria.StartTime); subtitle.Renumber(); }