using System; using System.Collections.Generic; using System.Text; using System.Xml; namespace Nikse.SubtitleEdit.Core.SubtitleFormats { public class FinalCutProXCM : SubtitleFormat { public double FrameRate { get; set; } public override string Extension => ".fcpxml"; public override string Name => "Final Cut Pro X Chapter Marker"; public override string ToText(Subtitle subtitle, string title) { if (Configuration.Settings.General.CurrentFrameRate > 26) { FrameRate = 30; } else { FrameRate = 25; } string xmlStructure = "" + Environment.NewLine + "" + Environment.NewLine + Environment.NewLine + "" + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + " " + Environment.NewLine + ""; var xml = new XmlDocument(); xml.LoadXml(xmlStructure); XmlNode videoNode = xml.DocumentElement.SelectSingleNode("project/sequence/spine/clip"); int number = 1; foreach (Paragraph p in subtitle.Paragraphs) { XmlNode chapterMarker = xml.CreateElement("chapter-marker"); var attr = xml.CreateAttribute("duration"); attr.Value = Convert.ToInt64(p.Duration.TotalSeconds * 2400000) + "/2400000s"; chapterMarker.Attributes.Append(attr); attr = xml.CreateAttribute("start"); attr.Value = Convert.ToInt64(p.StartTime.TotalSeconds * 2400000) + "/2400000s"; chapterMarker.Attributes.Append(attr); attr = xml.CreateAttribute("value"); attr.Value = p.Text.Replace(Environment.NewLine, Convert.ToChar(8232).ToString()); chapterMarker.Attributes.Append(attr); attr = xml.CreateAttribute("posterOffset"); attr.Value = "11/24s"; chapterMarker.Attributes.Append(attr); videoNode.AppendChild(chapterMarker); number++; } string xmlAsText = ToUtf8XmlString(xml); xmlAsText = xmlAsText.Replace("fcpxml[]", "fcpxml"); xmlAsText = xmlAsText.Replace("fcpxml []", "fcpxml"); return xmlAsText; } public override void LoadSubtitle(Subtitle subtitle, List lines, string fileName) { _errorCount = 0; FrameRate = Configuration.Settings.General.CurrentFrameRate; var sb = new StringBuilder(); lines.ForEach(line => sb.AppendLine(line)); var xml = new XmlDocument { XmlResolver = null }; xml.PreserveWhitespace = true; try { xml.LoadXml(sb.ToString().Trim()); foreach (XmlNode node in xml.SelectNodes("fcpxml/project/sequence/spine/clip/chapter-marker")) { try { var p = new Paragraph(); p.Text = node.Attributes["value"].InnerText; p.Text = p.Text.Replace(Convert.ToChar(8232).ToString(), Environment.NewLine); p.StartTime = DecodeTime(node.Attributes["start"]); p.EndTime.TotalMilliseconds = p.StartTime.TotalMilliseconds + DecodeTime(node.Attributes["duration"]).TotalMilliseconds; subtitle.Paragraphs.Add(p); } catch { _errorCount++; } } subtitle.Renumber(); } catch { _errorCount = 1; } } private static TimeCode DecodeTime(XmlAttribute duration) { // 220220/60000s if (duration != null) { var arr = duration.Value.TrimEnd('s').Split('/'); if (arr.Length == 2) { return TimeCode.FromSeconds(long.Parse(arr[0]) / double.Parse(arr[1])); } else if (arr.Length == 1) { return TimeCode.FromSeconds(float.Parse(arr[0])); } } return new TimeCode(); } } }