예제 #1
0
        public override void LoadSubtitle(Subtitle subtitle, List <string> lines, string fileName)
        {
            _errorCount = 0;
            var sb = new StringBuilder();

            lines.ForEach(line => sb.AppendLine(line));

            var xmlString = sb.ToString();

            if (!xmlString.Contains("smpte:backgroundImage") || !xmlString.Contains("smpte:image") || !xmlString.Contains("imagetype="))
            {
                return;
            }

            var xml = new XmlDocument {
                XmlResolver = null
            };

            try
            {
                xml.LoadXml(xmlString);
            }
            catch
            {
                _errorCount = 1;
                return;
            }

            var nsmgr = new XmlNamespaceManager(xml.NameTable);

            nsmgr.AddNamespace("smpte", "http://www.smpte-ra.org/schemas/2052-1/2010/smpte-tt");
            nsmgr.AddNamespace("tt", "http://www.w3.org/ns/ttml");
            var       images   = xml.DocumentElement.SelectNodes("//smpte:image", nsmgr);
            var       bgImages = xml.DocumentElement.SelectNodes("//tt:div", nsmgr);
            Paragraph last     = null;

            for (var i = 0; i < Math.Min(images.Count, bgImages.Count); i++)
            {
                var image = images[i];
                var text  = image.InnerText;

                var bgImage = bgImages[i];
                if (bgImage.Attributes?["begin"] != null && bgImage.Attributes["end"] != null)
                {
                    var p = new Paragraph {
                        Text = text
                    };

                    // Time codes
                    TimedText10.ExtractTimeCodes(bgImage, subtitle, out var begin, out var end);
                    p.StartTime.TotalMilliseconds = begin.TotalMilliseconds;
                    p.EndTime.TotalSeconds        = end.TotalSeconds;

                    if (last != null && last.Text == p.Text && Math.Abs(last.EndTime.TotalMilliseconds - p.EndTime.TotalMilliseconds) < 3000)
                    {
                        last.EndTime.TotalMilliseconds = p.EndTime.TotalMilliseconds;
                    }
                    else
                    {
                        subtitle.Paragraphs.Add(p);
                    }

                    last = p;
                }
            }

            subtitle.Renumber();
        }
예제 #2
0
        public override void LoadSubtitle(Subtitle subtitle, List <string> lines, string fileName)
        {
            _errorCount = 0;

            var sb = new StringBuilder();

            lines.ForEach(line => sb.AppendLine(line));
            var xml = new XmlDocument {
                XmlResolver = null, PreserveWhitespace = true
            };

            try
            {
                xml.LoadXml(sb.ToString().RemoveControlCharactersButWhiteSpace().Trim());
            }
            catch
            {
                xml.LoadXml(sb.ToString().Replace(" & ", " &amp; ").Replace("Q&A", "Q&amp;A").RemoveControlCharactersButWhiteSpace().Trim());
            }

            var frameRateAttr = xml.DocumentElement.Attributes["ttp:frameRate"];

            if (frameRateAttr != null)
            {
                if (double.TryParse(frameRateAttr.Value, out var fr))
                {
                    if (fr > 20 && fr < 100)
                    {
                        Configuration.Settings.General.CurrentFrameRate = fr;
                    }

                    var frameRateMultiplier = xml.DocumentElement.Attributes["ttp:frameRateMultiplier"];
                    if (frameRateMultiplier != null)
                    {
                        if (frameRateMultiplier.InnerText == "999 1000" && Math.Abs(fr - 30) < 0.01)
                        {
                            Configuration.Settings.General.CurrentFrameRate = 29.97;
                        }
                        else if (frameRateMultiplier.InnerText == "999 1000" && Math.Abs(fr - 24) < 0.01)
                        {
                            Configuration.Settings.General.CurrentFrameRate = 23.976;
                        }
                        else
                        {
                            var arr = frameRateMultiplier.InnerText.Split();
                            if (arr.Length == 2 && Utilities.IsInteger(arr[0]) && Utilities.IsInteger(arr[1]) && int.Parse(arr[1]) > 0)
                            {
                                fr = double.Parse(arr[0]) / double.Parse(arr[1]);
                                if (fr > 20 && fr < 100)
                                {
                                    Configuration.Settings.General.CurrentFrameRate = fr;
                                }
                            }
                        }
                    }
                }
            }

            if (BatchSourceFrameRate.HasValue)
            {
                Configuration.Settings.General.CurrentFrameRate = BatchSourceFrameRate.Value;
            }

            Configuration.Settings.SubtitleSettings.TimedText10TimeCodeFormatSource = null;
            subtitle.Header = sb.ToString();

            var namespaceManager = new XmlNamespaceManager(xml.NameTable);

            namespaceManager.AddNamespace("ttml", "http://www.w3.org/ns/ttml");
            var body = xml.DocumentElement.SelectSingleNode("ttml:body", namespaceManager);

            foreach (XmlNode node in body.SelectNodes("//ttml:p", namespaceManager))
            {
                TimedText10.ExtractTimeCodes(node, subtitle, out var begin, out var end);
                var assStyle = string.Empty;
                var region   = node.Attributes?["region"];
                if (region != null)
                {
                    assStyle = GetAssStyleFromRegion(region.InnerText);
                }

                var text = assStyle + ReadParagraph(node, xml);
                var p    = new Paragraph(begin, end, text);
                subtitle.Paragraphs.Add(p);
            }

            subtitle.Renumber();
        }