From 954399267c24ffc1e236972c285369a7cc366c11 Mon Sep 17 00:00:00 2001 From: niksedk Date: Mon, 23 Jun 2014 17:42:32 +0200 Subject: [PATCH] Added new subtitle format --- src/Logic/SubtitleFormats/SubtitleFormat.cs | 1 + .../YouTubeTranscriptOneLine.cs | 94 +++++++++++++++++++ src/SubtitleEdit.csproj | 1 + 3 files changed, 96 insertions(+) create mode 100644 src/Logic/SubtitleFormats/YouTubeTranscriptOneLine.cs diff --git a/src/Logic/SubtitleFormats/SubtitleFormat.cs b/src/Logic/SubtitleFormats/SubtitleFormat.cs index 0e5cda619..fa3cbc079 100644 --- a/src/Logic/SubtitleFormats/SubtitleFormat.cs +++ b/src/Logic/SubtitleFormats/SubtitleFormat.cs @@ -139,6 +139,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats new YouTubeAnnotations(), new YouTubeSbv(), new YouTubeTranscript(), + new YouTubeTranscriptOneLine(), new ZeroG(), // new Idx(), diff --git a/src/Logic/SubtitleFormats/YouTubeTranscriptOneLine.cs b/src/Logic/SubtitleFormats/YouTubeTranscriptOneLine.cs new file mode 100644 index 000000000..47e42d166 --- /dev/null +++ b/src/Logic/SubtitleFormats/YouTubeTranscriptOneLine.cs @@ -0,0 +1,94 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +namespace Nikse.SubtitleEdit.Logic.SubtitleFormats +{ + public class YouTubeTranscriptOneLine : SubtitleFormat + { + static Regex regexTimeCodes = new Regex(@"^\d{1,3}:\d\d.+$", RegexOptions.Compiled); + + public override string Extension + { + get { return ".txt"; } + } + + public override string Name + { + get { return "YouTube Transcript one line"; } + } + + public override bool IsTimeBased + { + get { return true; } + } + + public override bool IsMine(List lines, string fileName) + { + var subtitle = new Subtitle(); + + var 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) + { + var sb = new StringBuilder(); + int index = 0; + foreach (Paragraph p in subtitle.Paragraphs) + { + sb.AppendLine(string.Format("{0}{1}", EncodeTimeCode(p.StartTime), Utilities.RemoveHtmlTags(p.Text.Replace(Environment.NewLine, " ")))); + index++; + } + return sb.ToString(); + } + + private string EncodeTimeCode(TimeCode time) + { + return string.Format("{0}:{1:00}", time.Hours * 60 + time.Minutes , time.Seconds); + } + + public override void LoadSubtitle(Subtitle subtitle, List lines, string fileName) + { + _errorCount = 0; + Paragraph p = null; + subtitle.Paragraphs.Clear(); + foreach (string line in lines) + { + if (regexTimeCodes.IsMatch(line)) + { + int splitter = line.IndexOf(":") + 3; + p = new Paragraph(DecodeTimeCode(line.Substring(0, splitter)), new TimeCode(0,0,0,0), line.Remove(0, splitter)); + subtitle.Paragraphs.Add(p); + } + else + { + _errorCount++; + } + } + foreach (Paragraph p2 in subtitle.Paragraphs) + { + p2.Text = Utilities.AutoBreakLine(p2.Text); + } + subtitle.RecalculateDisplayTimes(Configuration.Settings.General.SubtitleMaximumDisplayMilliseconds, null); + subtitle.Renumber(1); + } + + private TimeCode DecodeTimeCode(string s) + { + string[] parts = s.Split(':'); + + string minutes = parts[0]; + string seconds = parts[1]; + + return new TimeCode(0, int.Parse(minutes), int.Parse(seconds), 0); + } + + } +} + diff --git a/src/SubtitleEdit.csproj b/src/SubtitleEdit.csproj index f11b6501b..d26607125 100644 --- a/src/SubtitleEdit.csproj +++ b/src/SubtitleEdit.csproj @@ -1084,6 +1084,7 @@ +