SubtitleEdit/libse/SubtitleFormats/UnknownSubtitle9.cs

110 lines
4.2 KiB
C#
Raw Normal View History

2016-02-08 21:11:03 +01:00
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class UnknownSubtitle9 : SubtitleFormat
{
//00:04:04.219
//The city council of long beach
2017-08-03 12:43:52 +02:00
public override string Extension => ".html";
2016-02-08 21:11:03 +01:00
2017-08-03 12:43:52 +02:00
public override string Name => "Unknown 9";
2016-02-08 21:11:03 +01:00
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder();
sb.AppendLine("<div id=\"transcript\">");
sb.AppendLine(" <div name=\"transcriptText\" id=\"transcriptText\">");
sb.AppendLine(" <div id=\"transcriptPanel\">");
foreach (Paragraph p in subtitle.Paragraphs)
{
2017-08-03 12:43:52 +02:00
sb.AppendLine($" <a class=\"caption\" starttime=\"{((long)(Math.Round(p.StartTime.TotalMilliseconds))).ToString(CultureInfo.InvariantCulture)}\" duration=\"{((long)(Math.Round(p.Duration.TotalMilliseconds))).ToString(CultureInfo.InvariantCulture)}\">{p.Text.Replace(Environment.NewLine, "<br />")}</a>");
2016-02-08 21:11:03 +01:00
}
sb.AppendLine(" </div>");
sb.AppendLine(" </div>");
sb.AppendLine("</div>");
return sb.ToString().Trim();
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
//<a class="caption" starttime="0" duration="16000">[♪techno music♪]</a>
var temp = new StringBuilder();
foreach (string l in lines)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
temp.Append(l);
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
string all = temp.ToString();
if (!all.Contains("class=\"caption\""))
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
return;
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
_errorCount = 0;
subtitle.Paragraphs.Clear();
for (int i = 0; i < lines.Count; i++)
{
string line = lines[i].Trim();
var indexOfStart = line.IndexOf("starttime=", StringComparison.Ordinal);
var indexOfDuration = line.IndexOf("duration=", StringComparison.Ordinal);
if (line.Contains("class=\"caption\"") && indexOfStart > 0 && indexOfDuration > 0)
{
string startTime = "0";
int index = indexOfStart + 10;
while (index < line.Length && @"0123456789""'.,".Contains(line[index]))
{
if (@"0123456789,.".Contains(line[index]))
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
startTime += line[index];
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
index++;
}
string duration = "0";
index = indexOfDuration + 9;
while (index < line.Length && @"0123456789""'.,".Contains(line[index]))
{
if (@"0123456789,.".Contains(line[index]))
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
duration += line[index];
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
index++;
}
string text = string.Empty;
index = line.IndexOf('>', indexOfDuration);
if (index > 0 && index + 1 < line.Length)
{
text = line.Substring(index + 1).Trim();
index = text.IndexOf("</", StringComparison.Ordinal);
if (index > 0)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
text = text.Substring(0, index);
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
text = text.Replace("<br />", Environment.NewLine);
}
long startMilliseconds;
long durationMilliseconds;
if (text.Length > 0 && long.TryParse(startTime, out startMilliseconds) && long.TryParse(duration, out durationMilliseconds))
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
subtitle.Paragraphs.Add(new Paragraph(text, startMilliseconds, startMilliseconds + durationMilliseconds));
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
}
}
subtitle.Renumber();
}
}
}