SubtitleEdit/libse/SubtitleFormats/UnknownSubtitle29.cs

84 lines
2.8 KiB
C#
Raw Normal View History

2016-02-06 07:52:53 +01:00
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class UnknownSubtitle29 : SubtitleFormat
{
//00:00:30:21 00:00:35:22
//Et c'est là que nous devions
//passer la nuit.
//00:00:38:04 00:00:40:18
//Un nouveau martyre a commencé.
private static readonly Regex RegexTimeCode = new Regex(@"^\d\d:\d\d:\d\d:\d\d\t\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 29";
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();
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($"{MakeTimeCode(p.StartTime)}\t{MakeTimeCode(p.EndTime)}\r\n{text}\r\n");
2016-02-06 07:52:53 +01:00
}
return sb.ToString();
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
_errorCount = 0;
Paragraph p = null;
var sb = new StringBuilder();
char[] splitChars = { ':', ';', ',' };
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('\t');
if (arr.Length == 2)
p = new Paragraph(DecodeTimeCodeFrames(arr[0], splitChars), DecodeTimeCodeFrames(arr[1], splitChars), string.Empty);
}
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();
}
}
}