mirror of
https://github.com/SubtitleEdit/subtitleedit.git
synced 2024-11-22 03:02:35 +01:00
128 lines
5.3 KiB
C#
128 lines
5.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
|
|
{
|
|
public class TitleExchangePro : SubtitleFormat
|
|
{
|
|
private static readonly Regex RegexTimeCodes = new Regex(@"^\d\d:\d\d:\d\d:\d\d,\t[ ]?\d\d:\d\d:\d\d:\d\d,\t[ ]?", RegexOptions.Compiled);
|
|
|
|
public override string Extension
|
|
{
|
|
get { return ".txt"; }
|
|
}
|
|
|
|
public override string Name
|
|
{
|
|
get { return "TitleExchange Pro"; }
|
|
}
|
|
|
|
public override bool IsTimeBased
|
|
{
|
|
get { return true; }
|
|
}
|
|
|
|
public override bool IsMine(List<string> lines, string fileName)
|
|
{
|
|
var subtitle = new Subtitle();
|
|
|
|
var sb = new StringBuilder();
|
|
foreach (string line in lines)
|
|
sb.AppendLine(line);
|
|
if (sb.ToString().Contains(Environment.NewLine + "SP_NUMBER\tSTART\tEND\tFILE_NAME"))
|
|
return false; // SON
|
|
|
|
LoadSubtitle(subtitle, lines, fileName);
|
|
return subtitle.Paragraphs.Count > _errorCount;
|
|
}
|
|
|
|
public override string ToText(Subtitle subtitle, string title)
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
foreach (Paragraph p in subtitle.Paragraphs)
|
|
{
|
|
//00:01:48:22, 00:01:52:17, - I need those samples, fast!//- Yes, professor.
|
|
string text = p.Text;
|
|
text = text.Replace("<i>", "@Italic@");
|
|
text = text.Replace("</i>", "@Italic@");
|
|
text = text.Replace(Environment.NewLine, "//");
|
|
sb.AppendLine(string.Format("{0},\t{1},\t{2}", EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), HtmlUtil.RemoveHtmlTags(text)));
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
|
|
private static 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, MillisecondsToFramesMaxFrameRate(time.Milliseconds));
|
|
}
|
|
|
|
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
|
|
{
|
|
//00:01:48:22, 00:01:52:17, - I need those samples, fast!//- Yes, professor.
|
|
Paragraph p = null;
|
|
subtitle.Paragraphs.Clear();
|
|
_errorCount = 0;
|
|
foreach (string line in lines)
|
|
{
|
|
string s = line.Replace(" ", "\t");
|
|
if (RegexTimeCodes.IsMatch(s))
|
|
{
|
|
var temp = s.Split('\t');
|
|
if (temp.Length > 1)
|
|
{
|
|
string start = temp[0].TrimEnd(',');
|
|
string end = temp[1].TrimEnd(',');
|
|
|
|
string[] startParts = start.Split(SplitCharColon, StringSplitOptions.RemoveEmptyEntries);
|
|
string[] endParts = end.Split(SplitCharColon, StringSplitOptions.RemoveEmptyEntries);
|
|
if (startParts.Length == 4 && endParts.Length == 4)
|
|
{
|
|
try
|
|
{
|
|
string text = s.Remove(0, RegexTimeCodes.Match(s).Length - 1).Trim();
|
|
if (!text.Contains(Environment.NewLine))
|
|
text = text.Replace("//", Environment.NewLine);
|
|
if (text.Contains("@Italic@"))
|
|
{
|
|
bool italicOn = false;
|
|
while (text.Contains("@Italic@"))
|
|
{
|
|
var index = text.IndexOf("@Italic@", StringComparison.Ordinal);
|
|
string italicTag = "<i>";
|
|
if (italicOn)
|
|
italicTag = "</i>";
|
|
text = text.Remove(index, "@Italic@".Length).Insert(index, italicTag);
|
|
italicOn = !italicOn;
|
|
}
|
|
text = HtmlUtil.FixInvalidItalicTags(text);
|
|
}
|
|
p = new Paragraph(DecodeTimeCodeFramesFourParts(startParts), DecodeTimeCodeFramesFourParts(endParts), text);
|
|
subtitle.Paragraphs.Add(p);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
_errorCount++;
|
|
System.Diagnostics.Debug.WriteLine(exception.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else if (string.IsNullOrWhiteSpace(line))
|
|
{
|
|
// skip empty lines
|
|
}
|
|
else if (p != null)
|
|
{
|
|
_errorCount++;
|
|
}
|
|
}
|
|
|
|
subtitle.Renumber();
|
|
}
|
|
|
|
}
|
|
}
|