using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class FinalCutProXXml : SubtitleFormat
{
public double FrameRate { get; set; }
public override string Extension => ".fcpxml";
public override string Name => "Final Cut Pro X Xml";
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 +
"";
string xmlClipStructure =
"
" + Environment.NewLine +
" " + Environment.NewLine +
" " + Environment.NewLine +
" ";
var xml = new XmlDocument();
xml.LoadXml(xmlStructure);
XmlNode videoNode = xml.DocumentElement.SelectSingleNode("project/sequence/spine");
int number = 1;
foreach (Paragraph p in subtitle.Paragraphs)
{
XmlNode clip = xml.CreateElement("clip");
clip.InnerXml = xmlClipStructure;
var attr = xml.CreateAttribute("name");
attr.Value = title;
clip.Attributes.Append(attr);
attr = xml.CreateAttribute("duration");
//attr.Value = "9529520/2400000s";
attr.Value = Convert.ToInt64(p.Duration.TotalSeconds * 2400000) + "/2400000s";
clip.Attributes.Append(attr);
attr = xml.CreateAttribute("start");
//attr.Value = "1201200/2400000s";
attr.Value = Convert.ToInt64(p.StartTime.TotalSeconds * 2400000) + "/2400000s";
clip.Attributes.Append(attr);
attr = xml.CreateAttribute("audioStart");
attr.Value = "0s";
clip.Attributes.Append(attr);
attr = xml.CreateAttribute("audioDuration");
attr.Value = Convert.ToInt64(p.Duration.TotalSeconds * 2400000) + "/2400000s";
clip.Attributes.Append(attr);
attr = xml.CreateAttribute("tcFormat");
attr.Value = "NDF";
clip.Attributes.Append(attr);
XmlNode titleNode = clip.SelectSingleNode("title");
titleNode.Attributes["offset"].Value = Convert.ToInt64(p.StartTime.TotalSeconds * 60000) + "/60000s";
titleNode.Attributes["name"].Value = HtmlUtil.RemoveHtmlTags(p.Text);
titleNode.Attributes["duration"].Value = Convert.ToInt64(p.Duration.TotalSeconds * 60000) + "/60000s";
titleNode.Attributes["start"].Value = Convert.ToInt64(p.StartTime.TotalSeconds * 60000) + "/60000s";
XmlNode text = clip.SelectSingleNode("title/text");
text.InnerText = HtmlUtil.RemoveHtmlTags(p.Text);
videoNode.AppendChild(clip);
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 };
try
{
xml.LoadXml(sb.ToString().Trim());
foreach (XmlNode node in xml.SelectNodes("fcpxml/project/sequence/spine/clip"))
{
try
{
foreach (XmlNode title in node.SelectNodes("title"))
{
var role = title.Attributes["role"];
if (role != null && role.InnerText == "Subtitles")
{
var textNode = title.SelectSingleNode("text");
if (textNode != null && !string.IsNullOrEmpty(textNode.InnerText))
{
string text = textNode.InnerText;
Paragraph p = new Paragraph();
p.Text = text.Trim();
p.StartTime = DecodeTime(title.Attributes["offset"]);
p.EndTime.TotalMilliseconds = p.StartTime.TotalMilliseconds + DecodeTime(title.Attributes["duration"]).TotalMilliseconds;
subtitle.Paragraphs.Add(p);
}
}
}
}
catch
{
_errorCount++;
}
}
subtitle.Renumber();
}
catch
{
_errorCount = 1;
return;
}
}
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();
}
}
}