2019-01-13 20:02:23 +00:00
|
|
|
using System;
|
2018-12-14 09:40:55 +00:00
|
|
|
using System.Globalization;
|
|
|
|
using System.IO;
|
|
|
|
using System.Text;
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
using System.Threading;
|
|
|
|
using MediaBrowser.Model.MediaInfo;
|
|
|
|
|
|
|
|
namespace MediaBrowser.MediaEncoding.Subtitles
|
|
|
|
{
|
2020-07-22 11:34:51 +00:00
|
|
|
/// <summary>
|
|
|
|
/// SRT subtitle writer.
|
|
|
|
/// </summary>
|
2023-05-22 20:48:09 +00:00
|
|
|
public partial class SrtWriter : ISubtitleWriter
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2023-05-22 20:48:09 +00:00
|
|
|
[GeneratedRegex(@"\\n", RegexOptions.IgnoreCase)]
|
|
|
|
private static partial Regex NewLineEscapedRegex();
|
|
|
|
|
2020-07-22 11:34:51 +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))
|
|
|
|
{
|
2019-10-26 20:53:53 +00:00
|
|
|
var trackEvents = info.TrackEvents;
|
2018-12-14 09:40:55 +00:00
|
|
|
|
2019-10-26 20:53:53 +00:00
|
|
|
for (int i = 0; i < trackEvents.Count; i++)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
2019-10-26 20:53:53 +00:00
|
|
|
var trackEvent = trackEvents[i];
|
|
|
|
|
|
|
|
writer.WriteLine((i + 1).ToString(CultureInfo.InvariantCulture));
|
|
|
|
writer.WriteLine(
|
|
|
|
@"{0:hh\:mm\:ss\,fff} --> {1:hh\:mm\:ss\,fff}",
|
|
|
|
TimeSpan.FromTicks(trackEvent.StartPositionTicks),
|
|
|
|
TimeSpan.FromTicks(trackEvent.EndPositionTicks));
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
var text = trackEvent.Text;
|
|
|
|
|
|
|
|
// TODO: Not sure how to handle these
|
2023-05-22 20:48:09 +00:00
|
|
|
text = NewLineEscapedRegex().Replace(text, " ");
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
writer.WriteLine(text);
|
2019-10-26 20:53:53 +00:00
|
|
|
writer.WriteLine();
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|