2019-01-13 20:02:23 +00:00
|
|
|
using System;
|
2018-12-14 09:40:55 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.Text;
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
using System.Threading;
|
|
|
|
using MediaBrowser.Model.MediaInfo;
|
|
|
|
|
|
|
|
namespace MediaBrowser.MediaEncoding.Subtitles
|
|
|
|
{
|
2020-05-09 17:49:55 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Subtitle writer for the WebVTT format.
|
|
|
|
/// </summary>
|
2018-12-14 09:40:55 +00:00
|
|
|
public class VttWriter : ISubtitleWriter
|
|
|
|
{
|
2020-05-09 17:49:55 +00:00
|
|
|
/// <inheritdoc />
|
2018-12-14 09:40:55 +00:00
|
|
|
public void Write(SubtitleTrackInfo info, Stream stream, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
using (var writer = new StreamWriter(stream, Encoding.UTF8, 1024, true))
|
|
|
|
{
|
|
|
|
writer.WriteLine("WEBVTT");
|
2021-09-19 18:53:31 +00:00
|
|
|
writer.WriteLine();
|
2021-11-04 14:44:15 +00:00
|
|
|
writer.WriteLine("Region: id:subtitle width:80% lines:3 regionanchor:50%,100% viewportanchor:50%,90%");
|
2021-09-19 18:53:31 +00:00
|
|
|
writer.WriteLine();
|
2018-12-14 09:40:55 +00:00
|
|
|
foreach (var trackEvent in info.TrackEvents)
|
|
|
|
{
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
2019-01-13 20:37:13 +00:00
|
|
|
var startTime = TimeSpan.FromTicks(trackEvent.StartPositionTicks);
|
|
|
|
var endTime = TimeSpan.FromTicks(trackEvent.EndPositionTicks);
|
2018-12-14 09:40:55 +00:00
|
|
|
|
2020-05-09 17:49:55 +00:00
|
|
|
// make sure the start and end times are different and sequential
|
2018-12-14 09:40:55 +00:00
|
|
|
if (endTime.TotalMilliseconds <= startTime.TotalMilliseconds)
|
|
|
|
{
|
|
|
|
endTime = startTime.Add(TimeSpan.FromMilliseconds(1));
|
|
|
|
}
|
|
|
|
|
2021-11-04 14:44:15 +00:00
|
|
|
writer.WriteLine(@"{0:hh\:mm\:ss\.fff} --> {1:hh\:mm\:ss\.fff} region:subtitle line:90%", startTime, endTime);
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
var text = trackEvent.Text;
|
|
|
|
|
|
|
|
// TODO: Not sure how to handle these
|
|
|
|
text = Regex.Replace(text, @"\\n", " ", RegexOptions.IgnoreCase);
|
|
|
|
|
|
|
|
writer.WriteLine(text);
|
2020-07-22 11:34:51 +00:00
|
|
|
writer.WriteLine();
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|