@kenzuya/mediabunny 1.26.0 → 1.28.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (238) hide show
  1. package/README.md +1 -1
  2. package/dist/bundles/{mediabunny.mjs → mediabunny.js} +21963 -21390
  3. package/dist/bundles/mediabunny.min.js +490 -0
  4. package/dist/modules/shared/mp3-misc.d.ts.map +1 -1
  5. package/dist/modules/src/adts/adts-demuxer.d.ts +6 -6
  6. package/dist/modules/src/adts/adts-demuxer.d.ts.map +1 -1
  7. package/dist/modules/src/adts/adts-muxer.d.ts +4 -4
  8. package/dist/modules/src/adts/adts-muxer.d.ts.map +1 -1
  9. package/dist/modules/src/adts/adts-reader.d.ts +1 -1
  10. package/dist/modules/src/adts/adts-reader.d.ts.map +1 -1
  11. package/dist/modules/src/avi/avi-demuxer.d.ts +44 -0
  12. package/dist/modules/src/avi/avi-demuxer.d.ts.map +1 -0
  13. package/dist/modules/src/avi/avi-misc.d.ts +88 -0
  14. package/dist/modules/src/avi/avi-misc.d.ts.map +1 -0
  15. package/dist/modules/src/avi/avi-muxer.d.ts +45 -0
  16. package/dist/modules/src/avi/avi-muxer.d.ts.map +1 -0
  17. package/dist/modules/src/avi/riff-writer.d.ts +26 -0
  18. package/dist/modules/src/avi/riff-writer.d.ts.map +1 -0
  19. package/dist/modules/src/codec-data.d.ts +8 -3
  20. package/dist/modules/src/codec-data.d.ts.map +1 -1
  21. package/dist/modules/src/codec.d.ts +10 -10
  22. package/dist/modules/src/codec.d.ts.map +1 -1
  23. package/dist/modules/src/conversion.d.ts +33 -16
  24. package/dist/modules/src/conversion.d.ts.map +1 -1
  25. package/dist/modules/src/custom-coder.d.ts +8 -8
  26. package/dist/modules/src/custom-coder.d.ts.map +1 -1
  27. package/dist/modules/src/demuxer.d.ts +3 -3
  28. package/dist/modules/src/demuxer.d.ts.map +1 -1
  29. package/dist/modules/src/encode.d.ts +8 -8
  30. package/dist/modules/src/encode.d.ts.map +1 -1
  31. package/dist/modules/src/flac/flac-demuxer.d.ts +7 -7
  32. package/dist/modules/src/flac/flac-demuxer.d.ts.map +1 -1
  33. package/dist/modules/src/flac/flac-misc.d.ts +3 -3
  34. package/dist/modules/src/flac/flac-misc.d.ts.map +1 -1
  35. package/dist/modules/src/flac/flac-muxer.d.ts +5 -5
  36. package/dist/modules/src/flac/flac-muxer.d.ts.map +1 -1
  37. package/dist/modules/src/id3.d.ts +3 -3
  38. package/dist/modules/src/id3.d.ts.map +1 -1
  39. package/dist/modules/src/index.d.ts +20 -20
  40. package/dist/modules/src/index.d.ts.map +1 -1
  41. package/dist/modules/src/input-format.d.ts +22 -0
  42. package/dist/modules/src/input-format.d.ts.map +1 -1
  43. package/dist/modules/src/input-track.d.ts +8 -8
  44. package/dist/modules/src/input-track.d.ts.map +1 -1
  45. package/dist/modules/src/input.d.ts +12 -12
  46. package/dist/modules/src/isobmff/isobmff-boxes.d.ts +2 -2
  47. package/dist/modules/src/isobmff/isobmff-boxes.d.ts.map +1 -1
  48. package/dist/modules/src/isobmff/isobmff-demuxer.d.ts +12 -12
  49. package/dist/modules/src/isobmff/isobmff-demuxer.d.ts.map +1 -1
  50. package/dist/modules/src/isobmff/isobmff-misc.d.ts.map +1 -1
  51. package/dist/modules/src/isobmff/isobmff-muxer.d.ts +11 -11
  52. package/dist/modules/src/isobmff/isobmff-muxer.d.ts.map +1 -1
  53. package/dist/modules/src/isobmff/isobmff-reader.d.ts +2 -2
  54. package/dist/modules/src/isobmff/isobmff-reader.d.ts.map +1 -1
  55. package/dist/modules/src/matroska/ebml.d.ts +3 -3
  56. package/dist/modules/src/matroska/ebml.d.ts.map +1 -1
  57. package/dist/modules/src/matroska/matroska-demuxer.d.ts +13 -13
  58. package/dist/modules/src/matroska/matroska-demuxer.d.ts.map +1 -1
  59. package/dist/modules/src/matroska/matroska-input.d.ts +33 -0
  60. package/dist/modules/src/matroska/matroska-input.d.ts.map +1 -0
  61. package/dist/modules/src/matroska/matroska-misc.d.ts.map +1 -1
  62. package/dist/modules/src/matroska/matroska-muxer.d.ts +5 -5
  63. package/dist/modules/src/matroska/matroska-muxer.d.ts.map +1 -1
  64. package/dist/modules/src/media-sink.d.ts +5 -5
  65. package/dist/modules/src/media-sink.d.ts.map +1 -1
  66. package/dist/modules/src/media-source.d.ts +22 -4
  67. package/dist/modules/src/media-source.d.ts.map +1 -1
  68. package/dist/modules/src/metadata.d.ts +2 -2
  69. package/dist/modules/src/metadata.d.ts.map +1 -1
  70. package/dist/modules/src/misc.d.ts +5 -4
  71. package/dist/modules/src/misc.d.ts.map +1 -1
  72. package/dist/modules/src/mp3/mp3-demuxer.d.ts +7 -7
  73. package/dist/modules/src/mp3/mp3-demuxer.d.ts.map +1 -1
  74. package/dist/modules/src/mp3/mp3-muxer.d.ts +4 -4
  75. package/dist/modules/src/mp3/mp3-muxer.d.ts.map +1 -1
  76. package/dist/modules/src/mp3/mp3-reader.d.ts +2 -2
  77. package/dist/modules/src/mp3/mp3-reader.d.ts.map +1 -1
  78. package/dist/modules/src/mp3/mp3-writer.d.ts +1 -1
  79. package/dist/modules/src/mp3/mp3-writer.d.ts.map +1 -1
  80. package/dist/modules/src/muxer.d.ts +4 -4
  81. package/dist/modules/src/muxer.d.ts.map +1 -1
  82. package/dist/modules/src/ogg/ogg-demuxer.d.ts +7 -7
  83. package/dist/modules/src/ogg/ogg-demuxer.d.ts.map +1 -1
  84. package/dist/modules/src/ogg/ogg-misc.d.ts +1 -1
  85. package/dist/modules/src/ogg/ogg-misc.d.ts.map +1 -1
  86. package/dist/modules/src/ogg/ogg-muxer.d.ts +5 -5
  87. package/dist/modules/src/ogg/ogg-muxer.d.ts.map +1 -1
  88. package/dist/modules/src/ogg/ogg-reader.d.ts +1 -1
  89. package/dist/modules/src/ogg/ogg-reader.d.ts.map +1 -1
  90. package/dist/modules/src/output-format.d.ts +51 -6
  91. package/dist/modules/src/output-format.d.ts.map +1 -1
  92. package/dist/modules/src/output.d.ts +13 -13
  93. package/dist/modules/src/output.d.ts.map +1 -1
  94. package/dist/modules/src/packet.d.ts +1 -1
  95. package/dist/modules/src/packet.d.ts.map +1 -1
  96. package/dist/modules/src/pcm.d.ts.map +1 -1
  97. package/dist/modules/src/reader.d.ts +2 -2
  98. package/dist/modules/src/reader.d.ts.map +1 -1
  99. package/dist/modules/src/sample.d.ts +57 -15
  100. package/dist/modules/src/sample.d.ts.map +1 -1
  101. package/dist/modules/src/source.d.ts +3 -3
  102. package/dist/modules/src/source.d.ts.map +1 -1
  103. package/dist/modules/src/subtitles.d.ts +1 -1
  104. package/dist/modules/src/subtitles.d.ts.map +1 -1
  105. package/dist/modules/src/target.d.ts +2 -2
  106. package/dist/modules/src/target.d.ts.map +1 -1
  107. package/dist/modules/src/tsconfig.tsbuildinfo +1 -1
  108. package/dist/modules/src/wave/riff-writer.d.ts +1 -1
  109. package/dist/modules/src/wave/riff-writer.d.ts.map +1 -1
  110. package/dist/modules/src/wave/wave-demuxer.d.ts +6 -6
  111. package/dist/modules/src/wave/wave-demuxer.d.ts.map +1 -1
  112. package/dist/modules/src/wave/wave-muxer.d.ts +4 -4
  113. package/dist/modules/src/wave/wave-muxer.d.ts.map +1 -1
  114. package/dist/modules/src/writer.d.ts +1 -1
  115. package/dist/modules/src/writer.d.ts.map +1 -1
  116. package/dist/packages/eac3/eac3.wasm +0 -0
  117. package/dist/packages/eac3/mediabunny-eac3.js +1058 -0
  118. package/dist/packages/eac3/mediabunny-eac3.min.js +44 -0
  119. package/dist/packages/mp3-encoder/mediabunny-mp3-encoder.js +694 -0
  120. package/dist/packages/mp3-encoder/mediabunny-mp3-encoder.min.js +58 -0
  121. package/dist/packages/mpeg4/mediabunny-mpeg4.js +1198 -0
  122. package/dist/packages/mpeg4/mediabunny-mpeg4.min.js +44 -0
  123. package/dist/packages/mpeg4/xvid.wasm +0 -0
  124. package/package.json +18 -57
  125. package/dist/bundles/mediabunny.cjs +0 -26140
  126. package/dist/bundles/mediabunny.min.cjs +0 -147
  127. package/dist/bundles/mediabunny.min.mjs +0 -146
  128. package/dist/mediabunny.d.ts +0 -3319
  129. package/dist/modules/shared/mp3-misc.js +0 -147
  130. package/dist/modules/src/adts/adts-demuxer.js +0 -239
  131. package/dist/modules/src/adts/adts-muxer.js +0 -80
  132. package/dist/modules/src/adts/adts-reader.js +0 -63
  133. package/dist/modules/src/codec-data.js +0 -1730
  134. package/dist/modules/src/codec.js +0 -869
  135. package/dist/modules/src/conversion.js +0 -1459
  136. package/dist/modules/src/custom-coder.js +0 -117
  137. package/dist/modules/src/demuxer.js +0 -12
  138. package/dist/modules/src/encode.js +0 -442
  139. package/dist/modules/src/flac/flac-demuxer.js +0 -504
  140. package/dist/modules/src/flac/flac-misc.js +0 -135
  141. package/dist/modules/src/flac/flac-muxer.js +0 -222
  142. package/dist/modules/src/id3.js +0 -848
  143. package/dist/modules/src/index.js +0 -28
  144. package/dist/modules/src/input-format.js +0 -480
  145. package/dist/modules/src/input-track.js +0 -372
  146. package/dist/modules/src/input.js +0 -188
  147. package/dist/modules/src/isobmff/isobmff-boxes.js +0 -1480
  148. package/dist/modules/src/isobmff/isobmff-demuxer.js +0 -2618
  149. package/dist/modules/src/isobmff/isobmff-misc.js +0 -20
  150. package/dist/modules/src/isobmff/isobmff-muxer.js +0 -966
  151. package/dist/modules/src/isobmff/isobmff-reader.js +0 -72
  152. package/dist/modules/src/matroska/ebml.js +0 -653
  153. package/dist/modules/src/matroska/matroska-demuxer.js +0 -2133
  154. package/dist/modules/src/matroska/matroska-misc.js +0 -20
  155. package/dist/modules/src/matroska/matroska-muxer.js +0 -1017
  156. package/dist/modules/src/media-sink.js +0 -1736
  157. package/dist/modules/src/media-source.js +0 -1825
  158. package/dist/modules/src/metadata.js +0 -193
  159. package/dist/modules/src/misc.js +0 -623
  160. package/dist/modules/src/mp3/mp3-demuxer.js +0 -285
  161. package/dist/modules/src/mp3/mp3-muxer.js +0 -123
  162. package/dist/modules/src/mp3/mp3-reader.js +0 -26
  163. package/dist/modules/src/mp3/mp3-writer.js +0 -78
  164. package/dist/modules/src/muxer.js +0 -50
  165. package/dist/modules/src/node.d.ts +0 -9
  166. package/dist/modules/src/node.d.ts.map +0 -1
  167. package/dist/modules/src/node.js +0 -9
  168. package/dist/modules/src/ogg/ogg-demuxer.js +0 -763
  169. package/dist/modules/src/ogg/ogg-misc.js +0 -78
  170. package/dist/modules/src/ogg/ogg-muxer.js +0 -353
  171. package/dist/modules/src/ogg/ogg-reader.js +0 -65
  172. package/dist/modules/src/output-format.js +0 -527
  173. package/dist/modules/src/output.js +0 -300
  174. package/dist/modules/src/packet.js +0 -182
  175. package/dist/modules/src/pcm.js +0 -85
  176. package/dist/modules/src/reader.js +0 -236
  177. package/dist/modules/src/sample.js +0 -1056
  178. package/dist/modules/src/source.js +0 -1182
  179. package/dist/modules/src/subtitles.js +0 -575
  180. package/dist/modules/src/target.js +0 -140
  181. package/dist/modules/src/wave/riff-writer.js +0 -30
  182. package/dist/modules/src/wave/wave-demuxer.js +0 -447
  183. package/dist/modules/src/wave/wave-muxer.js +0 -318
  184. package/dist/modules/src/writer.js +0 -370
  185. package/src/adts/adts-demuxer.ts +0 -331
  186. package/src/adts/adts-muxer.ts +0 -111
  187. package/src/adts/adts-reader.ts +0 -85
  188. package/src/codec-data.ts +0 -2078
  189. package/src/codec.ts +0 -1092
  190. package/src/conversion.ts +0 -2112
  191. package/src/custom-coder.ts +0 -197
  192. package/src/demuxer.ts +0 -24
  193. package/src/encode.ts +0 -739
  194. package/src/flac/flac-demuxer.ts +0 -730
  195. package/src/flac/flac-misc.ts +0 -164
  196. package/src/flac/flac-muxer.ts +0 -320
  197. package/src/id3.ts +0 -925
  198. package/src/index.ts +0 -221
  199. package/src/input-format.ts +0 -541
  200. package/src/input-track.ts +0 -529
  201. package/src/input.ts +0 -235
  202. package/src/isobmff/isobmff-boxes.ts +0 -1719
  203. package/src/isobmff/isobmff-demuxer.ts +0 -3190
  204. package/src/isobmff/isobmff-misc.ts +0 -29
  205. package/src/isobmff/isobmff-muxer.ts +0 -1348
  206. package/src/isobmff/isobmff-reader.ts +0 -91
  207. package/src/matroska/ebml.ts +0 -730
  208. package/src/matroska/matroska-demuxer.ts +0 -2481
  209. package/src/matroska/matroska-misc.ts +0 -29
  210. package/src/matroska/matroska-muxer.ts +0 -1276
  211. package/src/media-sink.ts +0 -2179
  212. package/src/media-source.ts +0 -2243
  213. package/src/metadata.ts +0 -320
  214. package/src/misc.ts +0 -798
  215. package/src/mp3/mp3-demuxer.ts +0 -383
  216. package/src/mp3/mp3-muxer.ts +0 -166
  217. package/src/mp3/mp3-reader.ts +0 -34
  218. package/src/mp3/mp3-writer.ts +0 -120
  219. package/src/muxer.ts +0 -88
  220. package/src/node.ts +0 -11
  221. package/src/ogg/ogg-demuxer.ts +0 -1053
  222. package/src/ogg/ogg-misc.ts +0 -116
  223. package/src/ogg/ogg-muxer.ts +0 -497
  224. package/src/ogg/ogg-reader.ts +0 -93
  225. package/src/output-format.ts +0 -945
  226. package/src/output.ts +0 -488
  227. package/src/packet.ts +0 -263
  228. package/src/pcm.ts +0 -112
  229. package/src/reader.ts +0 -323
  230. package/src/sample.ts +0 -1461
  231. package/src/source.ts +0 -1688
  232. package/src/subtitles.ts +0 -711
  233. package/src/target.ts +0 -204
  234. package/src/tsconfig.json +0 -16
  235. package/src/wave/riff-writer.ts +0 -36
  236. package/src/wave/wave-demuxer.ts +0 -529
  237. package/src/wave/wave-muxer.ts +0 -371
  238. package/src/writer.ts +0 -490
