Add new subtitle format - thx lererledd :)

Fix #8247
This commit is contained in:
Nikolaj Olsson 2024-04-27 19:48:20 +02:00
parent 1500ab3bb0
commit 1946176eef
2 changed files with 72 additions and 0 deletions

View File

@ -248,6 +248,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
new WebVTT(),
new WebVTTFileWithLineNumber(),
new WhisperRaw(),
new WhisperRaw2(),
new Xif(),
new Xmp(),
new YouTubeAnnotations(),

View File

@ -0,0 +1,71 @@
using Nikse.SubtitleEdit.Core.Common;
using Nikse.SubtitleEdit.Core.Enums;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class WhisperRaw2 : SubtitleFormat
{
private readonly Regex _timeRegex = new Regex(@"^\[\d+.\d+s -> \d+.\d+s\]", RegexOptions.Compiled);
public override string Extension => ".txt";
public override string Name => "Whisper Raw 2";
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder();
const string writeFormat = "[{0} -> {1}] {2}";
foreach (var p in subtitle.Paragraphs)
{
sb.AppendLine(string.Format(writeFormat, EncodeEndTimeCode(p.StartTime), EncodeEndTimeCode(p.EndTime), HtmlUtil.RemoveHtmlTags(p.Text.Replace(Environment.NewLine, " "), true)));
sb.AppendLine();
}
return sb.ToString();
}
private static string EncodeEndTimeCode(TimeCode time)
{
return $"{time.TotalSeconds:0.00}s";
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
subtitle.Paragraphs.Clear();
_errorCount = 0;
foreach (var line in lines)
{
var trimmedLine = line.Trim();
if (trimmedLine.StartsWith('['))
{
var match = _timeRegex.Match(trimmedLine);
if (match.Success)
{
var timeString = trimmedLine.Substring(0, match.Length).Trim('[', ']');
var splitPos = timeString.IndexOf('>');
if (splitPos > 1 && splitPos < timeString.Length - 3)
{
var start = timeString.Substring(0, splitPos -1).Trim().TrimEnd('s');
var end = timeString.Substring(splitPos +1).Trim().TrimEnd('s');
var text = trimmedLine.Remove(0, match.Length).Trim();
if (!string.IsNullOrEmpty(text))
{
if (double.TryParse(start, NumberStyles.Any, CultureInfo.InvariantCulture, out var dStart) &&
double.TryParse(end, NumberStyles.Any, CultureInfo.InvariantCulture, out var dEnd))
{
subtitle.Paragraphs.Add(new Paragraph(text, dStart * 1000.0, dEnd * 1000.0));
}
}
}
}
}
}
subtitle.Sort(SubtitleSortCriteria.StartTime);
subtitle.Renumber();
}
}
}