From 12390d9ea93289ab3e17baa08be916270b81444c Mon Sep 17 00:00:00 2001 From: niksedk Date: Thu, 9 Dec 2010 14:03:42 +0000 Subject: [PATCH] Added subtitle format "Quicktime text" (two variations) git-svn-id: https://subtitleedit.googlecode.com/svn/trunk@176 99eadd0c-20b8-1223-b5c4-2a2b2df33de2 --- src/Logic/SubtitleFormats/QuickTimeText.cs | 158 ++++++++++++++++++++ src/Logic/SubtitleFormats/SubtitleFormat.cs | 1 + 2 files changed, 159 insertions(+) create mode 100644 src/Logic/SubtitleFormats/QuickTimeText.cs diff --git a/src/Logic/SubtitleFormats/QuickTimeText.cs b/src/Logic/SubtitleFormats/QuickTimeText.cs new file mode 100644 index 000000000..8d9dbe5e5 --- /dev/null +++ b/src/Logic/SubtitleFormats/QuickTimeText.cs @@ -0,0 +1,158 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +namespace Nikse.SubtitleEdit.Logic.SubtitleFormats +{ + class QuickTimeText : SubtitleFormat + { + public override string Extension + { + get { return ".txt"; } + } + + public override string Name + { + get { return "QuickTime text"; } + } + + 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(); + LoadSubtitle(subtitle, lines, fileName); + return subtitle.Paragraphs.Count > _errorCount; + } + + public override string ToText(Subtitle subtitle, string title) + { + const string Header = @"{QTtext} {font:Tahoma} +{plain} {size:20} +{timeScale:30} +{width:160} {height:32} +{timeStamps:absolute} {language:0}"; + + StringBuilder sb = new StringBuilder(); + sb.AppendLine(Header); + int index = 0; + foreach (Paragraph p in subtitle.Paragraphs) + { + //[00:00:07.12] + //d’être perdu dans un brouillard de pensées,  + //[00:00:17.06] (this line is optional!) + // (blank line optional too) + //[00:00:26.26] + //tout le temps, + //[00:00:35.08] + + sb.AppendLine(string.Format("{0}{1}{2}", EncodeTimeCode(p.StartTime) + Environment.NewLine, Utilities.RemoveHtmlTags(p.Text) + Environment.NewLine, EncodeTimeCode(p.EndTime) + Environment.NewLine)); + index++; + } + return sb.ToString(); + } + + private string EncodeTimeCode(TimeCode time) + { + //[00:00:07.12] + 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:00:07.12] + //d’être perdu dans un brouillard de pensées,  + //[00:00:17.06] (this line is optional!) + // (blank line optional too) + //[00:00:26.26] + //tout le temps, + //[00:00:35.08] + Paragraph p = null; + subtitle.Paragraphs.Clear(); + var regexTimeCodes = new Regex(@"^\[\d\d:\d\d:\d\d.\d\d\]", RegexOptions.Compiled); + foreach (string line in lines) + { + if (regexTimeCodes.IsMatch(line)) + { + string temp = line.Substring(0, regexTimeCodes.Match(line).Length); + string[] parts = temp.Split(".:[]".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); + if (parts.Length == 4) + { + if (p == null || string.IsNullOrEmpty(p.Text)) + { + try + { + string text = string.Empty; + int indexOfEndTime = line.IndexOf(']'); + if (indexOfEndTime > 0 && indexOfEndTime + 1 < line.Length) + text = line.Substring(indexOfEndTime + 1); + p = new Paragraph(DecodeTimeCode(parts), DecodeTimeCode(parts), text); + } + catch + { + _errorCount++; + } + } + else + { + p.EndTime = DecodeTimeCode(parts); + subtitle.Paragraphs.Add(p); + + string text = string.Empty; + int indexOfEndTime = line.IndexOf(']'); + if (indexOfEndTime > 0 && indexOfEndTime + 1 < line.Length) + text = line.Substring(indexOfEndTime + 1); + p = new Paragraph(DecodeTimeCode(parts), DecodeTimeCode(parts), text); + } + } + } + else if (line.Trim().Length == 0) + { + // skip these lines + } + else if (line.Trim().Length > 0 && p != null) + { + if (string.IsNullOrEmpty(p.Text)) + p.Text = line; + else + p.Text = p.Text + Environment.NewLine + line; + } + else + { + _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 f25b17f7b..ef64909ed 100644 --- a/src/Logic/SubtitleFormats/SubtitleFormat.cs +++ b/src/Logic/SubtitleFormats/SubtitleFormat.cs @@ -21,6 +21,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats new MicroDvd(), new MPlayer2(), new OpenDvt(), + new QuickTimeText(), new SonyDVDArchitect(), new SonyDVDArchitectWithLineNumbers(), new SubStationAlpha(),