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 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("", "@Italic@"); text = text.Replace("", "@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 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(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries); string[] endParts = end.Split(new[] { ':' }, 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 = ""; if (italicOn) italicTag = ""; text = text.Remove(index, "@Italic@".Length).Insert(index, italicTag); italicOn = !italicOn; } text = HtmlUtil.FixInvalidItalicTags(text); } p = new Paragraph(DecodeTimeCode(startParts), DecodeTimeCode(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(); } private static TimeCode DecodeTimeCode(string[] parts) { //00:00:07:12 string hour = parts[0]; string minutes = parts[1]; string seconds = parts[2]; string frames = parts[3]; TimeCode tc = new TimeCode(int.Parse(hour), int.Parse(minutes), int.Parse(seconds), FramesToMillisecondsMax999(int.Parse(frames))); return tc; } } }