jellyfin/MediaBrowser.MediaEncoding/Subtitles/SrtWriter.cs

46 lines
1.3 KiB
C#
Raw Normal View History

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 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 15:15:21 +00:00
var writer = new StreamWriter(stream);
try
{
var index = 1;
foreach (var trackEvent in info.TrackEvents)
{
cancellationToken.ThrowIfCancellationRequested();
writer.WriteLine(index.ToString(CultureInfo.InvariantCulture));
writer.WriteLine(@"{0:hh\:mm\:ss\.fff} --> {1:hh\:mm\:ss\.fff}", TimeSpan.FromTicks(trackEvent.StartPositionTicks), TimeSpan.FromTicks(trackEvent.EndPositionTicks));
var text = trackEvent.Text;
// TODO: Not sure how to handle these
text = Regex.Replace(text, @"\\N", " ", RegexOptions.IgnoreCase);
writer.WriteLine(text);
writer.WriteLine(string.Empty);
index++;
}
}
catch
{
writer.Dispose();
throw;
}
2014-06-11 20:57:18 +00:00
}
}
}