Add new sub format - thx uckthis :)

Fix #3959
This commit is contained in:
Nikolaj Olsson 2020-01-29 21:59:16 +01:00
parent 579c8baeab
commit 942c0272b0
3 changed files with 76 additions and 1 deletions

View File

@ -210,6 +210,7 @@
<Compile Include="SeJsonParser.cs" /> <Compile Include="SeJsonParser.cs" />
<Compile Include="SeLogger.cs" /> <Compile Include="SeLogger.cs" />
<Compile Include="RulesProfile.cs" /> <Compile Include="RulesProfile.cs" />
<Compile Include="SubtitleFormats\UnknownSubtitle94.cs" />
<Compile Include="SubtitleFormats\UnknownSubtitle93.cs" /> <Compile Include="SubtitleFormats\UnknownSubtitle93.cs" />
<Compile Include="SubtitleFormats\DCinemaSmpte2014.cs" /> <Compile Include="SubtitleFormats\DCinemaSmpte2014.cs" />
<Compile Include="SubtitleFormats\ESubXf.cs" /> <Compile Include="SubtitleFormats\ESubXf.cs" />

View File

@ -293,7 +293,8 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
new UnknownSubtitle90(), new UnknownSubtitle90(),
new UnknownSubtitle91(), new UnknownSubtitle91(),
new UnknownSubtitle92(), new UnknownSubtitle92(),
new UnknownSubtitle93() new UnknownSubtitle93(),
new UnknownSubtitle94()
}; };
string path = Configuration.PluginsDirectory; string path = Configuration.PluginsDirectory;

View File

@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class UnknownSubtitle94 : SubtitleFormat
{
private static readonly Regex RegexTimeCodes = new Regex(@"^\d+:\d+\t+\d+:\d+\t.*", RegexOptions.Compiled);
public override string Extension => ".txt";
public override string Name => "Unknown 94";
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder();
foreach (var p in subtitle.Paragraphs)
{
//0:13 0:14 I'm from Londrina, Paraná, Brasil.
sb.AppendLine($"{EncodeTimeCode(p.StartTime)}\t{EncodeTimeCode(p.EndTime)}\t{HtmlUtil.RemoveHtmlTags(p.Text, true).Replace(Environment.NewLine, " ")}");
}
return sb.ToString();
}
private static string EncodeTimeCode(TimeCode time)
{
return $"{(int)(time.TotalSeconds / 60):0}:{time.Seconds:00}";
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
subtitle.Paragraphs.Clear();
_errorCount = 0;
foreach (string line in lines)
{
var s = line.Trim();
if (RegexTimeCodes.IsMatch(s))
{
var arr = s.Split('\t');
string start = arr[0];
string end = arr[1];
var text = arr[2];
var p = new Paragraph(DecodeTimeCode(start), DecodeTimeCode(end), text);
subtitle.Paragraphs.Add(p);
}
else
{
_errorCount++;
if (_errorCount > 20)
{
return;
}
}
}
subtitle.Renumber();
}
private static TimeCode DecodeTimeCode(string secondsAndMilliseconds)
{
var arr = secondsAndMilliseconds.Split(':');
// minutes to milliseconds
var totalMilliseconds = double.Parse(arr[0], NumberStyles.None, CultureInfo.InvariantCulture) * TimeCode.BaseUnit * 60;
// seconds to milliseconds
totalMilliseconds += double.Parse(arr[1], NumberStyles.None, CultureInfo.InvariantCulture) * TimeCode.BaseUnit;
return new TimeCode(totalMilliseconds);
}
}
}