jellyfin/MediaBrowser.MediaEncoding/Subtitles/SrtWriter.cs

40 lines
1.3 KiB
C#
Raw Normal View History

2014-06-11 13:57:18 -07:00
using System;
2014-06-18 08:15:21 -07:00
using System.Globalization;
2014-06-11 13:57:18 -07:00
using System.IO;
2014-06-18 09:08:54 -07:00
using System.Text;
2014-06-18 08:15:21 -07:00
using System.Text.RegularExpressions;
2014-06-11 13:57:18 -07:00
using System.Threading;
2015-07-19 20:43:13 -07:00
using MediaBrowser.Model.MediaInfo;
2014-06-11 13:57:18 -07:00
namespace MediaBrowser.MediaEncoding.Subtitles
{
public class SrtWriter : ISubtitleWriter
{
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-06-18 08:15:21 -07:00
{
var index = 1;
foreach (var trackEvent in info.TrackEvents)
{
cancellationToken.ThrowIfCancellationRequested();
writer.WriteLine(index.ToString(CultureInfo.InvariantCulture));
2014-06-21 22:52:31 -07:00
writer.WriteLine(@"{0:hh\:mm\:ss\,fff} --> {1:hh\:mm\:ss\,fff}", TimeSpan.FromTicks(trackEvent.StartPositionTicks), TimeSpan.FromTicks(trackEvent.EndPositionTicks));
2014-06-18 08:15:21 -07:00
var text = trackEvent.Text;
// TODO: Not sure how to handle these
2014-08-24 15:02:47 -07:00
text = Regex.Replace(text, @"\\n", " ", RegexOptions.IgnoreCase);
2014-06-18 08:15:21 -07:00
writer.WriteLine(text);
writer.WriteLine(string.Empty);
index++;
}
}
2014-06-11 13:57:18 -07:00
}
}
}