Source: lib/text/srt_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.SrtTextParser');
  7. goog.require('goog.asserts');
  8. goog.require('shaka.text.TextEngine');
  9. goog.require('shaka.text.VttTextParser');
  10. goog.require('shaka.util.BufferUtils');
  11. goog.require('shaka.util.StringUtils');
  12. /**
  13. * @implements {shaka.extern.TextParser}
  14. * @export
  15. */
  16. shaka.text.SrtTextParser = class {
  17. /** */
  18. constructor() {
  19. /**
  20. * @type {!shaka.extern.TextParser}
  21. * @private
  22. */
  23. this.parser_ = new shaka.text.VttTextParser();
  24. }
  25. /**
  26. * @override
  27. * @export
  28. */
  29. parseInit(data) {
  30. goog.asserts.assert(false, 'SRT does not have init segments');
  31. }
  32. /**
  33. * @override
  34. * @export
  35. */
  36. setSequenceMode(sequenceMode) {
  37. // Unused.
  38. }
  39. /**
  40. * @override
  41. * @export
  42. */
  43. setManifestType(manifestType) {
  44. // Unused.
  45. }
  46. /**
  47. * @override
  48. * @export
  49. */
  50. parseMedia(data, time, uri) {
  51. const SrtTextParser = shaka.text.SrtTextParser;
  52. const BufferUtils = shaka.util.BufferUtils;
  53. const StringUtils = shaka.util.StringUtils;
  54. // Get the input as a string.
  55. const str = StringUtils.fromUTF8(data);
  56. const vvtText = SrtTextParser.srt2webvtt(str);
  57. const newData = BufferUtils.toUint8(StringUtils.toUTF8(vvtText));
  58. return this.parser_.parseMedia(newData, time, uri, /* images= */ []);
  59. }
  60. /**
  61. * Convert a SRT format to WebVTT
  62. *
  63. * @param {string} data
  64. * @return {string}
  65. * @export
  66. */
  67. static srt2webvtt(data) {
  68. const SrtTextParser = shaka.text.SrtTextParser;
  69. let result = 'WEBVTT\n\n';
  70. // Supports no cues
  71. if (data == '') {
  72. return result;
  73. }
  74. // remove dos newlines
  75. let srt = data.replace(/\r+/g, '');
  76. // trim white space start and end
  77. srt = srt.trim();
  78. // get cues
  79. const cuelist = srt.split('\n\n');
  80. for (const cue of cuelist) {
  81. result += SrtTextParser.convertSrtCue_(cue);
  82. }
  83. return result;
  84. }
  85. /**
  86. * Convert a SRT cue into WebVTT cue
  87. *
  88. * @param {string} caption
  89. * @return {string}
  90. * @private
  91. */
  92. static convertSrtCue_(caption) {
  93. const lines = caption.split(/\n/);
  94. // detect and skip numeric identifier
  95. if (lines[0].match(/\d+/)) {
  96. lines.shift();
  97. }
  98. // convert time codes
  99. lines[0] = lines[0].replace(/,/g, '.');
  100. const webvttCue = lines.join('\n')
  101. .replace(/{b}/g, '<b>')
  102. .replace(/{\/b}/g, '</b>')
  103. .replace(/{i}/g, '<i>')
  104. .replace(/{\/i}/g, '</i>')
  105. .replace(/{u}/g, '<u>')
  106. .replace(/{\/u}/g, '</u>');
  107. return webvttCue + '\n\n';
  108. }
  109. };
  110. shaka.text.TextEngine.registerParser(
  111. 'text/srt', () => new shaka.text.SrtTextParser());