SubtitleEdit/libse/SubtitleFormats/UnknownSubtitle26.cs

123 lines
3.6 KiB
C#
Raw Normal View History

2016-02-06 07:52:53 +01:00
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class UnknownSubtitle26 : SubtitleFormat
{
//L'Enfant d'en haut
//1ab
//10/01/2012
//10/01/2012
//01:00:22.09
//01:00:30.09
//**:**:**.**
//**:**:**.**
//01:00:51.09
//01:00:55.22
//01:01:10.09
//**:**:**.**
//**:**:**.**
//01:13:23.09
//
//1: 01:01:28.22 01:01:33.09*
// SISTER
//
//2:
//01:02:58.15 01:03:00.00
//I'm pooping, sir.
//Were they easy to get?
private static readonly Regex RegexTimeCode = new Regex(@"^\d+: \d\d:\d\d:\d\d.\d\d \d\d:\d\d:\d\d.\d\d[\*]*$", RegexOptions.Compiled);
2017-08-03 12:43:52 +02:00
public override string Extension => ".txt";
2016-02-06 07:52:53 +01:00
2017-08-03 12:43:52 +02:00
public override string Name => "Unknown 26";
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(title);
sb.AppendLine(@"1ab
10/01/2012
10/01/2012
01:00:22.09
01:00:30.09
**:**:**.**
**:**:**.**
01:00:51.09
01:00:55.22
01:01:10.09
**:**:**.**
**:**:**.**
01:13:23.09");
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}: {MakeTimeCode(p.StartTime)} {MakeTimeCode(p.EndTime)}\r\n{text}\r\n");
2016-02-06 07:52:53 +01:00
count++;
}
return sb.ToString();
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
_errorCount = 0;
Paragraph p = null;
var sb = new StringBuilder();
foreach (string line in lines)
{
string s = line.TrimEnd();
if (RegexTimeCode.IsMatch(s))
{
try
{
if (p != null)
{
p.Text = sb.ToString().Trim();
subtitle.Paragraphs.Add(p);
}
sb.Clear();
2016-02-06 07:52:53 +01:00
string[] arr = s.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
char[] splitChars = { ':', ';', ',', '.' };
if (arr.Length == 3)
2019-01-19 14:40:37 +01:00
{
2016-02-06 07:52:53 +01:00
p = new Paragraph(DecodeTimeCodeFrames(arr[1].TrimEnd('*'), splitChars), DecodeTimeCodeFrames(arr[2].TrimEnd('*'), splitChars), string.Empty);
2019-01-19 14:40:37 +01:00
}
2016-02-06 07:52:53 +01:00
}
catch
{
_errorCount++;
p = null;
}
}
else if (!string.IsNullOrWhiteSpace(s))
{
sb.AppendLine(s);
}
}
if (p != null)
{
p.Text = sb.ToString().Trim();
subtitle.Paragraphs.Add(p);
}
subtitle.Renumber();
}
}
}