SubtitleEdit/libse/SubtitleFormats/UnknownSubtitle47.cs

103 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class UnknownSubtitle47 : SubtitleFormat
{
//7:00:01:27AM
private static Regex regexTimeCodes = new Regex(@"^\d\:\d\d\:\d\d\:\d\d\t", RegexOptions.Compiled);
public override string Extension
{
get { return ".txt"; }
}
public override string Name
{
get { return "Unknown 47"; }
}
public override bool IsTimeBased
{
get { return true; }
}
public override bool IsMine(List<string> lines, string fileName)
{
Subtitle subtitle = new Subtitle();
LoadSubtitle(subtitle, lines, fileName);
return subtitle.Paragraphs.Count > _errorCount;
}
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder();
foreach (Paragraph p in subtitle.Paragraphs)
{
sb.AppendLine(string.Format("{0}\t{1}", EncodeTimeCode(p.StartTime), p.Text.Replace(Environment.NewLine, " ")));
}
return sb.ToString().Trim();
}
private static string EncodeTimeCode(TimeCode timeCode)
{
return string.Format("{0}:{1:00}:{2:00}:{3:00}", timeCode.Hours, timeCode.Minutes, timeCode.Seconds, MillisecondsToFramesMaxFrameRate(timeCode.Milliseconds));
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
_errorCount = 0;
foreach (string line in lines)
{
string s = line.Trim();
if (regexTimeCodes.Match(s).Success)
{
try
{
var arr = s.Substring(0, 10).Split(':');
if (arr.Length == 4)
{
int hours = int.Parse(arr[0]);
int minutes = int.Parse(arr[1]);
int seconds = int.Parse(arr[2]);
int frames = int.Parse(arr[3]);
var p = new Paragraph();
p.StartTime = new TimeCode(hours, minutes, seconds, FramesToMillisecondsMax999(frames));
p.Text = s.Remove(0, 10).Trim();
subtitle.Paragraphs.Add(p);
}
}
catch
{
_errorCount++;
}
}
else if (s.Length > 0)
{
_errorCount++;
}
}
int index = 1;
foreach (Paragraph paragraph in subtitle.Paragraphs)
{
Paragraph next = subtitle.GetParagraphOrDefault(index);
if (next != null)
{
paragraph.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - 1;
}
if (paragraph.Duration.TotalMilliseconds > Configuration.Settings.General.SubtitleMaximumDisplayMilliseconds)
{
paragraph.EndTime.TotalMilliseconds = paragraph.StartTime.TotalMilliseconds + Utilities.GetOptimalDisplayMilliseconds(paragraph.Text);
}
index++;
}
subtitle.RemoveEmptyLines();
subtitle.Renumber();
}
}
}