2014-05-10 21:47:48 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2014-06-18 09:08:54 -07:00
|
|
|
|
using System.Text;
|
2014-06-13 07:24:14 -07:00
|
|
|
|
using System.Text.RegularExpressions;
|
2014-06-11 07:42:03 -07:00
|
|
|
|
using System.Threading;
|
2015-07-19 20:43:13 -07:00
|
|
|
|
using MediaBrowser.Model.MediaInfo;
|
2014-05-10 21:47:48 -07:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.MediaEncoding.Subtitles
|
|
|
|
|
{
|
|
|
|
|
public class VttWriter : ISubtitleWriter
|
|
|
|
|
{
|
2014-06-11 07:42:03 -07:00
|
|
|
|
public void Write(SubtitleTrackInfo info, Stream stream, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-06-18 09:08:54 -07:00
|
|
|
|
using (var writer = new StreamWriter(stream, Encoding.UTF8, 1024, true))
|
2014-05-10 23:29:44 -07:00
|
|
|
|
{
|
|
|
|
|
writer.WriteLine("WEBVTT");
|
|
|
|
|
writer.WriteLine(string.Empty);
|
|
|
|
|
foreach (var trackEvent in info.TrackEvents)
|
|
|
|
|
{
|
2014-06-11 07:42:03 -07:00
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
2014-08-28 23:42:56 -07:00
|
|
|
|
TimeSpan startTime = TimeSpan.FromTicks(trackEvent.StartPositionTicks);
|
|
|
|
|
TimeSpan endTime = TimeSpan.FromTicks(trackEvent.EndPositionTicks);
|
|
|
|
|
|
|
|
|
|
// make sure the start and end times are different and seqential
|
|
|
|
|
if (endTime.TotalMilliseconds <= startTime.TotalMilliseconds)
|
|
|
|
|
{
|
|
|
|
|
endTime = startTime.Add(TimeSpan.FromMilliseconds(1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writer.WriteLine(@"{0:hh\:mm\:ss\.fff} --> {1:hh\:mm\:ss\.fff}", startTime, endTime);
|
2014-06-13 07:24:14 -07:00
|
|
|
|
|
2014-06-14 11:24:20 -07:00
|
|
|
|
var text = trackEvent.Text;
|
2014-06-14 19:37:12 -07:00
|
|
|
|
|
|
|
|
|
// TODO: Not sure how to handle these
|
2014-08-24 15:02:47 -07:00
|
|
|
|
text = Regex.Replace(text, @"\\n", " ", RegexOptions.IgnoreCase);
|
2014-06-13 07:24:14 -07:00
|
|
|
|
|
|
|
|
|
writer.WriteLine(text);
|
2014-05-10 23:29:44 -07:00
|
|
|
|
writer.WriteLine(string.Empty);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-10 21:47:48 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|