mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-06-02 13:58:29 +01:00
Add JsonWriter back
This commit is contained in:
58
MediaBrowser.MediaEncoding/Subtitles/JsonWriter.cs
Normal file
58
MediaBrowser.MediaEncoding/Subtitles/JsonWriter.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Nikse.SubtitleEdit.Core.Common;
|
||||
using Nikse.SubtitleEdit.Core.SubtitleFormats;
|
||||
|
||||
namespace MediaBrowser.MediaEncoding.Subtitles;
|
||||
|
||||
/// <summary>
|
||||
/// JSON subtitle writer.
|
||||
/// </summary>
|
||||
public class JsonWriter : SubtitleFormat
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public override string Extension => ".json";
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string Name => "JSON Jellyfin";
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToText(Subtitle subtitle, string title)
|
||||
{
|
||||
using var ms = new MemoryStream();
|
||||
using (var writer = new Utf8JsonWriter(ms))
|
||||
{
|
||||
var trackevents = subtitle.Paragraphs;
|
||||
writer.WriteStartObject();
|
||||
writer.WriteStartArray("TrackEvents");
|
||||
|
||||
for (int i = 0; i < trackevents.Count; i++)
|
||||
{
|
||||
var current = trackevents[i];
|
||||
writer.WriteStartObject();
|
||||
|
||||
writer.WriteString("Id", current.Number.ToString(CultureInfo.InvariantCulture));
|
||||
writer.WriteString("Text", current.Text);
|
||||
writer.WriteNumber("StartPositionTicks", current.StartTime.TimeSpan.Ticks);
|
||||
writer.WriteNumber("EndPositionTicks", current.EndTime.TimeSpan.Ticks);
|
||||
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
|
||||
writer.WriteEndArray();
|
||||
writer.WriteEndObject();
|
||||
|
||||
writer.Flush();
|
||||
}
|
||||
|
||||
return Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Length);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
|
||||
=> throw new NotImplementedException();
|
||||
}
|
||||
@@ -269,10 +269,12 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
|
||||
if (string.Equals(format, "json", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
value = new JsonWriter();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (string.Equals(format, SubtitleFormat.SRT, StringComparison.OrdinalIgnoreCase) || string.Equals(format, SubtitleFormat.SUBRIP, StringComparison.OrdinalIgnoreCase))
|
||||
if (string.Equals(format, SubtitleFormat.SRT, StringComparison.OrdinalIgnoreCase)
|
||||
|| string.Equals(format, SubtitleFormat.SUBRIP, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
value = new SubRip();
|
||||
return true;
|
||||
@@ -284,7 +286,8 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
return true;
|
||||
}
|
||||
|
||||
if (string.Equals(format, SubtitleFormat.VTT, StringComparison.OrdinalIgnoreCase) || string.Equals(format, SubtitleFormat.WEBVTT, StringComparison.OrdinalIgnoreCase))
|
||||
if (string.Equals(format, SubtitleFormat.VTT, StringComparison.OrdinalIgnoreCase)
|
||||
|| string.Equals(format, SubtitleFormat.WEBVTT, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
value = new WebVTT();
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user