SubtitleEdit/libse/SubtitleFormats/UnknownSubtitle27.cs

131 lines
4.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class UnknownSubtitle27 : SubtitleFormat
{
private static Regex regexTimeCodes = new Regex(@"^\d\d:\d\d:\d\d: ", RegexOptions.Compiled);
public override string Extension
{
get { return ".txt"; }
}
public override string Name
{
get { return "Unknown 27"; }
}
public override bool IsTimeBased
{
get { return true; }
}
public override bool IsMine(List<string> lines, string fileName)
{
var subtitle = new Subtitle();
var sb = new StringBuilder();
foreach (string line in lines)
sb.AppendLine(line);
LoadSubtitle(subtitle, lines, fileName);
return subtitle.Paragraphs.Count > _errorCount;
}
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder();
int index = 0;
foreach (Paragraph p in subtitle.Paragraphs)
{
index++;
//00:18:02: (斉藤)失礼な大人って! (悠子)何言ってんのあんた?
string text = HtmlUtil.RemoveHtmlTags(p.Text);
text = text.Replace(Environment.NewLine, " ");
sb.AppendLine(string.Format("{0} {1}", EncodeTimeCode(p.StartTime), text));
sb.AppendLine();
}
return sb.ToString();
}
private static string EncodeTimeCode(TimeCode time)
{
//00:03:15:22 (last is frame)
int sec = time.Seconds;
if (time.Milliseconds >= 500)
sec++;
return string.Format("{0:00}:{1:00}:{2:00}:", time.Hours, time.Minutes, sec);
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
// 00:18:02: (斉藤)失礼な大人って! (悠子)何言ってんのあんた?
Paragraph p = null;
subtitle.Paragraphs.Clear();
_errorCount = 0;
foreach (string line in lines)
{
string s = line.Trim();
if (regexTimeCodes.IsMatch(s))
{
var temp = s.Substring(0, 8);
string[] startParts = temp.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
if (startParts.Length == 3)
{
try
{
string text = s.Remove(0, 10).Trim();
text = text.Replace(" ", Environment.NewLine);
p = new Paragraph(DecodeTimeCode(startParts), new TimeCode(0, 0, 0, 0), text);
subtitle.Paragraphs.Add(p);
}
catch (Exception exception)
{
_errorCount++;
System.Diagnostics.Debug.WriteLine(exception.Message);
}
}
}
else if (string.IsNullOrWhiteSpace(line))
{
// skip empty lines
}
else if (p != null)
{
_errorCount++;
}
}
for (int i = 0; i < subtitle.Paragraphs.Count; i++)
{
Paragraph current = subtitle.Paragraphs[i];
Paragraph next = subtitle.GetParagraphOrDefault(i + 1);
if (next != null)
current.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - Configuration.Settings.General.MinimumMillisecondsBetweenLines;
else
current.EndTime.TotalMilliseconds = current.StartTime.TotalMilliseconds + Utilities.GetOptimalDisplayMilliseconds(current.Text);
if (current.Duration.TotalMilliseconds > Configuration.Settings.General.SubtitleMaximumDisplayMilliseconds)
current.EndTime.TotalMilliseconds = current.StartTime.TotalMilliseconds + Configuration.Settings.General.SubtitleMaximumDisplayMilliseconds;
}
subtitle.Renumber();
}
private static TimeCode DecodeTimeCode(string[] parts)
{
//00:00:07:12
string hour = parts[0];
string minutes = parts[1];
string seconds = parts[2];
return new TimeCode(int.Parse(hour), int.Parse(minutes), int.Parse(seconds), 0);
}
}
}