我正在开发一个 Web 应用程序,它可以接收时间文本标记(TTML)或WebVTT格式的字幕文件。如果文件是定时文本,我想把它翻译成 WebVTT。这基本上不是问题,我遇到的一个问题是,如果 TTML 将 HTML 作为文本内容的一部分,那么 HTML 标记就会被删除。
例如:
<p begin="00:00:08.18" dur="00:00:03.86">(Music<br />playing)</p>
结果是:
(Musicplaying)
我使用的代码是:
private const string TIME_FORMAT = "hh\\:mm\\:ss\\.fff";
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(fileLocation);
XDocument xdoc = xmldoc.ToXDocument();
var ns = (from x in xdoc.Root.DescendantsAndSelf()
select x.Name.Namespace).First();
List<TTMLElement> elements =
(
from item in xdoc.Descendants(ns + "body").Descendants(ns + "div").Descendants(ns + "p")
select new TTMLElement
{
text = item.Value,
startTime = TimeSpan.Parse(item.Attribute("begin").Value),
duration = TimeSpan.Parse(item.Attribute("dur").Value),
}
).ToList<TTMLElement>();
StringBuilder sb = new StringBuilder();
sb.AppendLine("WEBVTT");
sb.AppendLine();
for (int i = 0; i < elements.Count; i++)
{
sb.AppendLine(i.ToString());
sb.AppendLine(elements[i].startTime.ToString(TIME_FORMAT) + " --> " + elements[i].startTime.Add(elements[i].duration).ToString(TIME_FORMAT));
sb.AppendLine(elements[i].text);
sb.AppendLine();
}
任何关于我遗漏的想法,或者是否有更好的方法,或者即使已经有将时间文本转换为 WebVTT 的解决方案,我们都将不胜感激。谢谢。