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. parseMedia(data, time) {
  31. const StringUtils = shaka.util.StringUtils;
  32. const LrcTextParser = shaka.text.LrcTextParser;
  33. // Get the input as a string.
  34. const str = StringUtils.fromUTF8(data);
  35. /** @type {shaka.extern.Cue} */
  36. let prevCue = null;
  37. /** @type {!Array.<!shaka.extern.Cue>} */
  38. const cues = [];
  39. const lines = str.split(/\r?\n/);
  40. for (const line of lines) {
  41. if (!line || /^\s+$/.test(line)) {
  42. continue;
  43. }
  44. // LRC content
  45. const match = LrcTextParser.lyricLine_.exec(line);
  46. if (match) {
  47. const startTime = LrcTextParser.parseTime_(match[1]);
  48. // This time can be overwritten by a subsequent cue.
  49. // By default we add 2 seconds of duration.
  50. const endTime = time.segmentEnd ? time.segmentEnd : startTime + 2;
  51. const payload = match[2];
  52. const cue = new shaka.text.Cue(startTime, endTime, payload);
  53. // Update previous
  54. if (prevCue) {
  55. prevCue.endTime = startTime;
  56. cues.push(prevCue);
  57. }
  58. prevCue = cue;
  59. continue;
  60. }
  61. shaka.log.warning('LrcTextParser encountered an unknown line.', line);
  62. }
  63. if (prevCue) {
  64. cues.push(prevCue);
  65. }
  66. return cues;
  67. }
  68. /**
  69. * Parses a LRC time from the given parser.
  70. *
  71. * @param {string} string
  72. * @return {number}
  73. * @private
  74. */
  75. static parseTime_(string) {
  76. const LrcTextParser = shaka.text.LrcTextParser;
  77. const match = LrcTextParser.timeFormat_.exec(string);
  78. const minutes = parseInt(match[1], 10);
  79. const seconds = parseFloat(match[2].replace(',', '.'));
  80. return minutes * 60 + seconds;
  81. }
  82. };
  83. /**
  84. * @const
  85. * @private {!RegExp}
  86. * @example [00:12.0]Text or [00:12.00]Text or [00:12.000]Text or
  87. * [00:12,0]Text or [00:12,00]Text or [00:12,000]Text
  88. */
  89. shaka.text.LrcTextParser.lyricLine_ =
  90. /^\[(\d{1,2}:\d{1,2}(?:[.,]\d{1,3})?)\](.*)/;
  91. /**
  92. * @const
  93. * @private {!RegExp}
  94. * @example 00:12.0 or 00:12.00 or 00:12.000 or
  95. * 00:12,0 or 00:12,00 or 00:12,000
  96. */
  97. shaka.text.LrcTextParser.timeFormat_ =
  98. /^(\d+):(\d{1,2}(?:[.,]\d{1,3})?)$/;
  99. shaka.text.TextEngine.registerParser(
  100. 'application/x-subtitle-lrc', () => new shaka.text.LrcTextParser());