Source: lib/text/lrc_text_parser.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.text.LrcTextParser');
  7. goog.require('goog.asserts');
  8. goog.require('shaka.log');
  9. goog.require('shaka.text.Cue');
  10. goog.require('shaka.text.TextEngine');
  11. goog.require('shaka.util.StringUtils');
  12. /**
  13. * LRC file format: https://en.wikipedia.org/wiki/LRC_(file_format)
  14. *
  15. * @implements {shaka.extern.TextParser}
  16. * @export
  17. */
  18. shaka.text.LrcTextParser = class {
  19. /**
  20. * @override
  21. * @export
  22. */
  23. parseInit(data) {
  24. goog.asserts.assert(false, 'LRC does not have init segments');
  25. }
  26. /**
  27. * @override
  28. * @export
  29. */
  30. setSequenceMode(sequenceMode) {
  31. // Unused.
  32. }
  33. /**
  34. * @override
  35. * @export
  36. */
  37. parseMedia(data, time) {
  38. const StringUtils = shaka.util.StringUtils;
  39. const LrcTextParser = shaka.text.LrcTextParser;
  40. // Get the input as a string.
  41. const str = StringUtils.fromUTF8(data);
  42. /** @type {shaka.extern.Cue} */
  43. let prevCue = null;
  44. /** @type {!Array.<!shaka.extern.Cue>} */
  45. const cues = [];
  46. const lines = str.split(/\r?\n/);
  47. for (const line of lines) {
  48. if (!line || /^\s+$/.test(line)) {
  49. continue;
  50. }
  51. // LRC content
  52. const match = LrcTextParser.lyricLine_.exec(line);
  53. if (match) {
  54. const startTime = LrcTextParser.parseTime_(match[1]);
  55. // This time can be overwritten by a subsequent cue.
  56. // By default we add 2 seconds of duration.
  57. const endTime = time.segmentEnd ? time.segmentEnd : startTime + 2;
  58. const payload = match[2];
  59. const cue = new shaka.text.Cue(startTime, endTime, payload);
  60. // Update previous
  61. if (prevCue) {
  62. prevCue.endTime = startTime;
  63. cues.push(prevCue);
  64. }
  65. prevCue = cue;
  66. continue;
  67. }
  68. shaka.log.warning('LrcTextParser encountered an unknown line.', line);
  69. }
  70. if (prevCue) {
  71. cues.push(prevCue);
  72. }
  73. return cues;
  74. }
  75. /**
  76. * Parses a LRC time from the given parser.
  77. *
  78. * @param {string} string
  79. * @return {number}
  80. * @private
  81. */
  82. static parseTime_(string) {
  83. const LrcTextParser = shaka.text.LrcTextParser;
  84. const match = LrcTextParser.timeFormat_.exec(string);
  85. const minutes = parseInt(match[1], 10);
  86. const seconds = parseFloat(match[2].replace(',', '.'));
  87. return minutes * 60 + seconds;
  88. }
  89. };
  90. /**
  91. * @const
  92. * @private {!RegExp}
  93. * @example [00:12.0]Text or [00:12.00]Text or [00:12.000]Text or
  94. * [00:12,0]Text or [00:12,00]Text or [00:12,000]Text
  95. */
  96. shaka.text.LrcTextParser.lyricLine_ =
  97. /^\[(\d{1,2}:\d{1,2}(?:[.,]\d{1,3})?)\](.*)/;
  98. /**
  99. * @const
  100. * @private {!RegExp}
  101. * @example 00:12.0 or 00:12.00 or 00:12.000 or
  102. * 00:12,0 or 00:12,00 or 00:12,000
  103. */
  104. shaka.text.LrcTextParser.timeFormat_ =
  105. /^(\d+):(\d{1,2}(?:[.,]\d{1,3})?)$/;
  106. shaka.text.TextEngine.registerParser(
  107. 'application/x-subtitle-lrc', () => new shaka.text.LrcTextParser());