2014-06-11 20:57:18 +00:00
|
|
|
|
using System;
|
2014-06-18 15:15:21 +00:00
|
|
|
|
using System.Globalization;
|
2014-06-11 20:57:18 +00:00
|
|
|
|
using System.IO;
|
2014-06-18 16:08:54 +00:00
|
|
|
|
using System.Text;
|
2014-06-18 15:15:21 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
2014-06-11 20:57:18 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.MediaEncoding.Subtitles
|
|
|
|
|
{
|
|
|
|
|
public class SrtWriter : ISubtitleWriter
|
|
|
|
|
{
|
|
|
|
|
public void Write(SubtitleTrackInfo info, Stream stream, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-06-18 16:08:54 +00:00
|
|
|
|
using (var writer = new StreamWriter(stream, Encoding.UTF8, 1024, true))
|
2014-06-18 15:15:21 +00:00
|
|
|
|
{
|
|
|
|
|
var index = 1;
|
|
|
|
|
|
|
|
|
|
foreach (var trackEvent in info.TrackEvents)
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
writer.WriteLine(index.ToString(CultureInfo.InvariantCulture));
|
2014-06-22 05:52:31 +00:00
|
|
|
|
writer.WriteLine(@"{0:hh\:mm\:ss\,fff} --> {1:hh\:mm\:ss\,fff}", TimeSpan.FromTicks(trackEvent.StartPositionTicks), TimeSpan.FromTicks(trackEvent.EndPositionTicks));
|
2014-06-18 15:15:21 +00:00
|
|
|
|
|
|
|
|
|
var text = trackEvent.Text;
|
|
|
|
|
|
|
|
|
|
// TODO: Not sure how to handle these
|
2014-08-24 22:02:47 +00:00
|
|
|
|
text = Regex.Replace(text, @"\\n", " ", RegexOptions.IgnoreCase);
|
2014-06-18 15:15:21 +00:00
|
|
|
|
|
|
|
|
|
writer.WriteLine(text);
|
|
|
|
|
writer.WriteLine(string.Empty);
|
|
|
|
|
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-11 20:57:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|