SubtitleEdit/libse/SubtitleFormats/PE2.cs

157 lines
5.9 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
{
2017-07-11 15:36:04 +02:00
public class Pe2 : SubtitleFormat
2016-02-06 07:52:53 +01:00
{
private static readonly Regex RegexTimeCode = new Regex(@"^\d\d:\d\d:\d\d:\d\d ", RegexOptions.Compiled);
private static readonly Regex RegexTimeCodeEnd = new Regex(@"^\d\d:\d\d:\d\d:\d\d$", RegexOptions.Compiled);
private enum ExpectingLine
{
TimeStart,
Text,
TimeEndOrText,
}
2017-07-11 15:36:04 +02:00
public override string Extension => ".txt";
2016-02-06 07:52:53 +01:00
2017-07-11 15:36:04 +02:00
public override string Name => "PE2";
2016-02-06 07:52:53 +01:00
public override bool IsMine(List<string> lines, string fileName)
{
var sb = new StringBuilder();
foreach (string line in lines)
2019-01-19 14:40:37 +01:00
{
2016-02-06 07:52:53 +01:00
sb.AppendLine(line);
2019-01-19 14:40:37 +01:00
}
2016-02-06 07:52:53 +01:00
string s = sb.ToString();
2017-07-11 15:36:04 +02:00
if (!s.Contains("#PE2"))
2019-01-19 14:40:37 +01:00
{
2017-08-03 12:43:52 +02:00
return false;
2019-01-19 14:40:37 +01:00
}
2016-02-06 07:52:53 +01:00
2017-08-03 12:43:52 +02:00
return base.IsMine(lines, fileName);
2016-02-06 07:52:53 +01:00
}
public override string ToText(Subtitle subtitle, string title)
{
//#PE2 Format file
//10:00:05:16 You will get a loan of//Rs 1.5 million in 15 minutes.
//10:00:08:19
//10:00:09:01 What have you brought//as the guarantee?
//10:00:12:01
//10:00:12:11 What?//I didn't get you.
//10:00:14:11
//10:00:14:15 We will sanction your loan.
//10:00:16:00
const string writeFormat = "{0} {2}{3}{1}";
var sb = new StringBuilder();
sb.AppendLine("#PE2 Format file");
foreach (Paragraph p in subtitle.Paragraphs)
{
string text = p.Text.Replace(Environment.NewLine, "//");
sb.AppendLine(string.Format(writeFormat, EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), text, Environment.NewLine));
}
return sb.ToString().Trim();
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
2017-07-11 15:36:04 +02:00
Paragraph paragraph = null;
var expecting = ExpectingLine.TimeStart;
2016-02-06 07:52:53 +01:00
_errorCount = 0;
subtitle.Paragraphs.Clear();
foreach (string line in lines)
{
if (RegexTimeCode.IsMatch(line))
{
string[] parts = line.Substring(0, 11).Split(SplitCharColon, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 4)
{
try
{
var tc = DecodeTimeCodeFramesFourParts(parts);
2017-07-11 15:36:04 +02:00
if (paragraph != null)
2016-02-06 07:52:53 +01:00
{
2017-07-11 15:36:04 +02:00
if (paragraph.EndTime.TotalMilliseconds < 0.001)
2019-01-19 14:40:37 +01:00
{
2017-07-11 15:36:04 +02:00
paragraph.EndTime.TotalMilliseconds = tc.TotalMilliseconds - Configuration.Settings.General.MinimumMillisecondsBetweenLines;
2019-01-19 14:40:37 +01:00
}
2017-07-11 15:36:04 +02:00
subtitle.Paragraphs.Add(paragraph);
2016-02-06 07:52:53 +01:00
}
2017-07-11 15:36:04 +02:00
paragraph = new Paragraph { StartTime = tc };
expecting = ExpectingLine.Text;
if (line.Length > 12)
2019-01-19 14:40:37 +01:00
{
2017-07-11 15:36:04 +02:00
paragraph.Text = line.Substring(12).Trim().Replace("//", Environment.NewLine);
2019-01-19 14:40:37 +01:00
}
2016-02-06 07:52:53 +01:00
}
catch
{
_errorCount++;
}
}
}
else if (RegexTimeCodeEnd.IsMatch(line))
{
string[] parts = line.Substring(0, 11).Split(SplitCharColon, StringSplitOptions.RemoveEmptyEntries);
2017-07-11 15:36:04 +02:00
if (parts.Length == 4 && paragraph != null)
2016-02-06 07:52:53 +01:00
{
var tc = DecodeTimeCodeFramesFourParts(parts);
2016-02-06 07:52:53 +01:00
paragraph.EndTime = tc;
subtitle.Paragraphs.Add(paragraph);
if (paragraph.StartTime.TotalMilliseconds < 0.001)
2019-01-19 14:40:37 +01:00
{
2016-02-06 07:52:53 +01:00
_errorCount++;
2019-01-19 14:40:37 +01:00
}
2017-07-11 15:36:04 +02:00
paragraph = null;
2016-02-06 07:52:53 +01:00
expecting = ExpectingLine.TimeStart;
}
}
else
{
2017-07-11 15:36:04 +02:00
if (expecting == ExpectingLine.Text && paragraph != null)
2016-02-06 07:52:53 +01:00
{
if (line.Length > 0)
{
string text = line.Replace("//", Environment.NewLine);
paragraph.Text += Environment.NewLine + text;
expecting = ExpectingLine.TimeEndOrText;
if (paragraph.Text.Length > 2000)
{
_errorCount += 100;
return;
}
}
}
else if (!string.IsNullOrWhiteSpace(line) && line != "#PE2 Format file")
{
_errorCount++;
}
}
}
2017-07-11 15:36:04 +02:00
if (!string.IsNullOrEmpty(paragraph?.Text) && paragraph.EndTime.TotalMilliseconds < 0.001)
{
paragraph.EndTime.TotalMilliseconds = 3000;
}
2016-02-06 07:52:53 +01:00
subtitle.Renumber();
}
private static string EncodeTimeCode(TimeCode time)
{
return time.ToHHMMSSFF();
}
}
}