SrtParser.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using MediaBrowser.Model.Extensions;
  2. using MediaBrowser.Model.Logging;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Text.RegularExpressions;
  8. using System.Threading;
  9. using MediaBrowser.Model.MediaInfo;
  10. namespace MediaBrowser.MediaEncoding.Subtitles
  11. {
  12. public class SrtParser : ISubtitleParser
  13. {
  14. private readonly ILogger _logger;
  15. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  16. public SrtParser(ILogger logger)
  17. {
  18. _logger = logger;
  19. }
  20. public SubtitleTrackInfo Parse(Stream stream, CancellationToken cancellationToken)
  21. {
  22. var trackInfo = new SubtitleTrackInfo();
  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.Warn("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. endTime = endTime.Substring(0, idx);
  52. subEvent.EndPositionTicks = GetTicks(endTime);
  53. var multiline = new List<string>();
  54. while ((line = reader.ReadLine()) != null)
  55. {
  56. if (string.IsNullOrEmpty(line))
  57. {
  58. break;
  59. }
  60. multiline.Add(line);
  61. }
  62. subEvent.Text = string.Join(ParserValues.NewLine, multiline);
  63. subEvent.Text = subEvent.Text.Replace(@"\N", ParserValues.NewLine, StringComparison.OrdinalIgnoreCase);
  64. subEvent.Text = Regex.Replace(subEvent.Text, @"\{(?:\\\d?[\w.-]+(?:\([^\)]*\)|&H?[0-9A-Fa-f]+&|))+\}", string.Empty, RegexOptions.IgnoreCase);
  65. subEvent.Text = Regex.Replace(subEvent.Text, "<", "&lt;", RegexOptions.IgnoreCase);
  66. subEvent.Text = Regex.Replace(subEvent.Text, ">", "&gt;", RegexOptions.IgnoreCase);
  67. 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);
  68. trackInfo.TrackEvents.Add(subEvent);
  69. }
  70. }
  71. return trackInfo;
  72. }
  73. long GetTicks(string time) {
  74. TimeSpan span;
  75. return TimeSpan.TryParseExact(time, @"hh\:mm\:ss\.fff", _usCulture, out span)
  76. ? span.Ticks
  77. : (TimeSpan.TryParseExact(time, @"hh\:mm\:ss\,fff", _usCulture, out span)
  78. ? span.Ticks : 0);
  79. }
  80. }
  81. }