Add new sub format - thx Sergiy :)

This commit is contained in:
niksedk 2022-03-21 19:02:37 +01:00
parent a1ee0bc50d
commit 40e4cb662e
3 changed files with 115 additions and 10 deletions

View File

@ -346,6 +346,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
new UnknownSubtitle103(),
new UnknownSubtitle104(),
new UnknownSubtitle105(),
new UnknownSubtitle106(),
};
string path = Configuration.PluginsDirectory;

View File

@ -0,0 +1,96 @@
using Nikse.SubtitleEdit.Core.Common;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class UnknownSubtitle106 : SubtitleFormat
{
//7:00:01AM
private static readonly Regex RegexTimeCodes = new Regex(@"^\d\:\d\d\:\d\d\t", RegexOptions.Compiled);
public override string Extension => ".txt";
public override string Name => "Unknown 106";
public override bool IsMine(List<string> lines, string fileName)
{
return base.IsMine(lines, fileName) &&
!new TimeCodesOnly2().IsMine(lines, fileName) &&
!new UnknownSubtitle47().IsMine(lines, fileName);
}
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder();
foreach (var p in subtitle.Paragraphs)
{
sb.AppendLine($"{EncodeTimeCode(p.StartTime)}\t{p.Text.Replace(Environment.NewLine, " ")}");
}
return sb.ToString().Trim();
}
private static string EncodeTimeCode(TimeCode timeCode)
{
return $"{timeCode.Hours}:{timeCode.Minutes:00}:{timeCode.Seconds:00}";
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
_errorCount = 0;
foreach (var line in lines)
{
var s = line.Trim();
if (RegexTimeCodes.Match(s).Success)
{
var arr = s.Substring(0, 7).Split(':');
if (arr.Length == 3 &&
int.TryParse(arr[0], out var hours) &&
int.TryParse(arr[1], out var minutes) &&
int.TryParse(arr[2], out var seconds))
{
var p = new Paragraph
{
StartTime = new TimeCode(hours, minutes, seconds, 0),
Text = s.Remove(0, 7).Trim()
};
subtitle.Paragraphs.Add(p);
}
else
{
_errorCount++;
}
}
else if (s.Length > 0)
{
_errorCount++;
}
}
var index = 1;
foreach (var paragraph in subtitle.Paragraphs)
{
var next = subtitle.GetParagraphOrDefault(index);
if (next != null)
{
paragraph.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - 1;
}
else
{
paragraph.EndTime.TotalMilliseconds = paragraph.StartTime.TotalMilliseconds + Configuration.Settings.General.NewEmptyDefaultMs;
}
if (paragraph.Duration.TotalMilliseconds > Configuration.Settings.General.SubtitleMaximumDisplayMilliseconds)
{
paragraph.EndTime.TotalMilliseconds = paragraph.StartTime.TotalMilliseconds + Utilities.GetOptimalDisplayMilliseconds(paragraph.Text);
}
index++;
}
subtitle.RemoveEmptyLines();
subtitle.Renumber();
}
}
}

View File

@ -23,7 +23,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder();
foreach (Paragraph p in subtitle.Paragraphs)
foreach (var p in subtitle.Paragraphs)
{
sb.AppendLine($"{EncodeTimeCode(p.StartTime)}\t{p.Text.Replace(Environment.NewLine, " ")}");
}
@ -38,9 +38,9 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
_errorCount = 0;
foreach (string line in lines)
foreach (var line in lines)
{
string s = line.Trim();
var s = line.Trim();
if (RegexTimeCodes.Match(s).Success)
{
try
@ -48,9 +48,11 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
var arr = s.Substring(0, 10).Split(':');
if (arr.Length == 4)
{
var p = new Paragraph();
p.StartTime = DecodeTimeCodeFramesFourParts(arr);
p.Text = s.Remove(0, 10).Trim();
var p = new Paragraph
{
StartTime = DecodeTimeCodeFramesFourParts(arr),
Text = s.Remove(0, 10).Trim()
};
subtitle.Paragraphs.Add(p);
}
}
@ -65,24 +67,30 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
}
}
int index = 1;
foreach (Paragraph paragraph in subtitle.Paragraphs)
var index = 1;
foreach (var paragraph in subtitle.Paragraphs)
{
Paragraph next = subtitle.GetParagraphOrDefault(index);
var next = subtitle.GetParagraphOrDefault(index);
if (next != null)
{
paragraph.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - 1;
}
else
{
paragraph.EndTime.TotalMilliseconds = paragraph.StartTime.TotalMilliseconds + Configuration.Settings.General.NewEmptyDefaultMs;
}
if (paragraph.Duration.TotalMilliseconds > Configuration.Settings.General.SubtitleMaximumDisplayMilliseconds)
{
paragraph.EndTime.TotalMilliseconds = paragraph.StartTime.TotalMilliseconds + Utilities.GetOptimalDisplayMilliseconds(paragraph.Text);
}
index++;
}
subtitle.RemoveEmptyLines();
subtitle.Renumber();
}
}
}