Added Adobe Encore NTSC (drop-frame timecode) + another format

git-svn-id: https://subtitleedit.googlecode.com/svn/trunk@968 99eadd0c-20b8-1223-b5c4-2a2b2df33de2
This commit is contained in:
niksedk 2012-02-08 18:20:59 +00:00
parent 89f6faa8a1
commit b186b7fc02
4 changed files with 257 additions and 0 deletions

View File

@ -0,0 +1,124 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
{
class AdobeEncoreWithLineNumbersNtsc : SubtitleFormat
{
static Regex regexTimeCodes = new Regex(@"^\d+ \d\d;\d\d;\d\d;\d\d \d\d;\d\d;\d\d;\d\d ", RegexOptions.Compiled);
public override string Extension
{
get { return ".txt"; }
}
public override string Name
{
get { return "Adobe Encore NTSC"; }
}
public override bool HasLineNumber
{
get { return false; }
}
public override bool IsTimeBased
{
get { return true; }
}
public override bool IsMine(List<string> lines, string fileName)
{
var subtitle = new Subtitle();
StringBuilder sb = new StringBuilder();
foreach (string line in lines)
sb.AppendLine(line);
if (sb.ToString().Contains("#INPOINT OUTPOINT PATH"))
return false; // Pinnacle Impression
LoadSubtitle(subtitle, lines, fileName);
return subtitle.Paragraphs.Count > _errorCount;
}
public override string ToText(Subtitle subtitle, string title)
{
StringBuilder sb = new StringBuilder();
int index = 0;
foreach (Paragraph p in subtitle.Paragraphs)
{
//00:03:15:22 00:03:23:10 This is line one.
//This is line two.
sb.AppendLine(string.Format("{0} {1} {2} {3}", index + 1, EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), Utilities.RemoveHtmlTags(p.Text)));
index++;
}
return sb.ToString();
}
private string EncodeTimeCode(TimeCode time)
{
//00:03:15:22 (last is frame)
return string.Format("{0:00};{1:00};{2:00};{3:00}", time.Hours, time.Minutes, time.Seconds, MillisecondsToFrames(time.Milliseconds));
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
//00:03:15:22 00:03:23:10 This is line one.
//This is line two.
Paragraph p = null;
subtitle.Paragraphs.Clear();
foreach (string line in lines)
{
if (regexTimeCodes.IsMatch(line))
{
string temp = line.Substring(0, regexTimeCodes.Match(line).Length);
temp = line.Substring(line.IndexOf(" ")).Trim();
string start = temp.Substring(0, 11);
string end = temp.Substring(12, 11);
string[] startParts = start.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
string[] endParts = end.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
if (startParts.Length == 4 && endParts.Length == 4)
{
string text = line.Remove(0, regexTimeCodes.Match(line).Length - 1).Trim();
p = new Paragraph(DecodeTimeCode(startParts), DecodeTimeCode(endParts), text);
subtitle.Paragraphs.Add(p);
}
}
else if (line.Trim().Length == 0)
{
// skip these lines
}
else if (line.Trim().Length > 0 && p != null)
{
if (string.IsNullOrEmpty(p.Text))
p.Text = line;
else
p.Text = p.Text + Environment.NewLine + line;
}
}
subtitle.Renumber(1);
}
private TimeCode DecodeTimeCode(string[] parts)
{
//00;00;07;12
string hour = parts[0];
string minutes = parts[1];
string seconds = parts[2];
string frames = parts[3];
int milliseconds = (int)((1000.0 / Configuration.Settings.General.CurrentFrameRate) * int.Parse(frames));
if (milliseconds > 999)
milliseconds = 999;
TimeCode tc = new TimeCode(int.Parse(hour), int.Parse(minutes), int.Parse(seconds), milliseconds);
return tc;
}
}
}

View File

@ -20,6 +20,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
new AdobeEncoreLineTabs(),
new AdobeEncoreTabs(),
new AdobeEncoreWithLineNumbers(),
new AdobeEncoreWithLineNumbersNtsc(),
new AdvancedSubStationAlpha(),
new AQTitle(),
new AvidCaption(),
@ -87,6 +88,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
new UnknownSubtitle11(),
new UnknownSubtitle12(),
new UnknownSubtitle13(),
new UnknownSubtitle14(),
new UTSubtitleXml(),
new Utx(),
new UtxFrames(),

View File

