SubtitleEdit/libse/SubtitleFormats/UnknownSubtitle46.cs

89 lines
3.4 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 UnknownSubtitle46 : SubtitleFormat
{
//7:00:01:27AM
2016-06-07 06:31:03 +02:00
private static readonly Regex RegexTimeCodesAm = new Regex(@"^\d\:\d\d\:\d\d\:\d\dAM", RegexOptions.Compiled);
private static readonly Regex RegexTimeCodesPm = new Regex(@"^\d\:\d\d\:\d\d\:\d\dPM", RegexOptions.Compiled);
2016-02-06 07:52:53 +01:00
2017-08-03 12:43:52 +02:00
public override string Extension => ".pst";
2016-02-06 07:52:53 +01:00
2017-08-03 12:43:52 +02:00
public override string Name => "Unknown 46";
2016-02-06 07:52:53 +01:00
public override string ToText(Subtitle subtitle, string title)
{
//OFF THE RECORD STARTS RIGHT NOW. 7:00:01:27AM
//HERE IS THE RUNDOWN. 7:00:05:03AM
var sb = new StringBuilder();
const string format = "{0}{1}";
foreach (Paragraph p in subtitle.Paragraphs)
{
sb.AppendLine(string.Format(format, p.Text.Replace(Environment.NewLine, " ").PadRight(35), EncodeTimeCode(p.StartTime)));
}
return sb.ToString().Trim();
}
private static string EncodeTimeCode(TimeCode timeCode)
{
2017-08-03 12:43:52 +02:00
return $"{timeCode.Hours}:{timeCode.Minutes:00}:{timeCode.Seconds:00}:{MillisecondsToFramesMaxFrameRate(timeCode.Milliseconds):00}AM";
2016-02-06 07:52:53 +01:00
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
_errorCount = 0;
Paragraph p = null;
foreach (string line in lines)
{
string s = line.Trim();
string[] arr = line.Split();
var timeCode = arr[arr.Length - 1];
2016-06-07 06:31:03 +02:00
if (RegexTimeCodesAm.Match(timeCode).Success || RegexTimeCodesPm.Match(timeCode).Success)
2016-02-06 07:52:53 +01:00
{
try
{
arr = timeCode.Substring(0, 10).Split(':');
if (arr.Length == 4)
{
p = new Paragraph();
p.StartTime = DecodeTimeCodeFramesFourParts(arr);
2016-02-06 07:52:53 +01:00
p.Text = s.Substring(0, s.IndexOf(timeCode, StringComparison.Ordinal)).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(p.Text);
}
index++;
}
subtitle.RemoveEmptyLines();
subtitle.Renumber();
}
}
}