SubtitleEdit/libse/SubtitleFormats/YouTubeTranscriptOneLine.cs

74 lines
2.6 KiB
C#
Raw Normal View History

2016-02-08 21:11:03 +01:00
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class YouTubeTranscriptOneLine : SubtitleFormat
{
private static readonly Regex RegexTimeCodes = new Regex(@"^\d{1,3}:\d\d.+$", RegexOptions.Compiled);
2017-08-03 12:43:52 +02:00
public override string Extension => ".txt";
2016-02-08 21:11:03 +01:00
2017-08-03 12:43:52 +02:00
public override string Name => "YouTube Transcript one line";
2016-02-08 21:11:03 +01:00
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder();
const string writeFormat = "{0}{1}";
foreach (Paragraph p in subtitle.Paragraphs)
{
sb.AppendLine(string.Format(writeFormat, EncodeTimeCode(p.StartTime), HtmlUtil.RemoveHtmlTags(p.Text.Replace(Environment.NewLine, " "))));
}
return sb.ToString();
}
private static string EncodeTimeCode(TimeCode time)
{
return string.Format("{0}:{1:00}", time.Hours * 60 + time.Minutes, time.Seconds);
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
_errorCount = 0;
subtitle.Paragraphs.Clear();
char[] trimChars = { '', '.', ';', ':' };
foreach (string line in lines)
{
if (RegexTimeCodes.IsMatch(line))
{
int splitter = line.IndexOf(':') + 3;
string text = line.Remove(0, splitter);
var p = new Paragraph(DecodeTimeCode(line.Substring(0, splitter)), new TimeCode(), text);
2016-02-08 21:11:03 +01:00
subtitle.Paragraphs.Add(p);
text = text.Trim().Trim(trimChars).Trim();
if (text.Length > 0 && char.IsDigit(text[0]))
_errorCount++;
}
else
{
_errorCount += 2;
}
}
foreach (Paragraph p2 in subtitle.Paragraphs)
{
p2.Text = Utilities.AutoBreakLine(p2.Text);
}
subtitle.RecalculateDisplayTimes(Configuration.Settings.General.SubtitleMaximumDisplayMilliseconds, null);
subtitle.Renumber();
}
private static 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);
}
}
}