| | | 1 | | using System; |
| | | 2 | | using System.Globalization; |
| | | 3 | | using System.IO; |
| | | 4 | | using System.Text; |
| | | 5 | | using System.Text.RegularExpressions; |
| | | 6 | | using System.Threading; |
| | | 7 | | using MediaBrowser.Model.MediaInfo; |
| | | 8 | | |
| | | 9 | | namespace MediaBrowser.MediaEncoding.Subtitles |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// SRT subtitle writer. |
| | | 13 | | /// </summary> |
| | | 14 | | public partial class SrtWriter : ISubtitleWriter |
| | | 15 | | { |
| | | 16 | | [GeneratedRegex(@"\\n", RegexOptions.IgnoreCase)] |
| | | 17 | | private static partial Regex NewLineEscapedRegex(); |
| | | 18 | | |
| | | 19 | | /// <inheritdoc /> |
| | | 20 | | public void Write(SubtitleTrackInfo info, Stream stream, CancellationToken cancellationToken) |
| | | 21 | | { |
| | 0 | 22 | | using (var writer = new StreamWriter(stream, Encoding.UTF8, 1024, true)) |
| | | 23 | | { |
| | 0 | 24 | | var trackEvents = info.TrackEvents; |
| | | 25 | | |
| | 0 | 26 | | for (int i = 0; i < trackEvents.Count; i++) |
| | | 27 | | { |
| | 0 | 28 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 29 | | |
| | 0 | 30 | | var trackEvent = trackEvents[i]; |
| | | 31 | | |
| | 0 | 32 | | writer.WriteLine((i + 1).ToString(CultureInfo.InvariantCulture)); |
| | 0 | 33 | | writer.WriteLine( |
| | 0 | 34 | | @"{0:hh\:mm\:ss\,fff} --> {1:hh\:mm\:ss\,fff}", |
| | 0 | 35 | | TimeSpan.FromTicks(trackEvent.StartPositionTicks), |
| | 0 | 36 | | TimeSpan.FromTicks(trackEvent.EndPositionTicks)); |
| | | 37 | | |
| | 0 | 38 | | var text = trackEvent.Text; |
| | | 39 | | |
| | | 40 | | // TODO: Not sure how to handle these |
| | 0 | 41 | | text = NewLineEscapedRegex().Replace(text, " "); |
| | | 42 | | |
| | 0 | 43 | | writer.WriteLine(text); |
| | 0 | 44 | | writer.WriteLine(); |
| | | 45 | | } |
| | 0 | 46 | | } |
| | 0 | 47 | | } |
| | | 48 | | } |
| | | 49 | | } |