using System; using System.Collections.Generic; using System.Text; using System.Xml; namespace Nikse.SubtitleEdit.Core.SubtitleFormats { class UnknownSubtitle78 : SubtitleFormat { public override string Extension { get { return ".xml"; } } public override string Name { get { return "Unknown 78"; } } public override bool IsTimeBased { get { return true; } } public override bool IsMine(List lines, string fileName) { var subtitle = new Subtitle(); LoadSubtitle(subtitle, lines, fileName); return subtitle.Paragraphs.Count > _errorCount; } public override string ToText(Subtitle subtitle, string title) { const string xmpTemplate = @" CCTV Subtitle Sequence File 1.0 1 CG1 Jetsen CCTV Subtitle Sequence Generate by Jetsen [YYYY-MM-DD] [YYYY-MM-DD] 1 0 0 HD_1080_25i 1 1008 2 2 [TIME_CODE_FIRST] [TIME_CODE_LAST] 0 66373 IsContinuousClip=FALSE 0 0 0 0 52 0 52 53 0 "; const string paragraphTemplate = @" 00:00:15:09 00:00:16:14 0 0 0 0 29 0 29 30 0 "; var xml = new XmlDocument(); var firstTimeCode = new TimeCode(0); var lastTimeCode = new TimeCode(0); if (subtitle.Paragraphs.Count > 0) { firstTimeCode = subtitle.Paragraphs[0].StartTime; lastTimeCode = subtitle.Paragraphs[subtitle.Paragraphs.Count - 1].StartTime; } string today = DateTime.Now.ToString("YYYY-mm-DD"); xml.LoadXml(xmpTemplate.Replace('\'', '"').Replace("[YYYY-MM-DD]", today).Replace("[TIME_CODE_FIRST]", firstTimeCode.ToHHMMSSFF()).Replace("[TIME_CODE_LAST]", lastTimeCode.ToHHMMSSFF())); var paragraphInsertNode = xml.DocumentElement.SelectSingleNode("TextSection"); foreach (Paragraph p in subtitle.Paragraphs) { XmlNode paragraph = xml.CreateElement("TextScreen"); paragraph.InnerXml = paragraphTemplate; paragraph.SelectSingleNode("TimeCodeIn").InnerText = p.StartTime.ToHHMMSSFF(); paragraph.SelectSingleNode("TimeCodeOut").InnerText = p.EndTime.ToHHMMSSFF(); var textBlockNodes = paragraph.SelectNodes("TextBlock"); textBlockNodes[0].SelectSingleNode("String").InnerText = p.Text; paragraphInsertNode.AppendChild(paragraph); } return ToUtf8XmlString(xml).Replace(" xmlns=\"\"", string.Empty); } public override void LoadSubtitle(Subtitle subtitle, List lines, string fileName) { _errorCount = 0; var sb = new StringBuilder(); lines.ForEach(line => sb.AppendLine(line)); var xmlAsText = sb.ToString().Trim(); if (!xmlAsText.Contains("") || !xmlAsText.Contains("")) { return; } try { var xml = new XmlDocument { XmlResolver = null }; xml.LoadXml(xmlAsText); foreach (XmlNode node in xml.DocumentElement.SelectNodes("TextSection/TextScreen")) { try { var timeCodeIn = DecodeTimeCode(node.SelectSingleNode("TimeCodeIn").InnerText); var timeCodeOut = DecodeTimeCode(node.SelectSingleNode("TimeCodeOut").InnerText); sb.Clear(); foreach (XmlNode textBlockNode in node.SelectNodes("TextBlock")) { sb.AppendLine(textBlockNode.InnerText); } var p = new Paragraph(timeCodeIn, timeCodeOut, sb.ToString().Trim()); subtitle.Paragraphs.Add(p); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); _errorCount++; } } subtitle.Renumber(); } catch (Exception) { _errorCount++; } } private static TimeCode DecodeTimeCode(string timeCode) { //00:00:07:12 var parts = timeCode.Split(':'); var hour = int.Parse(parts[0]); var minutes = int.Parse(parts[1]); var seconds = int.Parse(parts[2]); var frames = int.Parse(parts[3]); return new TimeCode(hour, minutes, seconds, FramesToMillisecondsMax999(frames)); } } }