Example #1
0
        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();
        }
Example #2
0
        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();
        }
Example #3
0
        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();
        }
Example #4
0
        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();
        }