2019-01-13 20:02:23 +00:00
|
|
|
using System.IO;
|
2019-10-26 20:53:53 +00:00
|
|
|
using System.Text.Json;
|
2018-12-14 09:40:55 +00:00
|
|
|
using System.Threading;
|
2019-01-13 19:26:04 +00:00
|
|
|
using MediaBrowser.Model.MediaInfo;
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.MediaEncoding.Subtitles
|
|
|
|
{
|
2019-10-26 20:53:53 +00:00
|
|
|
/// <summary>
|
|
|
|
/// JSON subtitle writer.
|
|
|
|
/// </summary>
|
2018-12-14 09:40:55 +00:00
|
|
|
public class JsonWriter : ISubtitleWriter
|
|
|
|
{
|
2019-10-26 20:53:53 +00:00
|
|
|
/// <inheritdoc />
|
2018-12-14 09:40:55 +00:00
|
|
|
public void Write(SubtitleTrackInfo info, Stream stream, CancellationToken cancellationToken)
|
|
|
|
{
|
2019-10-26 20:53:53 +00:00
|
|
|
using (var writer = new Utf8JsonWriter(stream))
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2019-10-26 20:53:53 +00:00
|
|
|
var trackevents = info.TrackEvents;
|
2020-01-02 13:38:13 +00:00
|
|
|
writer.WriteStartObject();
|
2019-10-26 20:53:53 +00:00
|
|
|
writer.WriteStartArray("TrackEvents");
|
|
|
|
|
|
|
|
for (int i = 0; i < trackevents.Count; i++)
|
|
|
|
{
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
var current = trackevents[i];
|
|
|
|
writer.WriteStartObject();
|
|
|
|
|
|
|
|
writer.WriteString("Id", current.Id);
|
|
|
|
writer.WriteString("Text", current.Text);
|
|
|
|
writer.WriteNumber("StartPositionTicks", current.StartPositionTicks);
|
|
|
|
writer.WriteNumber("EndPositionTicks", current.EndPositionTicks);
|
|
|
|
|
|
|
|
writer.WriteEndObject();
|
|
|
|
}
|
2018-12-14 09:40:55 +00:00
|
|
|
|
2020-01-02 13:38:13 +00:00
|
|
|
writer.WriteEndArray();
|
2019-10-26 20:53:53 +00:00
|
|
|
writer.WriteEndObject();
|
2020-01-02 13:38:13 +00:00
|
|
|
|
|
|
|
writer.Flush();
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|