Add new json format - thx Tim :)

This commit is contained in:
niksedk 2023-11-14 07:45:13 +01:00
parent c18b433f22
commit 604f6ab21a
4 changed files with 98 additions and 1 deletions

View File

@ -10,6 +10,7 @@
* Statistics for selected lines
* Shortcut "Set start time and go to next"
* Add new json subtitle format - thx Neven
* Add new json format - thx Tim
* Add Video CD DAT subtitle format - thx Flitskikker
* Add Italian SE word dictionary file - thx HG3112
* BD SUP edit: Add "Select non-forced lines" - thx manuelrn

View File

@ -1,4 +1,4 @@
using Nikse.SubtitleEdit.Core.Common;
 using Nikse.SubtitleEdit.Core.Common;
using System;
using System.Collections.Generic;
using System.Globalization;

View File

@ -0,0 +1,95 @@
using Nikse.SubtitleEdit.Core.Common;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
/// <summary>
/// Google Cloud Speech-to-text V2
/// </summary>
public class JsonType22 : SubtitleFormat
{
public override string Extension => ".json";
public override string Name => "JSON Type 22";
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder(@"{
'results': [
{
'alternatives': [
{
'transcript': '...',
'words': [".Replace('\'', '"'));
var count = 0;
foreach (var p in subtitle.Paragraphs)
{
if (count > 0)
{
sb.Append(',');
}
count++;
sb.AppendLine();
sb.AppendLine(" {");
sb.AppendLine($" \"startOffset\": {p.StartTime.TotalSeconds.ToString(CultureInfo.InvariantCulture)},");
sb.AppendLine($" \"endOffset\": {p.EndTime.TotalSeconds.ToString(CultureInfo.InvariantCulture)},");
sb.AppendLine($" \"word\": \"{Json.EncodeJsonText(p.Text, "\\n")}\"");
sb.Append(" }");
}
sb.Append(@"
]
}
]
}
]
}");
return sb.ToString().Trim();
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
_errorCount = 0;
var sb = new StringBuilder();
foreach (var s in lines)
{
sb.Append(s);
}
var text = sb.ToString().TrimStart();
if (!text.Contains("\"alternatives\"", StringComparison.Ordinal) ||
!text.Contains("\"words\"", StringComparison.Ordinal))
{
return;
}
var parser = new SeJsonParser();
var words = parser.GetArrayElementsByName(text, "words");
foreach (var word in words)
{
var startTimeObject = parser.GetFirstObject(word, "startOffset");
var endTimeObject = parser.GetFirstObject(word, "endOffset");
var s = parser.GetFirstObject(word, "word");
if (!string.IsNullOrEmpty(s) && !string.IsNullOrEmpty(startTimeObject) && !string.IsNullOrEmpty(endTimeObject))
{
if (double.TryParse(startTimeObject.TrimEnd('s'), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var startSecs) &&
double.TryParse(endTimeObject.TrimEnd('s'), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var endSecs))
{
var pText = Json.DecodeJsonText(s);
var p = new Paragraph(pText, startSecs * 1000.0, endSecs * 1000.0);
subtitle.Paragraphs.Add(p);
}
else
{
_errorCount++;
}
}
}
subtitle.Renumber();
}
}
}

View File

@ -149,6 +149,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
new JsonType19(),
new JsonType20(),
new JsonType21(),
new JsonType22(),
new KanopyHtml(),
new LambdaCap(),
new Lrc(),