SubtitleEdit/libse/SubtitleFormats/UnknownSubtitle23.cs

124 lines
3.8 KiB
C#
Raw Normal View History

2016-02-08 21:11:03 +01:00
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class UnknownSubtitle23 : SubtitleFormat
{
//1: 01:00:19.04 01:00:21.05
private static readonly Regex RegexTimeCode1 = new Regex(@"^\s*\d+:\s+\d\d:\d\d:\d\d.\d\d\s+\d\d:\d\d:\d\d.\d\d\s*$", RegexOptions.Compiled);
2017-08-03 12:43:52 +02:00
public override string Extension => ".rtf";
2016-02-08 21:11:03 +01:00
2017-08-03 12:43:52 +02:00
public override string Name => "Unknown 23";
2016-02-08 21:11:03 +01:00
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder();
sb.AppendLine(title);
sb.AppendLine(@"1ab
23/03/2012
03/05/2012
**:**:**.**
01:00:00.00
**:**:**.**
**:**:**.**
01:01:01.12
01:02:30.00
01:02:54.01
**:**:**.**
**:**:**.**
01:19:33.08
");
int count = 1;
foreach (Paragraph p in subtitle.Paragraphs)
{
2017-08-03 12:43:52 +02:00
sb.AppendLine($"{count.ToString(CultureInfo.InvariantCulture).PadLeft(9, ' ')}: {MakeTimeCode(p.StartTime)} {MakeTimeCode(p.EndTime)}\r\n{p.Text}");
2016-02-08 21:11:03 +01:00
count++;
}
return sb.ToString().Trim().ToRtf();
}
private static string MakeTimeCode(TimeCode timeCode)
{
return timeCode.ToHHMMSSPeriodFF();
}
private static TimeCode DecodeTimeCode(string timeCode)
{
string[] arr = timeCode.Split(new[] { ':', ';', ',', '.' }, StringSplitOptions.RemoveEmptyEntries);
return new TimeCode(int.Parse(arr[0]), int.Parse(arr[1]), int.Parse(arr[2]), FramesToMillisecondsMax999(int.Parse(arr[3])));
}
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-08 21:11:03 +01:00
sb.AppendLine(line);
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +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-08 21:11:03 +01:00
return;
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
lines = rtf.FromRtf().SplitToLines();
2016-02-08 21:11:03 +01:00
_errorCount = 0;
Paragraph p = null;
2017-01-20 22:25:06 +01:00
sb.Clear();
2016-02-08 21:11:03 +01:00
foreach (string line in lines)
{
string s = line.TrimEnd();
if (RegexTimeCode1.IsMatch(s))
{
try
{
if (p != null)
{
p.Text = sb.ToString().Trim();
subtitle.Paragraphs.Add(p);
}
sb.Clear();
2016-02-08 21:11:03 +01:00
string[] arr = s.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (arr.Length == 3)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
p = new Paragraph(DecodeTimeCode(arr[1]), DecodeTimeCode(arr[2]), string.Empty);
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
}
catch
{
_errorCount++;
p = null;
}
}
else if (p != null && s.Length > 0)
{
sb.AppendLine(s.Trim());
}
else if (!string.IsNullOrWhiteSpace(s))
{
_errorCount++;
}
}
if (p != null)
{
p.Text = sb.ToString().Trim();
subtitle.Paragraphs.Add(p);
}
subtitle.Renumber();
}
}
}