SubtitleEdit/libse/SubtitleFormats/UnknownSubtitle40.cs

87 lines
3.0 KiB
C#
Raw Normal View History

2016-02-08 21:11:03 +01:00
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class UnknownSubtitle40 : SubtitleFormat
{
// 0:01 0:03
private static readonly Regex RegexTimeCodes = new Regex(@"^\d+:\d\d \d+:\d\d$", RegexOptions.Compiled);
2017-08-03 12:43:52 +02:00
public override string Extension => ".txt";
2016-02-08 21:11:03 +01:00
2017-08-03 12:43:52 +02:00
public override string Name => "Unknown 40";
2016-02-08 21:11:03 +01:00
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder();
foreach (Paragraph p in subtitle.Paragraphs)
{
2017-08-03 12:43:52 +02:00
sb.AppendLine($"{EncodeTimeCode(p.StartTime)} {EncodeTimeCode(p.EndTime)}{Environment.NewLine}{HtmlUtil.RemoveHtmlTags(p.Text)}");
2016-02-08 21:11:03 +01:00
}
return sb.ToString();
}
private static string EncodeTimeCode(TimeCode time)
{
2017-08-03 12:43:52 +02:00
return $"{time.Hours * 60 + time.Minutes}:{time.Seconds:00}";
2016-02-08 21:11:03 +01:00
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
Paragraph p = null;
subtitle.Paragraphs.Clear();
_errorCount = 0;
foreach (string line in lines)
{
if (RegexTimeCodes.IsMatch(line))
{
string[] temp = line.Split('');
string start = temp[0].Trim();
string end = temp[1].Trim();
string[] startParts = start.Split(SplitCharColon, StringSplitOptions.RemoveEmptyEntries);
string[] endParts = end.Split(SplitCharColon, StringSplitOptions.RemoveEmptyEntries);
if (startParts.Length == 2 && endParts.Length == 2)
{
p = new Paragraph(DecodeTimeCode(startParts), DecodeTimeCode(endParts), string.Empty);
subtitle.Paragraphs.Add(p);
}
}
else if (string.IsNullOrWhiteSpace(line))
{
// skip these lines
}
else if (p != null)
{
if (string.IsNullOrEmpty(p.Text))
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
p.Text = line;
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
else
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
p.Text = p.Text.TrimEnd() + Environment.NewLine + line;
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
if (p.Text.Length > 500)
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
}
}
subtitle.Renumber();
}
private static TimeCode DecodeTimeCode(string[] parts)
{
//00:00:07:12
var minutes = int.Parse(parts[0]);
var seconds = int.Parse(parts[1]);
return new TimeCode(0, minutes, seconds, 0);
}
}
}