using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class FinalCutProXml14 : SubtitleFormat
{
public double FrameRate { get; set; }
public override string Extension => ".fcpxml";
public override string Name => "Final Cut Pro Xml 1.4";
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 +
" Apple ProRes 422 Proxy" + Environment.NewLine +
" Linear PCM" + 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 + // From here down I am unsure how it should be
" " + Environment.NewLine +
" " + Environment.NewLine +
" " + Environment.NewLine +
" " + Environment.NewLine +
" " + Environment.NewLine +
" " + Environment.NewLine +
"";
string xmlClipStructure =
" ";
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 video = xml.CreateElement("video");
video.InnerXml = xmlClipStructure;
XmlNode generatorNode = video.SelectSingleNode("video");
generatorNode.Attributes["offset"].Value = Convert.ToInt64(p.StartTime.TotalSeconds * 2400000) + "/2400000s";
generatorNode.Attributes["duration"].Value = Convert.ToInt64(p.Duration.TotalSeconds * 2400000) + "/2400000s";
generatorNode.Attributes["start"].Value = Convert.ToInt64(p.StartTime.TotalSeconds * 2400000) + "/2400000s";
XmlNode param = video.SelectSingleNode("video/param");
param.Attributes["value"].InnerText = HtmlUtil.RemoveHtmlTags(p.Text);
videoNode.AppendChild(generatorNode);
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));
string x = sb.ToString();
if (!x.Contains("") && !x.Contains(""))
{
return;
}
var xml = new XmlDocument { XmlResolver = null };
try
{
xml.LoadXml(x.Trim());
foreach (XmlNode node in xml.SelectNodes("//project/sequence/spine/clip/video/param[@name='Text']"))
{
try
{
string text = node.Attributes["value"].InnerText;
Paragraph p = new Paragraph();
p.Text = text.Trim();
p.StartTime = DecodeTime(node.ParentNode.Attributes["offset"]);
p.EndTime.TotalMilliseconds = p.StartTime.TotalMilliseconds + DecodeTime(node.ParentNode.Attributes["duration"]).TotalMilliseconds;
subtitle.Paragraphs.Add(p);
}
catch
{
_errorCount++;
}
}
if (subtitle.Paragraphs.Count == 0)
{
foreach (XmlNode node in xml.SelectNodes("//project/sequence/spine/clip/video/title/text"))
{
try
{
string text = node.ParentNode.InnerText;
Paragraph p = new Paragraph();
p.Text = text.Trim();
p.StartTime = DecodeTime(node.ParentNode.Attributes["offset"]);
p.EndTime.TotalMilliseconds = p.StartTime.TotalMilliseconds + DecodeTime(node.ParentNode.Attributes["duration"]).TotalMilliseconds;
bool add = true;
if (subtitle.Paragraphs.Count > 0)
{
var prev = subtitle.Paragraphs[subtitle.Paragraphs.Count - 1];
if (prev.Text == p.Text && Math.Abs(prev.StartTime.TotalMilliseconds - p.StartTime.TotalMilliseconds) < 0.01)
{
add = false;
}
}
if (add)
{
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();
}
}
}