SrtParser.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. 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. trackEvents.Add(subEvent);
  69. }
  70. }
  71. trackInfo.TrackEvents = trackEvents.ToArray();
  72. return trackInfo;
  73. }
  74. long GetTicks(string time)
  75. {
  76. return TimeSpan.TryParseExact(time, @"hh\:mm\:ss\.fff", _usCulture, out var span)
  77. ? span.Ticks
  78. : (TimeSpan.TryParseExact(time, @"hh\:mm\:ss\,fff", _usCulture, out span)
  79. ? span.Ticks : 0);
  80. }
  81. }
  82. }