Added subtitle format PE2

git-svn-id: https://subtitleedit.googlecode.com/svn/trunk@962 99eadd0c-20b8-1223-b5c4-2a2b2df33de2
This commit is contained in:
niksedk 2012-02-03 20:23:26 +00:00
parent d2be1549b3
commit bb2b366872
3 changed files with 159 additions and 0 deletions

View File

@ -0,0 +1,157 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
{
public class PE2 : SubtitleFormat
{
enum ExpectingLine
{
TimeStart,
Text,
TimeEndOrText,
}
public override string Extension
{
get { return ".txt"; }
}
public override string Name
{
get { return "PE2"; }
}
public override bool HasLineNumber
{
get { return false; }
}
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)
{
//#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 paragraphWriteFormat = "{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(paragraphWriteFormat, EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), text, Environment.NewLine));
}
return sb.ToString().Trim();
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
var regexTimeCode = new Regex(@"^\d\d:\d\d:\d\d:\d\d ", RegexOptions.Compiled);
var regexTimeCodeEnd = new Regex(@"^\d\d:\d\d:\d\d:\d\d$", RegexOptions.Compiled);
var paragraph = new Paragraph();
ExpectingLine expecting = ExpectingLine.TimeStart;
_errorCount = 0;
subtitle.Paragraphs.Clear();
foreach (string line in lines)
{
if (regexTimeCode.IsMatch(line))
{
string[] parts = line.Substring(0, 11).Split(":".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 4)
{
try
{
var tc = DecodeTimeCode(parts);
if (expecting == ExpectingLine.TimeStart)
{
paragraph = new Paragraph();
paragraph.StartTime = tc;
expecting = ExpectingLine.Text;
if (line.Length > 12)
paragraph.Text = line.Substring(12).Trim().Replace("//", Environment.NewLine);
}
}
catch
{
_errorCount++;
expecting = ExpectingLine.TimeStart;
}
}
}
else if (regexTimeCodeEnd.IsMatch(line))
{
string[] parts = line.Substring(0, 11).Split(":".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 4)
{
var tc = DecodeTimeCode(parts);
paragraph.EndTime = tc;
subtitle.Paragraphs.Add(paragraph);
paragraph = new Paragraph();
expecting = ExpectingLine.TimeStart;
}
}
else
{
if (expecting == ExpectingLine.Text)
{
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;
}
}
}
}
}
subtitle.Renumber(1);
}
private string EncodeTimeCode(TimeCode time)
{
return string.Format("{0:00}:{1:00}:{2:00}:{3:00}", time.Hours, time.Minutes, time.Seconds, MillisecondsToFrames(time.Milliseconds));
}
private TimeCode DecodeTimeCode(string[] parts)
{
string hour = parts[0];
string minutes = parts[1];
string seconds = parts[2];
string frames = parts[3];
int milliseconds = (int)((1000.0 / Configuration.Settings.General.CurrentFrameRate) * int.Parse(frames));
if (milliseconds > 999)
milliseconds = 999;
return new TimeCode(int.Parse(hour), int.Parse(minutes), int.Parse(seconds), milliseconds);
}
}
}

View File

@ -44,6 +44,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
new MicroDvd(),
new MPlayer2(),
new OpenDvt(),
new PE2(),
new PinnacleImpression(),
new PListCaption(),
new QuickTimeText(),

View File

@ -599,6 +599,7 @@
<Compile Include="Logic\SubtitleFormats\GpacTtxt.cs" />
<Compile Include="Logic\SubtitleFormats\Json.cs" />
<Compile Include="Logic\SubtitleFormats\Pac.cs" />
<Compile Include="Logic\SubtitleFormats\PE2.cs" />
<Compile Include="Logic\SubtitleFormats\PinnacleImpression.cs" />
<Compile Include="Logic\SubtitleFormats\PListCaption.cs" />
<Compile Include="Logic\SubtitleFormats\QuickTimeText.cs" />