From 7dc6f46364c0ba7a47766fa2b57acbe01cb62464 Mon Sep 17 00:00:00 2001 From: niksedk Date: Sat, 13 Nov 2010 21:01:06 +0000 Subject: [PATCH] Added Spruce + Ulead subtitle formats git-svn-id: https://subtitleedit.googlecode.com/svn/trunk@143 99eadd0c-20b8-1223-b5c4-2a2b2df33de2 --- src/Logic/SubtitleFormats/Spruce.cs | 3 +- src/Logic/SubtitleFormats/SubtitleFormat.cs | 2 + .../SubtitleFormats/UleadSubtitleFormat.cs | 143 ++++++++++++++++++ 3 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 src/Logic/SubtitleFormats/UleadSubtitleFormat.cs diff --git a/src/Logic/SubtitleFormats/Spruce.cs b/src/Logic/SubtitleFormats/Spruce.cs index dea787117..7d4cb4d68 100644 --- a/src/Logic/SubtitleFormats/Spruce.cs +++ b/src/Logic/SubtitleFormats/Spruce.cs @@ -71,7 +71,8 @@ $ColorIndex1 = 0 $ColorIndex2 = 1 $ColorIndex3 = 2 $ColorIndex4 = 3 -"; + +//Subtitles"; StringBuilder sb = new StringBuilder(); sb.AppendLine(Header); foreach (Paragraph p in subtitle.Paragraphs) diff --git a/src/Logic/SubtitleFormats/SubtitleFormat.cs b/src/Logic/SubtitleFormats/SubtitleFormat.cs index 76f08eee9..f25b17f7b 100644 --- a/src/Logic/SubtitleFormats/SubtitleFormat.cs +++ b/src/Logic/SubtitleFormats/SubtitleFormat.cs @@ -28,10 +28,12 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats new SubViewer20(), new Sami(), new Spruce(), + new SubtitleEditorProject(), new TimedText(), new TMPlayer(), new YouTubeSbv(), // new Idx(), + new UleadSubtitleFormat(), new UnknownSubtitle1(), new UnknownSubtitle2(), new UnknownSubtitle3(), diff --git a/src/Logic/SubtitleFormats/UleadSubtitleFormat.cs b/src/Logic/SubtitleFormats/UleadSubtitleFormat.cs new file mode 100644 index 000000000..2568be17d --- /dev/null +++ b/src/Logic/SubtitleFormats/UleadSubtitleFormat.cs @@ -0,0 +1,143 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +namespace Nikse.SubtitleEdit.Logic.SubtitleFormats +{ + class UleadSubtitleFormat : SubtitleFormat + { + public override string Extension + { + get { return ".txt"; } + } + + public override string Name + { + get { return "Ulead subtitle format"; } + } + + 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(); + LoadSubtitle(subtitle, lines, fileName); + return subtitle.Paragraphs.Count > _errorCount; + } + + public override string ToText(Subtitle subtitle, string title) + { + const string Header = @"#Ulead subtitle format + +#Subtitle stream attribute begin +#FR:25.00 +#Subtitle stream attribute end + +#Subtitle text begin"; + + const string Footer = @"#Subtitle text end + +#Subtitle text attribute begin +#/R:1,856 /FP:8 /FS:24 +#Subtitle text attribute end"; + + + StringBuilder sb = new StringBuilder(); + sb.AppendLine(Header); + int index = 0; + foreach (Paragraph p in subtitle.Paragraphs) + { + //#3 00;04;26;04 00;04;27;05 + //How much in there? - + //Three... + sb.AppendLine(string.Format("#{0} {1} {2}", index, EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime))); + sb.AppendLine(Utilities.RemoveHtmlTags(p.Text)); + index++; + } + sb.AppendLine(Footer); + return sb.ToString(); + } + + private string EncodeTimeCode(TimeCode time) + { + //00;04;27;05 + int frames = time.Milliseconds / (1000 / 25); + 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) + { + //#3 00;04;26;04 00;04;27;05 + //How much in there? - + //Three... + Paragraph p = null; + subtitle.Paragraphs.Clear(); + var regexTimeCodes = new Regex(@"^#\d+ \d\d;\d\d;\d\d;\d\d \d\d;\d\d;\d\d;\d\d", RegexOptions.Compiled); + foreach (string line in lines) + { + if (regexTimeCodes.IsMatch(line)) + { + string[] parts = line.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); + if (parts.Length == 3) + { + string start = parts[1]; + string end = parts[2]; + try + { + p = new Paragraph(DecodeTimeCode(start), DecodeTimeCode(end), string.Empty); + subtitle.Paragraphs.Add(p); + } + catch + { + _errorCount++; + } + } + } + else if (line.Trim().Length == 0 || line.StartsWith("#")) + { + // 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 time) + { + //00;04;26;04 + + string hour = time.Substring(0, 2); + string minutes = time.Substring(3, 2); + string seconds = time.Substring(6, 2); + string frames = time.Substring(9, 2); + + int milliseconds = (int)((1000 / 25.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; + } + + } +} +