@kenzuya/mediabunny 1.26.0 → 1.28.5

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