From 018b7be5dbbb9f29ab6e039e458014995f8a78ff Mon Sep 17 00:00:00 2001 From: niksedk Date: Fri, 5 Nov 2010 21:26:24 +0000 Subject: [PATCH] Added Spruce subtitle format git-svn-id: https://subtitleedit.googlecode.com/svn/trunk@135 99eadd0c-20b8-1223-b5c4-2a2b2df33de2 --- src/Logic/SubtitleFormats/Spruce.cs | 163 ++++++++++++++++++++ src/Logic/SubtitleFormats/SubtitleFormat.cs | 1 + 2 files changed, 164 insertions(+) create mode 100644 src/Logic/SubtitleFormats/Spruce.cs diff --git a/src/Logic/SubtitleFormats/Spruce.cs b/src/Logic/SubtitleFormats/Spruce.cs new file mode 100644 index 000000000..dea787117 --- /dev/null +++ b/src/Logic/SubtitleFormats/Spruce.cs @@ -0,0 +1,163 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +namespace Nikse.SubtitleEdit.Logic.SubtitleFormats +{ + public class Spruce : SubtitleFormat + { + public override string Extension + { + get { return ".stl"; } + } + + public override string Name + { + get { return "Spruce Subtitle File"; } + } + + 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 = @"//Font select and font size +$FontName = Arial +$FontSize = 30 + +//Character attributes (global) +$Bold = FALSE +$UnderLined = FALSE +$Italic = FALSE + +//Position Control +$HorzAlign = Center +$VertAlign = Bottom +$XOffset = 0 +$YOffset = 0 + +//Contrast Control +$TextContrast = 15 +$Outline1Contrast = 8 +$Outline2Contrast = 15 +$BackgroundContrast = 0 + +//Effects Control +$ForceDisplay = FALSE +$FadeIn = 0 +$FadeOut = 0 + +//Other Controls +$TapeOffset = FALSE +//$SetFilePathToken = <<:>> + +//Colors +$ColorIndex1 = 0 +$ColorIndex2 = 1 +$ColorIndex3 = 2 +$ColorIndex4 = 3 +"; + StringBuilder sb = new StringBuilder(); + sb.AppendLine(Header); + foreach (Paragraph p in subtitle.Paragraphs) + { + sb.AppendLine(string.Format("{0},{1},{2}", EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), EncodeText(p.Text))); + } + return sb.ToString(); + } + + private string EncodeText(string text) + { + text = text.Replace("", "^B"); + text = text.Replace("", string.Empty); + text = text.Replace("", "^I"); + text = text.Replace("", string.Empty); + text = text.Replace("", "^U"); + text = text.Replace("", string.Empty); + return text.Replace(Environment.NewLine, "|"); + } + + private string EncodeTimeCode(TimeCode time) + { + //00:01:54:19 + + 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) + { + //00:01:54:19,00:01:56:17,We should be thankful|they accepted our offer. + subtitle.Paragraphs.Clear(); + var regexTimeCodes = new Regex(@"^\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 start = line.Substring(0, 11); + string end = line.Substring(12, 11); + + try + { + Paragraph p = new Paragraph(DecodeTimeCode(start), DecodeTimeCode(end), DecodeText(line.Substring(24))); + subtitle.Paragraphs.Add(p); + } + catch + { + _errorCount++; + } + } + else if (line.Trim().Length > 0 && !line.StartsWith("//") && !line.StartsWith("$")) + { + _errorCount++; + } + } + subtitle.Renumber(1); + } + + private TimeCode DecodeTimeCode(string time) + { + //00:01:54:19 + + 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; + } + + private string DecodeText(string text) + { //TODO: improve end tags + text = text.Replace("|", Environment.NewLine); + if (text.Contains("^B")) + text = text.Replace("^B", "") + ""; + if (text.Contains("^I")) + text = text.Replace("^I", "") + ""; + if (text.Contains("^U")) + text = text.Replace("^U", "") + ""; + return text; + } + } +} diff --git a/src/Logic/SubtitleFormats/SubtitleFormat.cs b/src/Logic/SubtitleFormats/SubtitleFormat.cs index f8168378d..76f08eee9 100644 --- a/src/Logic/SubtitleFormats/SubtitleFormat.cs +++ b/src/Logic/SubtitleFormats/SubtitleFormat.cs @@ -27,6 +27,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats new SubViewer10(), new SubViewer20(), new Sami(), + new Spruce(), new TimedText(), new TMPlayer(), new YouTubeSbv(),