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; } }
/// <summary> /// The load subtitle. /// </summary> /// <param name="subtitle"> /// The subtitle. /// </param> /// <param name="lines"> /// The lines. /// </param> /// <param name="fileName"> /// The file name. /// </param> public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName) { this._errorCount = 0; StringBuilder sb = new StringBuilder(); lines.ForEach(line => sb.AppendLine(line)); if (!sb.ToString().Contains("</annotations>") || !sb.ToString().Contains("</TEXT>")) { return; } XmlDocument xml = new XmlDocument(); xml.XmlResolver = null; try { string xmlText = sb.ToString(); xml.LoadXml(xmlText); List<string> styles = new List<string> { "speech" }; if (this._promtForStyles) { Dictionary<string, int> 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) { 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 (string 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.Contains(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) { string startTime = regions[0].Attributes["t"].Value; string endTime = regions[1].Attributes["t"].Value; Paragraph p = new Paragraph(); p.StartTime = DecodeTimeCode(startTime); p.EndTime = DecodeTimeCode(endTime); p.Text = textNode.InnerText; subtitle.Paragraphs.Add(p); } } } catch (Exception ex) { Debug.WriteLine(ex.Message); this._errorCount++; } } subtitle.Sort(SubtitleSortCriteria.StartTime); // force order by start time subtitle.Renumber(); } catch (Exception ex) { Debug.WriteLine(ex.Message); this._errorCount = 1; } }