SubtitleEdit/libse/SubtitleFormats/UnknownSubtitle69.cs

100 lines
3.8 KiB
C#
Raw Normal View History

2016-02-06 07:52:53 +01:00
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class UnknownSubtitle69 : SubtitleFormat
{
private static readonly Regex RegexTimeCode = new Regex(@"^\d+\) \d\d:\d\d:\d\d:\d\d \d\d:\d\d:\d\d:\d\d Durée : \d\d:\d\d", RegexOptions.Compiled); //10:00:02F00
2017-08-03 12:43:52 +02:00
public override string Extension => ".txt";
2016-02-06 07:52:53 +01:00
2017-08-03 12:43:52 +02:00
public override string Name => "Unknown 69";
2016-02-06 07:52:53 +01:00
public override string ToText(Subtitle subtitle, string title)
{
//1) 00:00:06:14 00:00:07:07 Durée : 00:18 Lisibilité : 011 Intervalle : 06:14 Nbc : 018
//text
//line2
//2) 00:00:07:14 00:00:09:02 Durée : 01:13 Lisibilité : 023 Intervalle : 00:07 Nbc : 026
//text
var sb = new StringBuilder();
string paragraphWriteFormat = "{0}) {1} {2} Durée : {3} Lisibilité : {4} Intervalle : {5} Nbc : {6}" + Environment.NewLine + "{7}";
int count = 1;
foreach (Paragraph p in subtitle.Paragraphs)
{
string text = HtmlUtil.RemoveHtmlTags(p.Text, true);
string start = p.StartTime.ToHHMMSSFF();
string end = p.EndTime.ToHHMMSSFF();
2017-08-03 12:43:52 +02:00
string duration = $"{p.Duration.Seconds:00}:{MillisecondsToFramesMaxFrameRate(p.Duration.Milliseconds):00}";
2016-02-06 07:52:53 +01:00
const string readability = "011";
const string interval = "06:14";
string nbc = text.Length.ToString().PadLeft(3, '0');
sb.AppendLine(string.Format(paragraphWriteFormat, count, start, end, duration, readability, interval, nbc, text));
sb.AppendLine();
count++;
}
return sb.ToString().Trim();
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
_errorCount = 0;
subtitle.Paragraphs.Clear();
var text = new StringBuilder();
Paragraph p = null;
char[] splitChars = { ':', 'F' };
for (int i = 0; i < lines.Count; i++)
{
string line = lines[i].Trim();
if (line.Length == 0)
{
if (p != null)
2019-01-19 14:40:37 +01:00
{
2016-02-06 07:52:53 +01:00
p.Text = text.ToString().Trim();
2019-01-19 14:40:37 +01:00
}
2016-02-06 07:52:53 +01:00
}
else if (RegexTimeCode.IsMatch(line))
{
var timeParts = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (timeParts.Length > 4)
{
try
{
string start = timeParts[1];
string end = timeParts[2];
p = new Paragraph();
p.StartTime = DecodeTimeCodeFrames(start.Substring(0, 11), splitChars);
p.EndTime = DecodeTimeCodeFrames(end.Substring(0, 11), splitChars);
subtitle.Paragraphs.Add(p);
2017-01-20 22:25:06 +01:00
text.Clear();
2016-02-06 07:52:53 +01:00
}
catch
{
_errorCount++;
}
}
}
else
{
text.AppendLine(line);
if (text.Length > 5000)
2019-01-19 14:40:37 +01:00
{
2016-02-06 07:52:53 +01:00
return;
2019-01-19 14:40:37 +01:00
}
2016-02-06 07:52:53 +01:00
}
}
if (p != null)
2019-01-19 14:40:37 +01:00
{
2016-02-06 07:52:53 +01:00
p.Text = text.ToString().Trim();
2019-01-19 14:40:37 +01:00
}
2016-02-06 07:52:53 +01:00
subtitle.Renumber();
}
}
}