From 23b7f890bc4358b4d1a679d50777c940503686e4 Mon Sep 17 00:00:00 2001 From: niksedk Date: Thu, 13 Jan 2011 08:54:02 +0000 Subject: [PATCH] Added subtitle formats (Scenarist + Adobe Encore with tabs) git-svn-id: https://subtitleedit.googlecode.com/svn/trunk@244 99eadd0c-20b8-1223-b5c4-2a2b2df33de2 --- src/Logic/SubtitleFormats/AdobeEncoreTabs.cs | 119 +++++++++++++++++++ src/Logic/SubtitleFormats/Scenarist.cs | 118 ++++++++++++++++++ src/Logic/SubtitleFormats/SubtitleFormat.cs | 2 + src/SubtitleEdit.csproj | 2 + 4 files changed, 241 insertions(+) create mode 100644 src/Logic/SubtitleFormats/AdobeEncoreTabs.cs create mode 100644 src/Logic/SubtitleFormats/Scenarist.cs diff --git a/src/Logic/SubtitleFormats/AdobeEncoreTabs.cs b/src/Logic/SubtitleFormats/AdobeEncoreTabs.cs new file mode 100644 index 000000000..92e0782e5 --- /dev/null +++ b/src/Logic/SubtitleFormats/AdobeEncoreTabs.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +namespace Nikse.SubtitleEdit.Logic.SubtitleFormats +{ + class AdobeEncoreTab : SubtitleFormat + { + public override string Extension + { + get { return ".txt"; } + } + + public override string Name + { + get { return "Adobe Encore (tabs)"; } + } + + public override bool HasLineNumber + { + get { return false; } + } + + public override bool IsTimeBased + { + get { return true; } + } + + public override bool IsMine(List lines, string fileName) + { + var subtitle = new Subtitle(); + + StringBuilder sb = new StringBuilder(); + foreach (string line in lines) + sb.AppendLine(line); + + LoadSubtitle(subtitle, lines, fileName); + return subtitle.Paragraphs.Count > _errorCount; + } + + public override string ToText(Subtitle subtitle, string title) + { + StringBuilder sb = new StringBuilder(); + int index = 0; + foreach (Paragraph p in subtitle.Paragraphs) + { + //00:00:54:08 00:00:58:06 - Saucers... - ... a dry lake bed. (newline is \r) + sb.AppendLine(string.Format("{0}\t{1}\t{2}", EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), Utilities.RemoveHtmlTags(p.Text).Replace(Environment.NewLine, "\r"))); + index++; + } + return sb.ToString(); + } + + private string EncodeTimeCode(TimeCode time) + { + //00:03:15:22 (last is frame) + int frames = time.Milliseconds / (1000 / 30); + return string.Format("{0:00}:{1:00}:{2:00}:{3:00}", time.Hours, time.Minutes, time.Seconds, frames); + } + + public override void LoadSubtitle(Subtitle subtitle, List lines, string fileName) + { + //00:03:15:22 00:03:23:10 This is line one. + //This is line two. + Paragraph p = null; + subtitle.Paragraphs.Clear(); + var regexTimeCodes = new Regex(@"^\d\d:\d\d:\d\d:\d\d\t\d\d:\d\d:\d\d:\d\d\t", RegexOptions.Compiled); + foreach (string line in lines) + { + if (regexTimeCodes.IsMatch(line)) + { + string temp = line.Substring(0, regexTimeCodes.Match(line).Length); + string start = temp.Substring(0, 11); + string end = temp.Substring(12, 11); + + string[] startParts = start.Split(":".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); + string[] endParts = end.Split(":".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); + if (startParts.Length == 4 && endParts.Length == 4) + { + string text = line.Remove(0, regexTimeCodes.Match(line).Length - 1).Trim(); + if (!text.Contains(Environment.NewLine)) + text = text.Replace("\r", Environment.NewLine); + p = new Paragraph(DecodeTimeCode(startParts), DecodeTimeCode(endParts), text); + subtitle.Paragraphs.Add(p); + } + } + else if (line.Trim().Length == 0) + { + // skip these lines + } + else if (line.Trim().Length > 0 && p != null) + { + _errorCount++; + } + } + + subtitle.Renumber(1); + } + + private TimeCode DecodeTimeCode(string[] parts) + { + //00:00:07:12 + string hour = parts[0]; + string minutes = parts[1]; + string seconds = parts[2]; + string frames = parts[3]; + + int milliseconds = (int)((1000 / 30.0) * int.Parse(frames)); + if (milliseconds > 999) + milliseconds = 999; + + TimeCode tc = new TimeCode(int.Parse(hour), int.Parse(minutes), int.Parse(seconds), milliseconds); + return tc; + } + + } +} + diff --git a/src/Logic/SubtitleFormats/Scenarist.cs b/src/Logic/SubtitleFormats/Scenarist.cs new file mode 100644 index 000000000..bc33a7cd9 --- /dev/null +++ b/src/Logic/SubtitleFormats/Scenarist.cs @@ -0,0 +1,118 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +namespace Nikse.SubtitleEdit.Logic.SubtitleFormats +{ + class Scenarist : SubtitleFormat + { + public override string Extension + { + get { return ".txt"; } + } + + public override string Name + { + get { return "Scenarist"; } + } + + public override bool HasLineNumber + { + get { return true; } + } + + public override bool IsTimeBased + { + get { return true; } + } + + public override bool IsMine(List lines, string fileName) + { + var subtitle = new Subtitle(); + + StringBuilder sb = new StringBuilder(); + foreach (string line in lines) + sb.AppendLine(line); + + LoadSubtitle(subtitle, lines, fileName); + return subtitle.Paragraphs.Count > _errorCount; + } + + public override string ToText(Subtitle subtitle, string title) + { + StringBuilder sb = new StringBuilder(); + int index = 0; + foreach (Paragraph p in subtitle.Paragraphs) + { + //0003 00:00:28:16 00:00:31:04 Jeg vil lære jer frygten for HERREN." (newline is \t) + sb.AppendLine(string.Format("{0:0000}\t{1}\t{2}\t{3}", index + 1, EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), Utilities.RemoveHtmlTags(p.Text).Replace(Environment.NewLine, "\t"))); + index++; + } + return sb.ToString(); + } + + private string EncodeTimeCode(TimeCode time) + { + //00:03:15:22 (last is frame) + int frames = time.Milliseconds / (1000 / 30); + return string.Format("{0:00}:{1:00}:{2:00}:{3:00}", time.Hours, time.Minutes, time.Seconds, frames); + } + + public override void LoadSubtitle(Subtitle subtitle, List lines, string fileName) + { + //00:03:15:22 00:03:23:10 This is line one. + //This is line two. + Paragraph p = null; + subtitle.Paragraphs.Clear(); + var regexTimeCodes = new Regex(@"^\d\d\d\d\t\d\d:\d\d:\d\d:\d\d\t\d\d:\d\d:\d\d:\d\d\t", RegexOptions.Compiled); + foreach (string line in lines) + { + if (regexTimeCodes.IsMatch(line)) + { + string temp = line.Substring(0, regexTimeCodes.Match(line).Length); + string start = temp.Substring(5, 11); + string end = temp.Substring(12+5, 11); + + string[] startParts = start.Split(":".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); + string[] endParts = end.Split(":".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); + if (startParts.Length == 4 && endParts.Length == 4) + { + string text = line.Remove(0, regexTimeCodes.Match(line).Length - 1).Trim(); + if (!text.Contains(Environment.NewLine)) + text = text.Replace("\t", Environment.NewLine); + p = new Paragraph(DecodeTimeCode(startParts), DecodeTimeCode(endParts), text); + subtitle.Paragraphs.Add(p); + } + } + else if (line.Trim().Length == 0) + { + // skip these lines + } + else if (line.Trim().Length > 0 && p != null) + { + _errorCount++; + } + } + subtitle.Renumber(1); + } + + private TimeCode DecodeTimeCode(string[] parts) + { + //00:00:07:12 + string hour = parts[0]; + string minutes = parts[1]; + string seconds = parts[2]; + string frames = parts[3]; + + int milliseconds = (int)((1000 / 30.0) * int.Parse(frames)); + if (milliseconds > 999) + milliseconds = 999; + + TimeCode tc = new TimeCode(int.Parse(hour), int.Parse(minutes), int.Parse(seconds), milliseconds); + return tc; + } + + } +} + diff --git a/src/Logic/SubtitleFormats/SubtitleFormat.cs b/src/Logic/SubtitleFormats/SubtitleFormat.cs index 183132236..4dea85678 100644 --- a/src/Logic/SubtitleFormats/SubtitleFormat.cs +++ b/src/Logic/SubtitleFormats/SubtitleFormat.cs @@ -16,6 +16,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats { new SubRip(), new AdobeEncore(), + new AdobeEncoreTab(), new DvdStudioPro(), new DvdSubtitle(), // new Ebu(), @@ -25,6 +26,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats new PinnacleImpression(), new QuickTimeText(), new RealTime(), + new Scenarist(), new SonyDVDArchitect(), new SonyDVDArchitectWithLineNumbers(), new SubStationAlpha(), diff --git a/src/SubtitleEdit.csproj b/src/SubtitleEdit.csproj index fdeb30a3f..738da6ae1 100644 --- a/src/SubtitleEdit.csproj +++ b/src/SubtitleEdit.csproj @@ -504,9 +504,11 @@ + +