SrtParser.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. 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. using ( var reader = new StreamReader(stream))
  23. {
  24. string line;
  25. while ((line = reader.ReadLine()) != null)
  26. {
  27. cancellationToken.ThrowIfCancellationRequested();
  28. if (string.IsNullOrWhiteSpace(line))
  29. {
  30. continue;
  31. }
  32. var subEvent = new SubtitleTrackEvent {Id = line};
  33. line = reader.ReadLine();
  34. if (string.IsNullOrWhiteSpace(line))
  35. {
  36. continue;
  37. }
  38. var time = Regex.Split(line, @"[\t ]*-->[\t ]*");
  39. if (time.Length < 2)
  40. {
  41. // This occurs when subtitle text has an empty line as part of the text.
  42. // Need to adjust the break statement below to resolve this.
  43. _logger.Warn("Unrecognized line in srt: {0}", line);
  44. continue;
  45. }
  46. subEvent.StartPositionTicks = GetTicks(time[0]);
  47. var endTime = time[1];
  48. var idx = endTime.IndexOf(" ", StringComparison.Ordinal);
  49. if (idx > 0)
  50. endTime = endTime.Substring(0, idx);
  51. subEvent.EndPositionTicks = GetTicks(endTime);
  52. var multiline = new List<string>();
  53. while ((line = reader.ReadLine()) != null)
  54. {
  55. if (string.IsNullOrEmpty(line))
  56. {
  57. break;
  58. }
  59. multiline.Add(line);
  60. }
  61. subEvent.Text = string.Join(ParserValues.NewLine, multiline);
  62. subEvent.Text = subEvent.Text.Replace(@"\N", ParserValues.NewLine, StringComparison.OrdinalIgnoreCase);
  63. subEvent.Text = Regex.Replace(subEvent.Text, @"\{(\\[\w]+\(?([\w\d]+,?)+\)?)+\}", string.Empty, RegexOptions.IgnoreCase);
  64. subEvent.Text = Regex.Replace(subEvent.Text, "<", "&lt;", RegexOptions.IgnoreCase);
  65. subEvent.Text = Regex.Replace(subEvent.Text, ">", "&gt;", RegexOptions.IgnoreCase);
  66. 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);
  67. trackInfo.TrackEvents.Add(subEvent);
  68. }
  69. }
  70. return trackInfo;
  71. }
  72. long GetTicks(string time) {
  73. TimeSpan span;
  74. return TimeSpan.TryParseExact(time, @"hh\:mm\:ss\.fff", _usCulture, out span)
  75. ? span.Ticks
  76. : (TimeSpan.TryParseExact(time, @"hh\:mm\:ss\,fff", _usCulture, out span)
  77. ? span.Ticks : 0);
  78. }
  79. }
  80. }