public List <string> GetYouTubeAnnotationStyles(Dictionary <string, int> stylesWithCount)
        {
            if (stylesWithCount == null)
            {
                return(new List <string>());
            }

            using (var import = new YouTubeAnnotationsImport(stylesWithCount))
            {
                if (import.ShowDialog() == DialogResult.OK)
                {
                    return(import.SelectedStyles);
                }
                var styles = new List <string>();
                foreach (var k in stylesWithCount.Keys)
                {
                    styles.Add(k);
                }
                return(styles);
            }
        }
예제 #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));
            if (!sb.ToString().Contains("</annotations>") || !sb.ToString().Contains("</TEXT>"))
            {
                return;
            }
            var xml = new XmlDocument();

            try
            {
                string xmlText = sb.ToString();
                xml.LoadXml(xmlText);
                List <string> styles = new List <string> {
                    "speech"
                };

                if (_promtForStyles)
                {
                    var stylesWithCount = new Dictionary <string, int>();
                    foreach (XmlNode node in xml.SelectNodes("//annotation"))
                    {
                        try
                        {
                            if (node.Attributes["style"] != null && node.Attributes["style"].Value != null)
                            {
                                string style = node.Attributes["style"].Value;

                                XmlNode     textNode = node.SelectSingleNode("TEXT");
                                XmlNodeList regions  = node.SelectNodes("segment/movingRegion/anchoredRegion");

                                if (regions.Count != 2)
                                {
                                    regions = node.SelectNodes("segment/movingRegion/rectRegion");
                                }

                                if (textNode != null && regions.Count == 2)
                                {
                                    if (stylesWithCount.ContainsKey(style))
                                    {
                                        stylesWithCount[style]++;
                                    }
                                    else
                                    {
                                        stylesWithCount.Add(style, 1);
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            System.Diagnostics.Debug.WriteLine(ex.Message);
                        }
                    }
                    if (stylesWithCount.Count > 1)
                    {
                        YouTubeAnnotationsImport import = new YouTubeAnnotationsImport(stylesWithCount);
                        if (import.ShowDialog() == DialogResult.OK)
                        {
                            styles = import.SelectedStyles;
                        }
                    }
                    else
                    {
                        styles.Clear();
                        foreach (var k in stylesWithCount.Keys)
                        {
                            styles.Add(k);
                        }
                    }
                }
                else
                {
                    styles.Add("popup");
                    styles.Add("anchored");
                }

                foreach (XmlNode node in xml.SelectNodes("//annotation"))
                {
                    try
                    {
                        if (node.Attributes["style"] != null && styles.IndexOf(node.Attributes["style"].Value) >= 0)
                        {
                            XmlNode     textNode = node.SelectSingleNode("TEXT");
                            XmlNodeList regions  = node.SelectNodes("segment/movingRegion/anchoredRegion");

                            if (regions.Count != 2)
                            {
                                regions = node.SelectNodes("segment/movingRegion/rectRegion");
                            }

                            if (textNode != null && regions.Count == 2)
                            {
                                string startTime = regions[0].Attributes["t"].Value;
                                string endTime   = regions[1].Attributes["t"].Value;
                                var    p         = new Paragraph();
                                p.StartTime = DecodeTimeCode(startTime);
                                p.EndTime   = DecodeTimeCode(endTime);
                                p.Text      = textNode.InnerText;
                                subtitle.Paragraphs.Add(p);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine(ex.Message);
                        _errorCount++;
                    }
                }
                subtitle.Sort(Enums.SubtitleSortCriteria.StartTime); // force order by start time
                subtitle.Renumber(1);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                _errorCount = 1;
                return;
            }
        }