@@ -1,575 +0,0 @@
1
- /*!
2
- * Copyright (c) 2025-present, Vanilagy and contributors
3
- *
4
- * This Source Code Form is subject to the terms of the Mozilla Public
5
- * License, v. 2.0. If a copy of the MPL was not distributed with this
6
- * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7
- */
8
- const cueBlockHeaderRegex = /(?:(.+?)\n)?((?:\d{2}:)?\d{2}:\d{2}.\d{3})\s+-->\s+((?:\d{2}:)?\d{2}:\d{2}.\d{3})/g;
9
- const preambleStartRegex = /^WEBVTT(.|\n)*?\n{2}/;
10
- export const inlineTimestampRegex = /<(?:(\d{2}):)?(\d{2}):(\d{2}).(\d{3})>/g;
11
- export class SubtitleParser {
12
- constructor(options) {
13
- this.preambleText = null;
14
- this.preambleEmitted = false;
15
- this.options = options;
16
- }
17
- parse(text) {
18
- if (this.options.codec === 'srt') {
19
- this.parseSrt(text);
20
- }
21
- else if (this.options.codec === 'ass' || this.options.codec === 'ssa') {
22
- this.parseAss(text);
23
- }
24
- else if (this.options.codec === 'tx3g') {
25
- this.parseTx3g(text);
26
- }
27
- else if (this.options.codec === 'ttml') {
28
- this.parseTtml(text);
29
- }
30
- else {
31
- this.parseWebVTT(text);
32
- }
33
- }
34
- parseSrt(text) {
35
- const cues = splitSrtIntoCues(text);
36
- for (let i = 0; i < cues.length; i++) {
37
- const meta = {};
38
- // SRT doesn't have a header, but we need to provide a config for the first cue
39
- if (i === 0) {
40
- meta.config = { description: '' };
41
- }
42
- this.options.output(cues[i], meta);
43
- }
44
- }
45
- parseAss(text) {
46
- const { header, cues } = splitAssIntoCues(text);
47
- for (let i = 0; i < cues.length; i++) {
48
- const meta = {};
49
- if (i === 0 && header) {
50
- meta.config = { description: header };
51
- }
52
- this.options.output(cues[i], meta);
53
- }
54
- }
55
- parseWebVTT(text) {
56
- text = text.replaceAll('\r\n', '\n').replaceAll('\r', '\n');
57
- cueBlockHeaderRegex.lastIndex = 0;
58
- let match;
59
- if (!this.preambleText) {
60
- if (!preambleStartRegex.test(text)) {
61
- throw new Error('WebVTT preamble incorrect.');
62
- }
63
- match = cueBlockHeaderRegex.exec(text);
64
- const preamble = text.slice(0, match?.index ?? text.length).trimEnd();
65
- if (!preamble) {
66
- throw new Error('No WebVTT preamble provided.');
67
- }
68
- this.preambleText = preamble;
69
- if (match) {
70
- text = text.slice(match.index);
71
- cueBlockHeaderRegex.lastIndex = 0;
72
- }
73
- }
74
- while ((match = cueBlockHeaderRegex.exec(text))) {
75
- const notes = text.slice(0, match.index);
76
- const cueIdentifier = match[1];
77
- const matchEnd = match.index + match[0].length;
78
- const bodyStart = text.indexOf('\n', matchEnd) + 1;
79
- const cueSettings = text.slice(matchEnd, bodyStart).trim();
80
- let bodyEnd = text.indexOf('\n\n', matchEnd);
81
- if (bodyEnd === -1)
82
- bodyEnd = text.length;
83
- const startTime = parseSubtitleTimestamp(match[2]);
84
- const endTime = parseSubtitleTimestamp(match[3]);
85
- const duration = endTime - startTime;
86
- const body = text.slice(bodyStart, bodyEnd).trim();
87
- text = text.slice(bodyEnd).trimStart();
88
- cueBlockHeaderRegex.lastIndex = 0;
89
- const cue = {
90
- timestamp: startTime / 1000,
91
- duration: duration / 1000,
92
- text: body,
93
- identifier: cueIdentifier,
94
- settings: cueSettings,
95
- notes,
96
- };
97
- const meta = {};
98
- if (!this.preambleEmitted) {
99
- meta.config = {
100
- description: this.preambleText,
101
- };
102
- this.preambleEmitted = true;
103
- }
104
- this.options.output(cue, meta);
105
- }
106
- }
107
- parseTx3g(text) {
108
- // tx3g (3GPP Timed Text) samples are usually already plain text
109
- // For now, treat as plain text cue - timing comes from container
110
- const meta = { config: { description: '' } };
111
- const cue = {
112
- timestamp: 0,
113
- duration: 0,
114
- text: text.trim(),
115
- };
116
- this.options.output(cue, meta);
117
- }
118
- parseTtml(text) {
119
- // Basic TTML parsing - extract text content from <p> elements
120
- // TODO: Full TTML/IMSC parser with styling support
121
- const pRegex = /<p[^>]*>(.*?)<\/p>/gs;
122
- const matches = [...text.matchAll(pRegex)];
123
- for (let i = 0; i < matches.length; i++) {
124
- const match = matches[i];
125
- const content = match[1]?.replace(/<[^>]+>/g, '') || ''; // Strip inner tags
126
- const meta = {};
127
- if (i === 0) {
128
- meta.config = { description: '' };
129
- }
130
- const cue = {
131
- timestamp: 0,
132
- duration: 0,
133
- text: content.trim(),
134
- };
135
- this.options.output(cue, meta);
136
- }
137
- }
138
- }
139
- const timestampRegex = /(?:(\d{2}):)?(\d{2}):(\d{2}).(\d{3})/;
140
- /**
141
- * Parses a WebVTT timestamp string to milliseconds.
142
- * @group Media sources
143
- * @internal
144
- */
145
- export const parseSubtitleTimestamp = (string) => {
146
- const match = timestampRegex.exec(string);
147
- if (!match)
148
- throw new Error('Expected match.');
149
- return 60 * 60 * 1000 * Number(match[1] || '0')
150
- + 60 * 1000 * Number(match[2])
151
- + 1000 * Number(match[3])
152
- + Number(match[4]);
153
- };
154
- /**
155
- * Formats milliseconds to WebVTT timestamp format.
156
- * @group Media sources
157
- * @internal
158
- */
159
- export const formatSubtitleTimestamp = (timestamp) => {
160
- const hours = Math.floor(timestamp / (60 * 60 * 1000));
161
- const minutes = Math.floor((timestamp % (60 * 60 * 1000)) / (60 * 1000));
162
- const seconds = Math.floor((timestamp % (60 * 1000)) / 1000);
163
- const milliseconds = timestamp % 1000;
164
- return hours.toString().padStart(2, '0') + ':'
165
- + minutes.toString().padStart(2, '0') + ':'
166
- + seconds.toString().padStart(2, '0') + '.'
167
- + milliseconds.toString().padStart(3, '0');
168
- };
169
- // SRT parsing functions
170
- const srtTimestampRegex = /(\d{2}):(\d{2}):(\d{2}),(\d{3})/;
171
- /**
172
- * Parses an SRT timestamp string (HH:MM:SS,mmm) to seconds.
173
- * @group Media sources
174
- * @public
175
- */
176
- export const parseSrtTimestamp = (timeString) => {
177
- const match = srtTimestampRegex.exec(timeString);
178
- if (!match)
179
- throw new Error('Invalid SRT timestamp format');
180
- const hours = Number(match[1]);
181
- const minutes = Number(match[2]);
182
- const seconds = Number(match[3]);
183
- const milliseconds = Number(match[4]);
184
- return hours * 3600 + minutes * 60 + seconds + milliseconds / 1000;
185
- };
186
- /**
187
- * Formats seconds to SRT timestamp format (HH:MM:SS,mmm).
188
- * @group Media sources
189
- * @public
190
- */
191
- export const formatSrtTimestamp = (seconds) => {
192
- const hours = Math.floor(seconds / 3600);
193
- const minutes = Math.floor((seconds % 3600) / 60);
194
- const secs = Math.floor(seconds % 60);
195
- const milliseconds = Math.round((seconds % 1) * 1000);
196
- return hours.toString().padStart(2, '0') + ':'
197
- + minutes.toString().padStart(2, '0') + ':'
198
- + secs.toString().padStart(2, '0') + ','
199
- + milliseconds.toString().padStart(3, '0');
200
- };
201
- /**
202
- * Splits SRT subtitle text into individual cues.
203
- * @group Media sources
204
- * @public
205
- */
206
- export const splitSrtIntoCues = (text) => {
207
- text = text.replaceAll('\r\n', '\n').replaceAll('\r', '\n');
208
- const cues = [];
209
- const cueRegex = /(\d+)\n(\d{2}:\d{2}:\d{2},\d{3})\s+-->\s+(\d{2}:\d{2}:\d{2},\d{3})\n([\s\S]*?)(?=\n\n\d+\n|\n*$)/g;
210
- let match;
211
- while ((match = cueRegex.exec(text))) {
212
- const startTime = parseSrtTimestamp(match[2]);
213
- const endTime = parseSrtTimestamp(match[3]);
214
- const cueText = match[4].trim();
215
- cues.push({
216
- timestamp: startTime,
217
- duration: endTime - startTime,
218
- text: cueText,
219
- identifier: match[1],
220
- });
221
- }
222
- return cues;
223
- };
224
- /**
225
- * Extracts plain text from ASS/SSA Dialogue/Comment line.
226
- * If the text is already plain (not ASS format), returns as-is.
227
- */
228
- const extractTextFromAssCue = (text) => {
229
- // Check if this is an ASS Dialogue/Comment line
230
- if (text.startsWith('Dialogue:') || text.startsWith('Comment:')) {
231
- // ASS format: Dialogue: Layer,Start,End,Style,Name,MarginL,MarginR,MarginV,Effect,Text
232
- // We need to extract the last field (Text) which may contain commas
233
- const colonIndex = text.indexOf(':');
234
- if (colonIndex === -1)
235
- return text;
236
- const afterColon = text.substring(colonIndex + 1);
237
- const parts = afterColon.split(',');
238
- // Text is the 10th field (index 9), but it may contain commas
239
- // So we need to join everything from index 9 onward
240
- if (parts.length >= 10) {
241
- return parts.slice(9).join(',');
242
- }
243
- }
244
- // Check if this is MKV ASS format (without Dialogue: prefix)
245
- // MKV format: ReadOrder,Layer,Style,Name,MarginL,MarginR,MarginV,Effect,Text
246
- // OR: Layer,Style,Name,MarginL,MarginR,MarginV,Effect,Text
247
- const parts = text.split(',');
248
- if (parts.length >= 8) {
249
- const firstPart = parts[0]?.trim();
250
- const secondPart = parts[1]?.trim();
251
- // Check if first field is numeric (Layer or ReadOrder)
252
- if (firstPart && !isNaN(parseInt(firstPart))) {
253
- // Check if second field is also numeric (ReadOrder,Layer format)
254
- if (secondPart && !isNaN(parseInt(secondPart)) && parts.length >= 9) {
255
- // MKV format with ReadOrder: text is 9th field (index 8) onward
256
- return parts.slice(8).join(',');
257
- }
258
- else if (parts.length >= 8) {
259
- // Standard ASS format without ReadOrder: text is 8th field (index 7) onward
260
- return parts.slice(7).join(',');
261
- }
262
- }
263
- }
264
- // Not ASS format, return as-is
265
- return text;
266
- };
267
- /**
268
- * Formats subtitle cues back to SRT text format.
269
- * @group Media sources
270
- * @public
271
- */
272
- export const formatCuesToSrt = (cues) => {
273
- return cues.map((cue, index) => {
274
- const sequenceNumber = index + 1;
275
- const startTime = formatSrtTimestamp(cue.timestamp);
276
- const endTime = formatSrtTimestamp(cue.timestamp + cue.duration);
277
- const text = extractTextFromAssCue(cue.text);
278
- return `${sequenceNumber}\n${startTime} --> ${endTime}\n${text}\n`;
279
- }).join('\n');
280
- };
281
- /**
282
- * Formats subtitle cues back to WebVTT text format.
283
- * @group Media sources
284
- * @public
285
- */
286
- export const formatCuesToWebVTT = (cues, preamble) => {
287
- // Start with the WebVTT header
288
- let result = preamble || 'WEBVTT\n';
289
- // Ensure there's a blank line after the header
290
- if (!result.endsWith('\n\n')) {
291
- result += '\n';
292
- }
293
- // Format each cue
294
- const formattedCues = cues.map((cue) => {
295
- const startTime = formatSubtitleTimestamp(cue.timestamp * 1000); // Convert to milliseconds
296
- const endTime = formatSubtitleTimestamp((cue.timestamp + cue.duration) * 1000);
297
- const text = extractTextFromAssCue(cue.text);
298
- // WebVTT doesn't require sequence numbers like SRT
299
- return `${startTime} --> ${endTime}\n${text}`;
300
- });
301
- return result + formattedCues.join('\n\n');
302
- };
303
- // ASS/SSA parsing functions
304
- const assTimestampRegex = /(\d+):(\d{2}):(\d{2})\.(\d{2})/;
305
- /**
306
- * Parses an ASS/SSA timestamp string (H:MM:SS.cc) to seconds.
307
- * @group Media sources
308
- * @public
309
- */
310
- export const parseAssTimestamp = (timeString) => {
311
- const match = assTimestampRegex.exec(timeString);
312
- if (!match)
313
- throw new Error('Invalid ASS timestamp format');
314
- const hours = Number(match[1]);
315
- const minutes = Number(match[2]);
316
- const seconds = Number(match[3]);
317
- const centiseconds = Number(match[4]);
318
- return hours * 3600 + minutes * 60 + seconds + centiseconds / 100;
319
- };
320
- /**
321
- * Formats seconds to ASS/SSA timestamp format (H:MM:SS.cc).
322
- * @group Media sources
323
- * @public
324
- */
325
- export const formatAssTimestamp = (seconds) => {
326
- const hours = Math.floor(seconds / 3600);
327
- const minutes = Math.floor((seconds % 3600) / 60);
328
- const secs = Math.floor(seconds % 60);
329
- const centiseconds = Math.floor((seconds % 1) * 100);
330
- return hours.toString() + ':'
331
- + minutes.toString().padStart(2, '0') + ':'
332
- + secs.toString().padStart(2, '0') + '.'
333
- + centiseconds.toString().padStart(2, '0');
334
- };
335
- /**
336
- * Splits ASS/SSA subtitle text into header (styles) and individual cues.
337
- * Preserves all sections including [Fonts], [Graphics], and Aegisub sections.
338
- * Aegisub sections are moved to the end to avoid breaking [Events].
339
- * @group Media sources
340
- * @public
341
- */
342
- export const splitAssIntoCues = (text) => {
343
- text = text.replaceAll('\r\n', '\n').replaceAll('\r', '\n');
344
- const lines = text.split('\n');
345
- // Find [Events] section
346
- const eventsIndex = lines.findIndex(line => line.trim() === '[Events]');
347
- if (eventsIndex === -1) {
348
- return { header: text, cues: [] };
349
- }
350
- // Separate sections for proper ordering
351
- const headerSections = []; // [Script Info], [V4+ Styles], etc. (before Events)
352
- const eventsHeader = []; // [Events] and Format: line
353
- const eventLines = []; // Dialogue/Comment lines
354
- const postEventsSections = []; // [Fonts], [Graphics], [Aegisub...] (after Events)
355
- let currentSection = headerSections;
356
- let inEventsSection = false;
357
- for (let i = 0; i < lines.length; i++) {
358
- const line = lines[i];
359
- // Section header
360
- if (line && line.startsWith('[') && line.endsWith(']')) {
361
- const trimmedLine = line.trim();
362
- if (trimmedLine === '[Events]') {
363
- inEventsSection = true;
364
- eventsHeader.push(line);
365
- continue;
366
- }
367
- // Any section after [Events] goes to post-events
368
- if (inEventsSection) {
369
- currentSection = postEventsSections;
370
- inEventsSection = false;
371
- }
372
- currentSection.push(line);
373
- continue;
374
- }
375
- if (inEventsSection) {
376
- if (!line) {
377
- continue; // Skip empty lines in Events
378
- }
379
- if (line.startsWith('Format:')) {
380
- eventsHeader.push(line);
381
- }
382
- else if (line.startsWith('Dialogue:')) {
383
- // Dialogue lines go to eventLines (will be reconstructed with timestamps from blocks)
384
- eventLines.push(line);
385
- }
386
- else if (line.startsWith('Comment:')) {
387
- // Comment lines stay in header (they're metadata, not in MKV blocks)
388
- eventsHeader.push(line);
389
- }
390
- }
391
- else {
392
- if (line !== undefined) {
393
- currentSection.push(line);
394
- }
395
- }
396
- }
397
- // Build header: everything except Dialogue lines (keep Comments)
398
- // Format: [Header Sections] + [Events] + Format + Comments + [Post-Events Sections]
399
- const header = [
400
- ...headerSections,
401
- ...eventsHeader, // Includes [Events], Format:, and Comment: lines
402
- ...postEventsSections,
403
- ].join('\n');
404
- // Parse Comment and Dialogue lines
405
- const cues = [];
406
- for (const line of eventLines) {
407
- // Parse ASS dialogue/comment format
408
- // Dialogue: Layer,Start,End,Style,Name,MarginL,MarginR,MarginV,Effect,Text
409
- const colonIndex = line.indexOf(':');
410
- if (colonIndex === -1)
411
- continue;
412
- const parts = line.substring(colonIndex + 1).split(',');
413
- if (parts.length < 10)
414
- continue;
415
- try {
416
- const startTime = parseAssTimestamp(parts[1].trim());
417
- const endTime = parseAssTimestamp(parts[2].trim());
418
- cues.push({
419
- timestamp: startTime,
420
- duration: endTime - startTime,
421
- text: line, // Store the entire line (Dialogue: or Comment:)
422
- });
423
- }
424
- catch {
425
- // Skip malformed lines
426
- continue;
427
- }
428
- }
429
- return { header, cues };
430
- };
431
- /**
432
- * Parses ASS Format line to get field order.
433
- * Returns map of field name to index.
434
- */
435
- const parseAssFormat = (formatLine) => {
436
- // Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
437
- const fields = formatLine
438
- .substring(formatLine.indexOf(':') + 1)
439
- .split(',')
440
- .map(f => f.trim());
441
- const fieldMap = new Map();
442
- fields.forEach((field, index) => {
443
- fieldMap.set(field, index);
444
- });
445
- return fieldMap;
446
- };
447
- /**
448
- * Converts a full Dialogue/Comment line to MKV block format.
449
- * @group Media sources
450
- * @internal
451
- */
452
- export const convertDialogueLineToMkvFormat = (line) => {
453
- const match = /^(Dialogue|Comment):\s*(\d+),\d+:\d{2}:\d{2}\.\d{2},\d+:\d{2}:\d{2}\.\d{2},(.*)$/.exec(line);
454
- if (match) {
455
- const layer = match[2];
456
- const restFields = match[3];
457
- return `${layer},${restFields}`;
458
- }
459
- if (line.startsWith('Dialogue:') || line.startsWith('Comment:')) {
460
- return line.substring(line.indexOf(':') + 1).trim();
461
- }
462
- return line;
463
- };
464
- /**
465
- * Formats subtitle cues back to ASS/SSA text format with header.
466
- * Properly inserts Dialogue/Comment lines within [Events] section.
467
- * @group Media sources
468
- * @public
469
- */
470
- export const formatCuesToAss = (cues, header) => {
471
- // If header is empty or missing, create a default ASS header
472
- if (!header || header.trim() === '') {
473
- header = `[Script Info]
474
- Title: Default
475
- ScriptType: v4.00+
476
-
477
- [V4+ Styles]
478
- Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
479
- Style: Default,Arial,20,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,2,2,10,10,10,1
480
-
481
- [Events]
482
- Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text`;
483
- }
484
- // Find [Events] section and its Format line
485
- const headerLines = header.split('\n');
486
- const eventsIndex = headerLines.findIndex(line => line.trim() === '[Events]');
487
- if (eventsIndex === -1) {
488
- // No [Events] section, create one
489
- return header + `\n\n[Events]\nFormat: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n` + cues.map(c => c.text).join('\n');
490
- }
491
- // Find Format line AFTER [Events]
492
- let formatIndex = -1;
493
- let formatLine = '';
494
- for (let i = eventsIndex + 1; i < headerLines.length; i++) {
495
- const line = headerLines[i];
496
- if (line && line.trim().startsWith('Format:')) {
497
- formatIndex = i;
498
- formatLine = line;
499
- break;
500
- }
501
- // Stop if we hit another section
502
- if (line && line.startsWith('[') && line.endsWith(']')) {
503
- break;
504
- }
505
- }
506
- // Parse format to understand field order
507
- const fieldMap = formatLine ? parseAssFormat(formatLine) : null;
508
- // Reconstruct dialogue lines with proper field order
509
- const dialogueLines = cues.map(cue => {
510
- // If text already has full Dialogue/Comment line with timestamps, use as-is
511
- if (cue.text.startsWith('Dialogue:') || cue.text.startsWith('Comment:')) {
512
- if (/^(Dialogue|Comment):\s*\d+,\d+:\d{2}:\d{2}\.\d{2},\d+:\d{2}:\d{2}\.\d{2},/.test(cue.text)) {
513
- return cue.text;
514
- }
515
- }
516
- // Parse MKV block data or plain text
517
- let params = cue.text;
518
- const isComment = params.startsWith('Comment:');
519
- const prefix = isComment ? 'Comment:' : 'Dialogue:';
520
- if (params.startsWith('Dialogue:') || params.startsWith('Comment:')) {
521
- params = params.substring(params.indexOf(':') + 1).trim();
522
- }
523
- const parts = params.split(',');
524
- const startTime = formatAssTimestamp(cue.timestamp);
525
- const endTime = formatAssTimestamp(cue.timestamp + cue.duration);
526
- let layer;
527
- let restFields;
528
- // Detect ReadOrder format from actual block data first
529
- // MKV blocks: ReadOrder,Layer,Style,... (9+ fields, first two numeric) OR Layer,Style,... (8+ fields, first numeric)
530
- const blockHasReadOrder = parts.length >= 9 && !isNaN(parseInt(parts[0])) && !isNaN(parseInt(parts[1]));
531
- const blockHasLayer = parts.length >= 8 && !isNaN(parseInt(parts[0]));
532
- if (blockHasReadOrder) {
533
- layer = parts[1] || '0';
534
- restFields = parts.slice(2);
535
- }
536
- else if (blockHasLayer) {
537
- layer = parts[0] || '0';
538
- restFields = parts.slice(1);
539
- }
540
- else {
541
- return `${prefix} 0,${startTime},${endTime},Default,,0,0,0,,${cue.text}`;
542
- }
543
- return `${prefix} ${layer},${startTime},${endTime},${restFields.join(',')}`;
544
- });
545
- if (formatIndex === -1) {
546
- // No Format line found, just append
547
- return header + '\n' + dialogueLines.join('\n');
548
- }
549
- // Find Comment lines and next section after [Events]
550
- const commentLines = [];
551
- let nextSectionIndex = headerLines.length;
552
- for (let i = formatIndex + 1; i < headerLines.length; i++) {
553
- const line = headerLines[i];
554
- if (line && line.startsWith('Comment:')) {
555
- commentLines.push(line);
556
- }
557
- if (line && line.startsWith('[') && line.endsWith(']')) {
558
- nextSectionIndex = i;
559
- break;
560
- }
561
- }
562
- // Build final structure:
563
- // 1. Everything up to and including Format line
564
- // 2. All Dialogue lines
565
- // 3. All Comment lines (at the end of Events)
566
- // 4. Everything after Events section
567
- const beforeDialogues = headerLines.slice(0, formatIndex + 1);
568
- const afterDialogues = headerLines.slice(nextSectionIndex);
569
- return [
570
- ...beforeDialogues,
571
- ...dialogueLines,
572
- ...commentLines,
573
- ...afterDialogues,
574
- ].join('\n');
575
- };