private static Line ReadLine(XmlElement elem, List <Line> positions, List <Line> windowStyles, List <Section> pens)
        {
            int t = elem.GetIntAttribute("t") ?? 0;
            int d = elem.GetIntAttribute("d") ?? 5000;

            if (t == 1) // Reverse Android workaround (See WriteLine)
            {
                t--;
                d++;
            }

            DateTime start = TimeBase.AddMilliseconds(t);
            DateTime end   = start.AddMilliseconds(d);
            Line     line  = new Line(start, end);

            Line position = GetItemAtIndex(positions, elem.GetIntAttribute("wp"));

            if (position != null)
            {
                line.AnchorPoint = position.AnchorPoint;
                line.Position    = position.Position;
            }

            Line windowStyle = GetItemAtIndex(windowStyles, elem.GetIntAttribute("ws"));

            if (windowStyle != null)
            {
                line.VerticalTextType = windowStyle.VerticalTextType;
            }

            Section linePen = GetItemAtIndex(pens, elem.GetIntAttribute("p"));

            line.Sections.AddRange(ReadSections(elem, pens, linePen).Where(s => s.Text.Length > 0 || s.StartOffset > TimeSpan.Zero));
            return(line);
        }
Exemple #2
0
        public YttDocument(string filePath)
        {
            XmlDocument doc = new XmlDocument();

            doc.Load(filePath);
            foreach (XmlElement parElem in doc.SelectNodes("/timedtext/body/p"))
            {
                int.TryParse(parElem.GetAttribute("t"), out int startMs);
                int.TryParse(parElem.GetAttribute("d"), out int durationMs);

                DateTime start = TimeBase.AddMilliseconds(startMs + SubtitleDelayMs);
                DateTime end   = start.AddMilliseconds(durationMs);
                Line     line  = new Line(start, end, parElem.InnerText);
                Lines.Add(line);
            }
        }
Exemple #3
0
        /// <summary>
        /// On PC, the first piece of italic text shows up with a noticeable delay: the background appears as usual,
        /// but for a fraction of a second after that, the text is either shown as non-italic or not shown at all.
        /// To work around this, we add an invisible italic subtitle at the start to make YouTube eagerly load
        /// whatever it normally loads lazily.
        /// </summary>
        private void AddItalicPrefetch()
        {
            Line italicLine =
                new Line(TimeBase.AddMilliseconds(SubtitleDelayMs), TimeBase.AddMilliseconds(SubtitleDelayMs + 100))
            {
                Position    = new PointF(0, 0),
                AnchorPoint = AnchorPoint.BottomRight
            };
            Section section =
                new Section("\x200B")
            {
                ForeColor = Color.FromArgb(1, 0, 0, 0),
                BackColor = Color.Empty,
                Italic    = true
            };

            italicLine.Sections.Add(section);
            Lines.Add(italicLine);
        }
        /// <summary>
        /// On PC, the first piece of italic text shows up with a noticeable delay: the background appears instantly,
        /// but during a fraction of a second after that, the text is either shown as non-italic or not shown at all.
        /// To work around this, we add an invisible italic subtitle at the start to make YouTube eagerly load
        /// whatever it normally loads lazily.
        /// </summary>
        private void AddItalicPrefetch()
        {
            if (!Lines.SelectMany(l => l.Sections).Any(s => s.Italic))
            {
                return;
            }

            Line italicLine =
                new Line(TimeBase.AddMilliseconds(SubtitleDelayMs + 5000), TimeBase.AddMilliseconds(SubtitleDelayMs + 5100))
            {
                Position    = new PointF(0, 0),
                AnchorPoint = AnchorPoint.BottomRight
            };
            Section section =
                new Section("\x200B")
            {
                ForeColor = Color.FromArgb(1, 0, 0, 0),
                BackColor = Color.Empty,
                Italic    = true
            };

            italicLine.Sections.Add(section);
            Lines.Add(italicLine);
        }