SubtitleEdit/libse/SubtitleFormats/UnknownSubtitle21.cs

127 lines
4.6 KiB
C#
Raw Normal View History

2016-02-06 07:52:53 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class UnknownSubtitle21 : SubtitleFormat
{
private static readonly Regex RegexTimeCode1 = new Regex(@"^\s*\d+\t\d\d:\d\d:\d\d;\d\d", RegexOptions.Compiled);
2017-08-03 12:43:52 +02:00
public override string Extension => ".rtf";
2016-02-06 07:52:53 +01:00
2017-08-03 12:43:52 +02:00
public override string Name => "Unknown 21";
2016-02-06 07:52:53 +01:00
private static string MakeTimeCode(TimeCode tc)
{
2017-08-03 12:43:52 +02:00
return $"{tc.Hours:00}:{tc.Minutes:00}:{tc.Seconds:00};{MillisecondsToFramesMaxFrameRate(tc.Milliseconds):00}";
2016-02-06 07:52:53 +01:00
}
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder();
sb.AppendLine("#\tAppearance\tCaption\t");
sb.AppendLine();
int count = 1;
for (int i = 0; i < subtitle.Paragraphs.Count; i++)
{
Paragraph p = subtitle.Paragraphs[i];
string text = HtmlUtil.RemoveHtmlTags(p.Text);
2017-08-03 12:43:52 +02:00
sb.AppendLine($"{count.ToString().PadLeft(5, ' ')}\t{MakeTimeCode(p.StartTime)}\t{text}\t");
2016-02-06 07:52:53 +01:00
sb.AppendLine("\t\t\t\t");
Paragraph next = subtitle.GetParagraphOrDefault(i + 1);
if (next == null || Math.Abs(p.EndTime.TotalMilliseconds - next.StartTime.TotalMilliseconds) > 50)
{
count++;
2017-08-03 12:43:52 +02:00
sb.AppendLine($"{count.ToString().PadLeft(5, ' ')}\t{MakeTimeCode(p.EndTime)}");
2016-02-06 07:52:53 +01:00
}
count++;
}
return sb.ToString().ToRtf();
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
_errorCount = 0;
var sb = new StringBuilder();
foreach (string line in lines)
2019-01-19 14:40:37 +01:00
{
2016-02-06 07:52:53 +01:00
sb.AppendLine(line);
2019-01-19 14:40:37 +01:00
}
2016-02-06 07:52:53 +01:00
string rtf = sb.ToString().Trim();
2016-08-10 04:40:15 +02:00
if (!rtf.StartsWith("{\\rtf", StringComparison.Ordinal))
2019-01-19 14:40:37 +01:00
{
2016-02-06 07:52:53 +01:00
return;
2019-01-19 14:40:37 +01:00
}
2016-02-06 07:52:53 +01:00
lines = rtf.FromRtf().SplitToLines();
2016-02-06 07:52:53 +01:00
_errorCount = 0;
Paragraph p = null;
char[] splitChar = { ':', ';', ',' };
foreach (string line in lines)
{
string s = line.TrimEnd();
if (RegexTimeCode1.IsMatch(s))
{
try
{
if (p != null)
2019-01-19 14:40:37 +01:00
{
2016-02-06 07:52:53 +01:00
subtitle.Paragraphs.Add(p);
2019-01-19 14:40:37 +01:00
}
2016-02-06 07:52:53 +01:00
string[] arr = s.Split('\t');
if (arr.Length > 2)
2019-01-19 14:40:37 +01:00
{
p = new Paragraph(DecodeTimeCodeFrames(arr[1], splitChar), new TimeCode(), arr[2].Trim());
2019-01-19 14:40:37 +01:00
}
2016-02-06 07:52:53 +01:00
else
2019-01-19 14:40:37 +01:00
{
p = new Paragraph(DecodeTimeCodeFrames(arr[1], splitChar), new TimeCode(), string.Empty);
2019-01-19 14:40:37 +01:00
}
2016-02-06 07:52:53 +01:00
}
catch
{
_errorCount++;
p = null;
}
}
else if (s.StartsWith("\t\t", StringComparison.Ordinal))
{
if (p != null)
2019-01-19 14:40:37 +01:00
{
2016-02-06 07:52:53 +01:00
p.Text = p.Text + Environment.NewLine + s.Trim();
2019-01-19 14:40:37 +01:00
}
2016-02-06 07:52:53 +01:00
}
else if (!string.IsNullOrWhiteSpace(s))
{
_errorCount++;
}
}
if (p != null)
2019-01-19 14:40:37 +01:00
{
2016-02-06 07:52:53 +01:00
subtitle.Paragraphs.Add(p);
2019-01-19 14:40:37 +01:00
}
2016-02-06 07:52:53 +01:00
for (int j = 0; j < subtitle.Paragraphs.Count - 1; j++)
{
p = subtitle.Paragraphs[j];
Paragraph next = subtitle.Paragraphs[j + 1];
p.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - Configuration.Settings.General.MinimumMillisecondsBetweenLines;
}
if (subtitle.Paragraphs.Count > 0)
{
p = subtitle.Paragraphs[subtitle.Paragraphs.Count - 1];
p.EndTime.TotalMilliseconds = p.StartTime.TotalMilliseconds + Utilities.GetOptimalDisplayMilliseconds(p.Text);
}
subtitle.RemoveEmptyLines();
subtitle.Renumber();
}
}
}