using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; namespace Nikse.SubtitleEdit.Core.SubtitleFormats { public class UnknownSubtitle56 : SubtitleFormat { //0001 01:00:37:22 01:00:39:11 private static readonly Regex RegexTimeCodes1 = new Regex(@"^\d\d\d\d\t\d\d:\d\d:\d\d:\d\d\t\d\d:\d\d:\d\d:\d\d$", RegexOptions.Compiled); public override string Extension { get { return ".txt"; } } public override string Name { get { return "Unknown 56"; } } 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); LoadSubtitle(subtitle, lines, fileName); return subtitle.Paragraphs.Count > _errorCount; } public override string ToText(Subtitle subtitle, string title) { var sb = new StringBuilder(); const string format = "{0:0000}\t{1}\t{2}"; int count = 1; foreach (Paragraph p in subtitle.Paragraphs) { sb.AppendLine(string.Format(format, 1, EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime))); sb.AppendLine(HtmlUtil.RemoveHtmlTags(p.Text)); sb.AppendLine(); count++; } return sb.ToString(); } private static string EncodeTimeCode(TimeCode time) { 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) { _errorCount = 0; bool expectStartTime = true; var p = new Paragraph(); subtitle.Paragraphs.Clear(); foreach (string line in lines) { string s = line.Trim(); var match = RegexTimeCodes1.Match(s); if (match.Success) { string[] parts = s.Split('\t'); if (parts.Length == 3) { try { if (!string.IsNullOrEmpty(p.Text)) { subtitle.Paragraphs.Add(p); p = new Paragraph(); } p.StartTime = DecodeTimeCode(parts[1]); p.EndTime = DecodeTimeCode(parts[2]); expectStartTime = false; } catch (Exception exception) { _errorCount++; System.Diagnostics.Debug.WriteLine(exception.Message); } } } else if (string.IsNullOrWhiteSpace(line)) { if (p.StartTime.TotalMilliseconds == 0 && p.EndTime.TotalMilliseconds == 0) _errorCount++; else subtitle.Paragraphs.Add(p); p = new Paragraph(); } else if (!expectStartTime) { p.Text = (p.Text + Environment.NewLine + line).Trim(); if (p.Text.Length > 500) { _errorCount += 10; return; } while (p.Text.Contains(Environment.NewLine + " ")) p.Text = p.Text.Replace(Environment.NewLine + " ", Environment.NewLine); } } if (p.EndTime.TotalMilliseconds > 0) subtitle.Paragraphs.Add(p); foreach (Paragraph temp in subtitle.Paragraphs) temp.Text = temp.Text.Replace("<", "@ITALIC_START").Replace(">", "").Replace("@ITALIC_START", ""); subtitle.RemoveEmptyLines(); subtitle.Renumber(); } private static TimeCode DecodeTimeCode(string part) { string[] parts = part.Split(new[] { '.', ':' }, StringSplitOptions.RemoveEmptyEntries); //00:00:07:12 string hour = parts[0]; string minutes = parts[1]; string seconds = parts[2]; string frames = parts[3]; return new TimeCode(int.Parse(hour), int.Parse(minutes), int.Parse(seconds), FramesToMillisecondsMax999(int.Parse(frames))); } } }