From bb2b366872d0fdaf94a9561cd7b587f1eb6deb28 Mon Sep 17 00:00:00 2001 From: niksedk Date: Fri, 3 Feb 2012 20:23:26 +0000 Subject: [PATCH] Added subtitle format PE2 git-svn-id: https://subtitleedit.googlecode.com/svn/trunk@962 99eadd0c-20b8-1223-b5c4-2a2b2df33de2 --- src/Logic/SubtitleFormats/PE2.cs | 157 ++++++++++++++++++++ src/Logic/SubtitleFormats/SubtitleFormat.cs | 1 + src/SubtitleEdit.csproj | 1 + 3 files changed, 159 insertions(+) create mode 100644 src/Logic/SubtitleFormats/PE2.cs diff --git a/src/Logic/SubtitleFormats/PE2.cs b/src/Logic/SubtitleFormats/PE2.cs new file mode 100644 index 000000000..fc5def261 --- /dev/null +++ b/src/Logic/SubtitleFormats/PE2.cs @@ -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 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 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); + } + + } +} \ No newline at end of file diff --git a/src/Logic/SubtitleFormats/SubtitleFormat.cs b/src/Logic/SubtitleFormats/SubtitleFormat.cs index afe92ada3..7c24ca13a 100644 --- a/src/Logic/SubtitleFormats/SubtitleFormat.cs +++ b/src/Logic/SubtitleFormats/SubtitleFormat.cs @@ -44,6 +44,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats new MicroDvd(), new MPlayer2(), new OpenDvt(), + new PE2(), new PinnacleImpression(), new PListCaption(), new QuickTimeText(), diff --git a/src/SubtitleEdit.csproj b/src/SubtitleEdit.csproj index 7a033f54f..67a1e3f90 100644 --- a/src/SubtitleEdit.csproj +++ b/src/SubtitleEdit.csproj @@ -599,6 +599,7 @@ +