SrtParser.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Text.RegularExpressions;
  6. using System.Threading;
  7. using MediaBrowser.Model.MediaInfo;
  8. using Microsoft.Extensions.Logging;
  9. namespace MediaBrowser.MediaEncoding.Subtitles
  10. {
  11. public class SrtParser : ISubtitleParser
  12. {
  13. private readonly ILogger _logger;
  14. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  15. public SrtParser(ILogger logger)
  16. {
  17. _logger = logger;
  18. }
  19. public SubtitleTrackInfo Parse(Stream stream, CancellationToken cancellationToken)
  20. {
  21. var trackInfo = new SubtitleTrackInfo();
  22. var trackEvents = new List<SubtitleTrackEvent>();
  23. using (var reader = new StreamReader(stream))
  24. {
  25. string line;
  26. while ((line = reader.ReadLine()) != null)
  27. {
  28. cancellationToken.ThrowIfCancellationRequested();
  29. if (string.IsNullOrWhiteSpace(line))
  30. {
  31. continue;
  32. }
  33. var subEvent = new SubtitleTrackEvent { Id = line };
  34. line = reader.ReadLine();
  35. if (string.IsNullOrWhiteSpace(line))
  36. {
  37. continue;
  38. }
  39. var time = Regex.Split(line, @"[\t ]*-->[\t ]*");
  40. if (time.Length < 2)
  41. {
  42. // This occurs when subtitle text has an empty line as part of the text.
  43. // Need to adjust the break statement below to resolve this.
  44. _logger.LogWarning("Unrecognized line in srt: {0}", line);
  45. continue;
  46. }
  47. subEvent.StartPositionTicks = GetTicks(time[0]);
  48. var endTime = time[1];
  49. var idx = endTime.IndexOf(" ", StringComparison.Ordinal);
  50. if (idx > 0)
  51. {
  52. endTime = endTime.Substring(0, idx);
  53. }
  54. subEvent.EndPositionTicks = GetTicks(endTime);
  55. var multiline = new List<string>();
  56. while ((line = reader.ReadLine()) != null)
  57. {
  58. if (string.IsNullOrEmpty(line))
  59. {
  60. break;
  61. }
  62. multiline.Add(line);
  63. }
  64. subEvent.Text = string.Join(ParserValues.NewLine, multiline);
  65. subEvent.Text = subEvent.Text.Replace(@"\N", ParserValues.NewLine, StringComparison.OrdinalIgnoreCase);
  66. subEvent.Text = Regex.Replace(subEvent.Text, @"\{(?:\\\d?[\w.-]+(?:\([^\)]*\)|&H?[0-9A-Fa-f]+&|))+\}", string.Empty, RegexOptions.IgnoreCase);
  67. subEvent.Text = Regex.Replace(subEvent.Text, "<", "&lt;", RegexOptions.IgnoreCase);
  68. subEvent.Text = Regex.Replace(subEvent.Text, ">", "&gt;", RegexOptions.IgnoreCase);
  69. subEvent.Text = Regex.Replace(subEvent.Text, "&lt;(\\/?(font|b|u|i|s))((\\s+(\\w|\\w[\\w\\-]*\\w)(\\s*=\\s*(?:\\\".*?\\\"|'.*?'|[^'\\\">\\s]+))?)+\\s*|\\s*)(\\/?)&gt;", "<$1$3$7>", RegexOptions.IgnoreCase);
  70. trackEvents.Add(subEvent);
  71. }
  72. }
  73. trackInfo.TrackEvents = trackEvents.ToArray();
  74. return trackInfo;
  75. }
  76. long GetTicks(string time)
  77. {
  78. return TimeSpan.TryParseExact(time, @"hh\:mm\:ss\.fff", _usCulture, out var span)
  79. ? span.Ticks
  80. : (TimeSpan.TryParseExact(time, @"hh\:mm\:ss\,fff", _usCulture, out span)
  81. ? span.Ticks : 0);
  82. }
  83. }
  84. }