@ -0,0 +1,129 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
{
class UnknownSubtitle14 : SubtitleFormat
{
public override string Extension
{
get { return ".xml"; }
}
public override string Name
{
get { return "Unknown 14"; }
}
public override bool HasLineNumber
{
get { return false; }
}
public override bool IsTimeBased
{
get { return true; }
}
public override bool IsMine(List<string> lines, string fileName)
{
Subtitle subtitle = new Subtitle();
this.LoadSubtitle(subtitle, lines, fileName);
return subtitle.Paragraphs.Count > 0;
}
public override string ToText(Subtitle subtitle, string title)
{
//<Phrase TimeStart="4020" TimeEnd="6020">
// <Text>XYZ PRESENTS</Text>
//</Phrase>
string xmlStructure =
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + Environment.NewLine +
"<Subtitle xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"></Subtitle>";
var xml = new XmlDocument();
xml.LoadXml(xmlStructure);
int id = 1;
foreach (Paragraph p in subtitle.Paragraphs)
{
XmlNode paragraph = xml.CreateElement("Phrase");
XmlAttribute start = xml.CreateAttribute("TimeStart");
start.InnerText = p.StartTime.TotalMilliseconds.ToString();
paragraph.Attributes.Append(start);
XmlAttribute duration = xml.CreateAttribute("TimeEnd");
duration.InnerText = p.EndTime.TotalMilliseconds.ToString();
paragraph.Attributes.Append(duration);
XmlNode text = xml.CreateElement("Text");
text.InnerText = Utilities.RemoveHtmlTags(p.Text).Replace(Environment.NewLine, "\\n");
paragraph.AppendChild(text);
xml.DocumentElement.AppendChild(paragraph);
id++;
}
var ms = new MemoryStream();
var writer = new XmlTextWriter(ms, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
xml.Save(writer);
return Encoding.UTF8.GetString(ms.ToArray()).Trim();
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
_errorCount = 0;
StringBuilder sb = new StringBuilder();
lines.ForEach(line => sb.AppendLine(line));
string allText = sb.ToString();
if (!allText.Contains("<Subtitle") || !allText.Contains("TimeStart="))
return;
XmlDocument xml = new XmlDocument();
try
{
xml.LoadXml(allText);
}
catch (Exception exception)
{
System.Diagnostics.Debug.WriteLine(exception.Message);
_errorCount = 1;
return;
}
foreach (XmlNode node in xml.DocumentElement.SelectNodes("Phrase"))
{
try
{
string start = node.Attributes["TimeStart"].InnerText;
string end = node.Attributes["TimeEnd"].InnerText;
string text = node.SelectSingleNode("Text").InnerText;
text = text.Replace("\\n", Environment.NewLine);
subtitle.Paragraphs.Add(new Paragraph(DecodeTimeCode(start), DecodeTimeCode(end), text));
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
_errorCount++;
}
}
subtitle.Renumber(1);
}
private TimeCode DecodeTimeCode(string timeCode)
{
return new TimeCode(TimeSpan.FromMilliseconds(long.Parse(timeCode)));
}
}
}

View File

@ -579,6 +579,7 @@
<Compile Include="Logic\SubtitleFormats\AdobeEncoreLineTabs.cs" />
<Compile Include="Logic\SubtitleFormats\AdobeEncoreTabs.cs" />
<Compile Include="Logic\SubtitleFormats\AdobeEncoreWithLineNumbers.cs" />
<Compile Include="Logic\SubtitleFormats\AdobeEncoreWithLineNumbersNtsc.cs" />
<Compile Include="Logic\SubtitleFormats\AdvancedSubStationAlpha.cs" />
<Compile Include="Logic\SubtitleFormats\AQTitle.cs" />
<Compile Include="Logic\SubtitleFormats\AvidCaption.cs" />
@ -628,6 +629,7 @@
<Compile Include="Logic\SubtitleFormats\UnknownSubtitle11.cs" />
<Compile Include="Logic\SubtitleFormats\UnknownSubtitle12.cs" />
<Compile Include="Logic\SubtitleFormats\UnknownSubtitle13.cs" />
<Compile Include="Logic\SubtitleFormats\UnknownSubtitle14.cs" />
<Compile Include="Logic\SubtitleFormats\UnknownSubtitle5.cs" />
<Compile Include="Logic\SubtitleFormats\OpenDvt.cs" />
<Compile Include="Logic\SubtitleFormats\AbcIViewer.cs" />