示例#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 allText = sb.ToString().Trim();

            if (!allText.StartsWith("[", StringComparison.Ordinal) || !allText.Contains("\"start\""))
            {
                return;
            }

            foreach (var line in Json.ReadObjectArray(allText))
            {
                var s = line.Trim();
                if (s.Length > 10)
                {
                    var start     = Json.ReadTag(s, "start");
                    var end       = Json.ReadTag(s, "end");
                    var textLines = Json.ReadArray(s, "text");
                    try
                    {
                        if (textLines.Count == 0)
                        {
                            _errorCount++;
                        }
                        sb.Clear();
                        foreach (var textLine in textLines)
                        {
                            sb.AppendLine(Json.DecodeJsonText(textLine));
                        }
                        var startArr = start.Split(new[] { ':', ',', '.' }, StringSplitOptions.RemoveEmptyEntries);
                        var endArr   = end.Split(new[] { ':', ',', '.' }, StringSplitOptions.RemoveEmptyEntries);
                        if (startArr.Length == 4 && endArr.Length == 4)
                        {
                            subtitle.Paragraphs.Add(new Paragraph(sb.ToString().Trim(), TimeCode.ParseToMilliseconds(start), TimeCode.ParseToMilliseconds(end)));
                        }
                    }
                    catch (Exception)
                    {
                        _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("\"text\""))
            {
                return;
            }

            var startTimes = Json.ReadArray(allText, "start");
            var endTimes   = Json.ReadArray(allText, "end");
            var texts      = Json.ReadArray(allText, "text");

            var max = Math.Min(Math.Min(startTimes.Count, texts.Count), endTimes.Count);

            for (var i = 0; i < max; i++)
            {
                if (int.TryParse(startTimes[i], out var startMs) && int.TryParse(endTimes[i], out var endMs))
                {
                    var text = Json.DecodeJsonText(texts[i]);
                    var p    = new Paragraph(text, startMs, endMs);
                    subtitle.Paragraphs.Add(p);
                }
                else
                {
                    _errorCount++;
                }
            }
            subtitle.RemoveEmptyLines();
            subtitle.Renumber();
        }
示例#3
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);
            }

            string allText = sb.ToString();

            if (!allText.Contains("\"text\""))
            {
                return;
            }

            var startTimes = Json.ReadArray(allText, "start");
            var endTimes   = Json.ReadArray(allText, "end");
            var texts      = Json.ReadArray(allText, "text");

            for (int i = 0; i < Math.Min(Math.Min(startTimes.Count, texts.Count), endTimes.Count); i++)
            {
                try
                {
                    string text = Json.DecodeJsonText(texts[i]);
                    var    p    = new Paragraph(text, int.Parse(startTimes[i]), int.Parse(endTimes[i]));
                    subtitle.Paragraphs.Add(p);
                }
                catch
                {
                    _errorCount++;
                }
            }
            subtitle.RemoveEmptyLines();
            subtitle.Renumber();
        }
示例#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);
            }

            string allText = sb.ToString();

            if (!allText.Contains("text_tees"))
            {
                return;
            }

            var times = Json.ReadArray(allText, "text_tees");
            var texts = Json.ReadArray(allText, "text_content");

            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("[\"", StringComparison.Ordinal) && t.EndsWith("\"]", StringComparison.Ordinal))
                            {
                                var innerSb        = new StringBuilder();
                                var innerTextLines = Json.ReadArray("{\"text\":" + t + "}", "text");
                                foreach (string innerLine in innerTextLines)
                                {
                                    innerSb.Append(' ');
                                    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);
                    }
                    var 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();
        }
示例#5
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);
            }

            string allText = sb.ToString();

            if (!allText.Contains("\"words\"") && !allText.Contains("'words'"))
            {
                return;
            }

            var words = Json.ReadArray(allText, "words");

            foreach (string word in words)
            {
                var elements = Json.ReadArray(word);
                if (elements.Count == 2)
                {
                    string milliseconds = elements[0].Trim('"').Trim();
                    string text         = elements[1].Trim();
                    if (text.StartsWith('"'))
                    {
                        text = text.Remove(0, 1);
                    }

                    if (text.EndsWith('"'))
                    {
                        text = text.Remove(text.Length - 1, 1);
                    }

                    if (long.TryParse(milliseconds, out var number))
                    {
                        subtitle.Paragraphs.Add(new Paragraph(text, number, number));
                    }
                    else
                    {
                        _errorCount++;
                    }
                }
            }

            sb.Clear();
            var    sub = new Subtitle();
            double startMilliseconds = 0;

            if (subtitle.Paragraphs.Count > 0)
            {
                startMilliseconds = subtitle.Paragraphs[0].StartTime.TotalMilliseconds;
            }

            for (int i = 0; i < subtitle.Paragraphs.Count - 1; i++)
            {
                Paragraph p    = subtitle.Paragraphs[i];
                Paragraph next = subtitle.Paragraphs[i + 1];
                Paragraph prev = subtitle.GetParagraphOrDefault(i - 1);
                if (sb.Length + p.Text.Length > (Configuration.Settings.General.SubtitleLineMaximumLength * 2) - 15) // text too big
                {
                    var newParagraph = new Paragraph(sb.ToString(), startMilliseconds, prev.EndTime.TotalMilliseconds);
                    sub.Paragraphs.Add(newParagraph);
                    sb.Clear();
                    if (!string.IsNullOrWhiteSpace(p.Text))
                    {
                        sb.Append(p.Text);
                        startMilliseconds = p.StartTime.TotalMilliseconds;
                    }
                }
                else if (next.StartTime.TotalMilliseconds - p.EndTime.TotalMilliseconds > 2000) // long time to next sub
                {
                    if (!string.IsNullOrWhiteSpace(p.Text))
                    {
                        sb.Append(' ');
                        sb.Append(p.Text);
                    }
                    var newParagraph = new Paragraph(sb.ToString(), startMilliseconds, next.StartTime.TotalMilliseconds);
                    sub.Paragraphs.Add(newParagraph);
                    sb.Clear();
                    startMilliseconds = next.StartTime.TotalMilliseconds;
                }
                else if (string.IsNullOrWhiteSpace(p.Text)) // empty text line
                {
                    if (string.IsNullOrWhiteSpace(next.Text) && sb.Length > 0)
                    {
                        var newParagraph = new Paragraph(sb.ToString(), startMilliseconds, next.StartTime.TotalMilliseconds);
                        sub.Paragraphs.Add(newParagraph);
                        sb.Clear();
                    }
                }
                else // just add word to current sub
                {
                    if (sb.Length == 0)
                    {
                        startMilliseconds = p.StartTime.TotalMilliseconds;
                    }

                    sb.Append(' ');
                    sb.Append(p.Text);
                }
            }
            if (sb.Length > 0)
            {
                var newParagraph = new Paragraph(sb.ToString().Trim(), startMilliseconds, Utilities.GetOptimalDisplayMilliseconds(sb.ToString()));
                sub.Paragraphs.Add(newParagraph);
            }

            subtitle.Paragraphs.Clear();
            foreach (Paragraph p in sub.Paragraphs)
            {
                p.Text = Utilities.AutoBreakLine(p.Text);
                subtitle.Paragraphs.Add(new Paragraph(p));
            }
            subtitle.RemoveEmptyLines();
            subtitle.Renumber();
        }