@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,1276 +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 {
10
- Bitstream,
11
- COLOR_PRIMARIES_MAP,
12
- MATRIX_COEFFICIENTS_MAP,
13
- TRANSFER_CHARACTERISTICS_MAP,
14
- UNDETERMINED_LANGUAGE,
15
- assert,
16
- assertNever,
17
- colorSpaceIsComplete,
18
- imageMimeTypeToExtension,
19
- keyValueIterator,
20
- normalizeRotation,
21
- promiseWithResolvers,
22
- roundToMultiple,
23
- textEncoder,
24
- toUint8Array,
25
- uint8ArraysAreEqual,
26
- writeBits,
27
- } from '../misc';
28
- import {
29
- CODEC_STRING_MAP,
30
- EBML,
31
- EBMLElement,
32
- EBMLFloat32,
33
- EBMLFloat64,
34
- EBMLId,
35
- EBMLSignedInt,
36
- EBMLUnicodeString,
37
- EBMLWriter,
38
- } from './ebml';
39
- import { buildMatroskaMimeType } from './matroska-misc';
40
- import { MkvOutputFormat, WebMOutputFormat } from '../output-format';
41
- import { Output, OutputAudioTrack, OutputSubtitleTrack, OutputTrack, OutputVideoTrack } from '../output';
42
- import {
43
- SubtitleConfig,
44
- SubtitleCue,
45
- SubtitleMetadata,
46
- formatSubtitleTimestamp,
47
- inlineTimestampRegex,
48
- parseSubtitleTimestamp,
49
- convertDialogueLineToMkvFormat,
50
- } from '../subtitles';
51
- import {
52
- OPUS_SAMPLE_RATE,
53
- PCM_AUDIO_CODECS,
54
- PcmAudioCodec,
55
- SubtitleCodec,
56
- generateAv1CodecConfigurationFromCodecString,
57
- generateVp9CodecConfigurationFromCodecString,
58
- parsePcmCodec,
59
- validateAudioChunkMetadata,
60
- validateSubtitleMetadata,
61
- validateVideoChunkMetadata,
62
- } from '../codec';
63
- import { Muxer } from '../muxer';
64
- import { Writer } from '../writer';
65
- import { EncodedPacket } from '../packet';
66
- import { parseOpusIdentificationHeader } from '../codec-data';
67
- import { AttachedFile } from '../metadata';
68
-
69
- const MIN_CLUSTER_TIMESTAMP_MS = /* #__PURE__ */ -(2 ** 15);
70
- const MAX_CLUSTER_TIMESTAMP_MS = /* #__PURE__ */ 2 ** 15 - 1;
71
- const APP_NAME = 'Mediabunny';
72
- const SEGMENT_SIZE_BYTES = 6;
73
- const CLUSTER_SIZE_BYTES = 5;
74
-
75
- type InternalMediaChunk = {
76
- data: Uint8Array;
77
- type: 'key' | 'delta';
78
- timestamp: number;
79
- duration: number;
80
- additions: Uint8Array | null;
81
- };
82
-
83
- type MatroskaTrackData = {
84
- chunkQueue: InternalMediaChunk[];
85
- lastWrittenMsTimestamp: number | null;
86
- } & ({
87
- track: OutputVideoTrack;
88
- type: 'video';
89
- info: {
90
- width: number;
91
- height: number;
92
- decoderConfig: VideoDecoderConfig;
93
- alphaMode: boolean;
94
- };
95
- } | {
96
- track: OutputAudioTrack;
97
- type: 'audio';
98
- info: {
99
- numberOfChannels: number;
100
- sampleRate: number;
101
- decoderConfig: AudioDecoderConfig;
102
- };
103
- } | {
104
- track: OutputSubtitleTrack;
105
- type: 'subtitle';
106
- info: {
107
- config: SubtitleConfig;
108
- };
109
- });
110
-
111
- type MatroskaVideoTrackData = MatroskaTrackData & { type: 'video' };
112
- type MatroskaAudioTrackData = MatroskaTrackData & { type: 'audio' };
113
- type MatroskaSubtitleTrackData = MatroskaTrackData & { type: 'subtitle' };
114
-
115
- const TRACK_TYPE_MAP: Record<OutputTrack['type'], number> = {
116
- video: 1,
117
- audio: 2,
118
- subtitle: 17,
119
- };
120
-
121
- export class MatroskaMuxer extends Muxer {
122
- private writer: Writer;
123
- private ebmlWriter: EBMLWriter;
124
- private format: WebMOutputFormat | MkvOutputFormat;
125
-
126
- private trackDatas: MatroskaTrackData[] = [];
127
- private allTracksKnown = promiseWithResolvers();
128
-
129
- private segment: EBMLElement | null = null;
130
- private segmentInfo: EBMLElement | null = null;
131
- private seekHead: EBMLElement | null = null;
132
- private tracksElement: EBMLElement | null = null;
133
- private tagsElement: EBMLElement | null = null;
134
- private attachmentsElement: EBMLElement | null = null;
135
- private segmentDuration: EBMLElement | null = null;
136
- private cues: EBMLElement | null = null;
137
-
138
- private currentCluster: EBMLElement | null = null;
139
- private currentClusterStartMsTimestamp: number | null = null;
140
- private currentClusterMaxMsTimestamp: number | null = null;
141
- private trackDatasInCurrentCluster = new Map<MatroskaTrackData, {
142
- firstMsTimestamp: number;
143
- }>();
144
-
145
- private duration = 0;
146
-
147
- constructor(output: Output, format: MkvOutputFormat) {
148
- super(output);
149
-
150
- this.writer = output._writer;
151
- this.format = format;
152
-
153
- this.ebmlWriter = new EBMLWriter(this.writer);
154
-
155
- if (this.format._options.appendOnly) {
156
- this.writer.ensureMonotonicity = true;
157
- }
158
- }
159
-
160
- async start() {
161
- const release = await this.mutex.acquire();
162
-
163
- this.writeEBMLHeader();
164
-
165
- this.createSegmentInfo();
166
- this.createCues();
167
-
168
- await this.writer.flush();
169
-
170
- release();
171
- }
172
-
173
- private writeEBMLHeader() {
174
- if (this.format._options.onEbmlHeader) {
175
- this.writer.startTrackingWrites();
176
- }
177
-
178
- const ebmlHeader: EBML = { id: EBMLId.EBML, data: [
179
- { id: EBMLId.EBMLVersion, data: 1 },
180
- { id: EBMLId.EBMLReadVersion, data: 1 },
181
- { id: EBMLId.EBMLMaxIDLength, data: 4 },
182
- { id: EBMLId.EBMLMaxSizeLength, data: 8 },
183
- { id: EBMLId.DocType, data: this.format instanceof WebMOutputFormat ? 'webm' : 'matroska' },
184
- { id: EBMLId.DocTypeVersion, data: 2 },
185
- { id: EBMLId.DocTypeReadVersion, data: 2 },
186
- ] };
187
- this.ebmlWriter.writeEBML(ebmlHeader);
188
-
189
- if (this.format._options.onEbmlHeader) {
190
- const { data, start } = this.writer.stopTrackingWrites(); // start should be 0
191
- this.format._options.onEbmlHeader(data, start);
192
- }
193
- }
194
-
195
- /**
196
- * Creates a SeekHead element which is positioned near the start of the file and allows the media player to seek to
197
- * relevant sections more easily. Since we don't know the positions of those sections yet, we'll set them later.
198
- */
199
- private maybeCreateSeekHead(writeOffsets: boolean) {
200
- if (this.format._options.appendOnly) {
201
- return;
202
- }
203
-
204
- const kaxCues = new Uint8Array([0x1c, 0x53, 0xbb, 0x6b]);
205
- const kaxInfo = new Uint8Array([0x15, 0x49, 0xa9, 0x66]);
206
- const kaxTracks = new Uint8Array([0x16, 0x54, 0xae, 0x6b]);
207
- const kaxAttachments = new Uint8Array([0x19, 0x41, 0xa4, 0x69]);
208
- const kaxTags = new Uint8Array([0x12, 0x54, 0xc3, 0x67]);
209
-
210
- const seekHead = { id: EBMLId.SeekHead, data: [
211
- { id: EBMLId.Seek, data: [
212
- { id: EBMLId.SeekID, data: kaxCues },
213
- {
214
- id: EBMLId.SeekPosition,
215
- size: 5,
216
- data: writeOffsets
217
- ? this.ebmlWriter.offsets.get(this.cues!)! - this.segmentDataOffset
218
- : 0,
219
- },
220
- ] },
221
- { id: EBMLId.Seek, data: [
222
- { id: EBMLId.SeekID, data: kaxInfo },
223
- {
224
- id: EBMLId.SeekPosition,
225
- size: 5,
226
- data: writeOffsets
227
- ? this.ebmlWriter.offsets.get(this.segmentInfo!)! - this.segmentDataOffset
228
- : 0,
229
- },
230
- ] },
231
- { id: EBMLId.Seek, data: [
232
- { id: EBMLId.SeekID, data: kaxTracks },
233
- {
234
- id: EBMLId.SeekPosition,
235
- size: 5,
236
- data: writeOffsets
237
- ? this.ebmlWriter.offsets.get(this.tracksElement!)! - this.segmentDataOffset
238
- : 0,
239
- },
240
- ] },
241
- this.attachmentsElement
242
- ? { id: EBMLId.Seek, data: [
243
- { id: EBMLId.SeekID, data: kaxAttachments },
244
- {
245
- id: EBMLId.SeekPosition,
246
- size: 5,
247
- data: writeOffsets
248
- ? this.ebmlWriter.offsets.get(this.attachmentsElement)! - this.segmentDataOffset
249
- : 0,
250
- },
251
- ] }
252
- : null,
253
- this.tagsElement
254
- ? { id: EBMLId.Seek, data: [
255
- { id: EBMLId.SeekID, data: kaxTags },
256
- {
257
- id: EBMLId.SeekPosition,
258
- size: 5,
259
- data: writeOffsets
260
- ? this.ebmlWriter.offsets.get(this.tagsElement)! - this.segmentDataOffset
261
- : 0,
262
- },
263
- ] }
264
- : null,
265
- ] };
266
- this.seekHead = seekHead;
267
- }
268
-
269
- private createSegmentInfo() {
270
- const segmentDuration: EBML = { id: EBMLId.Duration, data: new EBMLFloat64(0) };
271
- this.segmentDuration = segmentDuration;
272
-
273
- const segmentInfo: EBML = { id: EBMLId.Info, data: [
274
- { id: EBMLId.TimestampScale, data: 1e6 },
275
- { id: EBMLId.MuxingApp, data: APP_NAME },
276
- { id: EBMLId.WritingApp, data: APP_NAME },
277
- !this.format._options.appendOnly ? segmentDuration : null,
278
- ] };
279
- this.segmentInfo = segmentInfo;
280
- }
281
-
282
- private createTracks() {
283
- const tracksElement = { id: EBMLId.Tracks, data: [] as EBML[] };
284
- this.tracksElement = tracksElement;
285
-
286
- for (const trackData of this.trackDatas) {
287
- const codecId = CODEC_STRING_MAP[trackData.track.source._codec];
288
- assert(codecId);
289
-
290
- let seekPreRollNs = 0;
291
- if (trackData.type === 'audio' && trackData.track.source._codec === 'opus') {
292
- seekPreRollNs = 1e6 * 80; // In "Matroska ticks" (nanoseconds)
293
-
294
- const description = trackData.info.decoderConfig.description;
295
- if (description) {
296
- const bytes = toUint8Array(description);
297
- const header = parseOpusIdentificationHeader(bytes);
298
-
299
- // Use the preSkip value from the header
300
- seekPreRollNs = Math.round(1e9 * (header.preSkip / OPUS_SAMPLE_RATE));
301
- }
302
- }
303
-
304
- tracksElement.data.push({ id: EBMLId.TrackEntry, data: [
305
- { id: EBMLId.TrackNumber, data: trackData.track.id },
306
- { id: EBMLId.TrackUID, data: trackData.track.id },
307
- { id: EBMLId.TrackType, data: TRACK_TYPE_MAP[trackData.type] },
308
- trackData.track.metadata.disposition?.default === false
309
- ? { id: EBMLId.FlagDefault, data: 0 }
310
- : null,
311
- trackData.track.metadata.disposition?.forced
312
- ? { id: EBMLId.FlagForced, data: 1 }
313
- : null,
314
- trackData.track.metadata.disposition?.hearingImpaired
315
- ? { id: EBMLId.FlagHearingImpaired, data: 1 }
316
- : null,
317
- trackData.track.metadata.disposition?.visuallyImpaired
318
- ? { id: EBMLId.FlagVisualImpaired, data: 1 }
319
- : null,
320
- trackData.track.metadata.disposition?.original
321
- ? { id: EBMLId.FlagOriginal, data: 1 }
322
- : null,
323
- trackData.track.metadata.disposition?.commentary
324
- ? { id: EBMLId.FlagCommentary, data: 1 }
325
- : null,
326
- { id: EBMLId.FlagLacing, data: 0 },
327
- { id: EBMLId.Language, data: trackData.track.metadata.languageCode ?? UNDETERMINED_LANGUAGE },
328
- { id: EBMLId.CodecID, data: codecId },
329
- { id: EBMLId.CodecDelay, data: 0 },
330
- { id: EBMLId.SeekPreRoll, data: seekPreRollNs },
331
- trackData.track.metadata.name !== undefined
332
- ? { id: EBMLId.Name, data: new EBMLUnicodeString(trackData.track.metadata.name) }
333
- : null,
334
- (trackData.type === 'video' ? this.videoSpecificTrackInfo(trackData) : null),
335
- (trackData.type === 'audio' ? this.audioSpecificTrackInfo(trackData) : null),
336
- (trackData.type === 'subtitle' ? this.subtitleSpecificTrackInfo(trackData) : null),
337
- ] });
338
- }
339
- }
340
-
341
- private videoSpecificTrackInfo(trackData: MatroskaVideoTrackData) {
342
- const { frameRate, rotation } = trackData.track.metadata;
343
-
344
- const elements: EBMLElement['data'] = [
345
- (trackData.info.decoderConfig.description
346
- ? {
347
- id: EBMLId.CodecPrivate,
348
- data: toUint8Array(trackData.info.decoderConfig.description),
349
- }
350
- : null),
351
- (frameRate
352
- ? {
353
- id: EBMLId.DefaultDuration,
354
- data: 1e9 / frameRate,
355
- }
356
- : null),
357
- ];
358
-
359
- // Convert from clockwise to counter-clockwise
360
- const flippedRotation = rotation ? normalizeRotation(-rotation) : 0;
361
-
362
- const colorSpace = trackData.info.decoderConfig.colorSpace;
363
- const videoElement: EBMLElement = { id: EBMLId.Video, data: [
364
- { id: EBMLId.PixelWidth, data: trackData.info.width },
365
- { id: EBMLId.PixelHeight, data: trackData.info.height },
366
- trackData.info.alphaMode ? { id: EBMLId.AlphaMode, data: 1 } : null,
367
- (colorSpaceIsComplete(colorSpace)
368
- ? {
369
- id: EBMLId.Colour,
370
- data: [
371
- {
372
- id: EBMLId.MatrixCoefficients,
373
- data: MATRIX_COEFFICIENTS_MAP[colorSpace.matrix],
374
- },
375
- {
376
- id: EBMLId.TransferCharacteristics,
377
- data: TRANSFER_CHARACTERISTICS_MAP[colorSpace.transfer],
378
- },
379
- {
380
- id: EBMLId.Primaries,
381
- data: COLOR_PRIMARIES_MAP[colorSpace.primaries],
382
- },
383
- {
384
- id: EBMLId.Range,
385
- data: colorSpace.fullRange ? 2 : 1,
386
- },
387
- ],
388
- }
389
- : null),
390
- (flippedRotation
391
- ? {
392
- id: EBMLId.Projection,
393
- data: [
394
- {
395
- id: EBMLId.ProjectionType,
396
- data: 0, // rectangular
397
- },
398
- {
399
- id: EBMLId.ProjectionPoseRoll,
400
- data: new EBMLFloat32((flippedRotation + 180) % 360 - 180), // [0, 270] -> [-180, 90]
401
- },
402
- ],
403
- }
404
- : null),
405
- ] };
406
-
407
- elements.push(videoElement);
408
-
409
- return elements;
410
- }
411
-
412
- private audioSpecificTrackInfo(trackData: MatroskaAudioTrackData) {
413
- const pcmInfo = (PCM_AUDIO_CODECS as readonly string[]).includes(trackData.track.source._codec)
414
- ? parsePcmCodec(trackData.track.source._codec as PcmAudioCodec)
415
- : null;
416
-
417
- return [
418
- (trackData.info.decoderConfig.description
419
- ? {
420
- id: EBMLId.CodecPrivate,
421
- data: toUint8Array(trackData.info.decoderConfig.description),
422
- }
423
- : null),
424
- { id: EBMLId.Audio, data: [
425
- { id: EBMLId.SamplingFrequency, data: new EBMLFloat32(trackData.info.sampleRate) },
426
- { id: EBMLId.Channels, data: trackData.info.numberOfChannels },
427
- pcmInfo ? { id: EBMLId.BitDepth, data: 8 * pcmInfo.sampleSize } : null,
428
- ] },
429
- ];
430
- }
431
-
432
- private subtitleSpecificTrackInfo(trackData: MatroskaSubtitleTrackData) {
433
- return [
434
- { id: EBMLId.CodecPrivate, data: textEncoder.encode(trackData.info.config.description) },
435
- ];
436
- }
437
-
438
- private maybeCreateTags() {
439
- const simpleTags: EBMLElement[] = [];
440
-
441
- const addSimpleTag = (key: string, value: string | Uint8Array) => {
442
- simpleTags.push({ id: EBMLId.SimpleTag, data: [
443
- { id: EBMLId.TagName, data: new EBMLUnicodeString(key) },
444
- typeof value === 'string'
445
- ? { id: EBMLId.TagString, data: new EBMLUnicodeString(value) }
446
- : { id: EBMLId.TagBinary, data: value },
447
- ] });
448
- };
449
-
450
- const metadataTags = this.output._metadataTags;
451
- const writtenTags = new Set<string>();
452
-
453
- for (const { key, value } of keyValueIterator(metadataTags)) {
454
- switch (key) {
455
- case 'title': {
456
- addSimpleTag('TITLE', value);
457
- writtenTags.add('TITLE');
458
- }; break;
459
-
460
- case 'description': {
461
- addSimpleTag('DESCRIPTION', value);
462
- writtenTags.add('DESCRIPTION');
463
- }; break;
464
-
465
- case 'artist': {
466
- addSimpleTag('ARTIST', value);
467
- writtenTags.add('ARTIST');
468
- }; break;
469
-
470
- case 'album': {
471
- addSimpleTag('ALBUM', value);
472
- writtenTags.add('ALBUM');
473
- }; break;
474
-
475
- case 'albumArtist': {
476
- addSimpleTag('ALBUM_ARTIST', value);
477
- writtenTags.add('ALBUM_ARTIST');
478
- }; break;
479
-
480
- case 'genre': {
481
- addSimpleTag('GENRE', value);
482
- writtenTags.add('GENRE');
483
- }; break;
484
-
485
- case 'comment': {
486
- addSimpleTag('COMMENT', value);
487
- writtenTags.add('COMMENT');
488
- }; break;
489
-
490
- case 'lyrics': {
491
- addSimpleTag('LYRICS', value);
492
- writtenTags.add('LYRICS');
493
- }; break;
494
-
495
- case 'date': {
496
- addSimpleTag('DATE', value.toISOString().slice(0, 10));
497
- writtenTags.add('DATE');
498
- }; break;
499
-
500
- case 'trackNumber': {
501
- const string = metadataTags.tracksTotal !== undefined
502
- ? `${value}/${metadataTags.tracksTotal}`
503
- : value.toString();
504
-
505
- addSimpleTag('PART_NUMBER', string);
506
- writtenTags.add('PART_NUMBER');
507
- }; break;
508
-
509
- case 'discNumber': {
510
- const string = metadataTags.discsTotal !== undefined
511
- ? `${value}/${metadataTags.discsTotal}`
512
- : value.toString();
513
-
514
- addSimpleTag('DISC', string);
515
- writtenTags.add('DISC');
516
- }; break;
517
-
518
- case 'tracksTotal':
519
- case 'discsTotal': {
520
- // Handled with trackNumber and discNumber respectively
521
- }; break;
522
-
523
- case 'images':
524
- case 'raw': {
525
- // Handled elsewhere
526
- }; break;
527
-
528
- default: assertNever(key);
529
- }
530
- }
531
-
532
- if (metadataTags.raw) {
533
- for (const key in metadataTags.raw) {
534
- const value = metadataTags.raw[key]!;
535
- if (value == null || writtenTags.has(key)) {
536
- continue;
537
- }
538
-
539
- if (typeof value === 'string' || value instanceof Uint8Array) {
540
- addSimpleTag(key, value);
541
- }
542
- }
543
- }
544
-
545
- if (simpleTags.length === 0) {
546
- return;
547
- }
548
-
549
- this.tagsElement = {
550
- id: EBMLId.Tags,
551
- data: [{ id: EBMLId.Tag, data: [
552
- { id: EBMLId.Targets, data: [
553
- { id: EBMLId.TargetTypeValue, data: 50 },
554
- { id: EBMLId.TargetType, data: 'MOVIE' },
555
- ] },
556
- ...simpleTags,
557
- ] }],
558
- };
559
- }
560
-
561
- private maybeCreateAttachments() {
562
- const metadataTags = this.output._metadataTags;
563
- const elements: EBMLElement[] = [];
564
-
565
- const existingFileUids = new Set<bigint>();
566
- const images = metadataTags.images ?? [];
567
-
568
- for (const image of images) {
569
- let imageName = image.name;
570
- if (imageName === undefined) {
571
- const baseName = image.kind === 'coverFront' ? 'cover' : image.kind === 'coverBack' ? 'back' : 'image';
572
- imageName = baseName + (imageMimeTypeToExtension(image.mimeType) ?? '');
573
- }
574
-
575
- let fileUid: bigint;
576
- while (true) {
577
- // Generate a random 64-bit unsigned integer
578
- fileUid = 0n;
579
- for (let i = 0; i < 8; i++) {
580
- fileUid <<= 8n;
581
- fileUid |= BigInt(Math.floor(Math.random() * 256));
582
- }
583
-
584
- if (fileUid !== 0n && !existingFileUids.has(fileUid)) {
585
- break;
586
- }
587
- }
588
-
589
- existingFileUids.add(fileUid);
590
-
591
- elements.push({
592
- id: EBMLId.AttachedFile,
593
- data: [
594
- image.description !== undefined
595
- ? { id: EBMLId.FileDescription, data: new EBMLUnicodeString(image.description) }
596
- : null,
597
- { id: EBMLId.FileName, data: new EBMLUnicodeString(imageName) },
598
- { id: EBMLId.FileMediaType, data: image.mimeType },
599
- { id: EBMLId.FileData, data: image.data },
600
- { id: EBMLId.FileUID, data: fileUid },
601
- ],
602
- });
603
- }
604
-
605
- // Add all AttachedFiles from the raw metadata
606
- for (const [key, value] of Object.entries(metadataTags.raw ?? {})) {
607
- if (!(value instanceof AttachedFile)) {
608
- continue;
609
- }
610
-
611
- const keyIsNumeric = /^\d+$/.test(key);
612
- if (!keyIsNumeric) {
613
- continue;
614
- }
615
-
616
- if (images.find(x => x.mimeType === value.mimeType && uint8ArraysAreEqual(x.data, value.data))) {
617
- // This attached file has very likely already been added as an image above
618
- // (happens when remuxing Matroska)
619
- continue;
620
- }
621
-
622
- elements.push({
623
- id: EBMLId.AttachedFile,
624
- data: [
625
- value.description !== undefined
626
- ? { id: EBMLId.FileDescription, data: new EBMLUnicodeString(value.description) }
627
- : null,
628
- { id: EBMLId.FileName, data: new EBMLUnicodeString(value.name ?? '') },
629
- { id: EBMLId.FileMediaType, data: value.mimeType ?? '' },
630
- { id: EBMLId.FileData, data: value.data },
631
- { id: EBMLId.FileUID, data: BigInt(key) },
632
- ],
633
- });
634
- }
635
-
636
- if (elements.length === 0) {
637
- return;
638
- }
639
-
640
- this.attachmentsElement = { id: EBMLId.Attachments, data: elements };
641
- }
642
-
643
- private createSegment() {
644
- this.createTracks();
645
- this.maybeCreateTags();
646
- this.maybeCreateAttachments();
647
- this.maybeCreateSeekHead(false);
648
-
649
- const segment: EBML = {
650
- id: EBMLId.Segment,
651
- size: this.format._options.appendOnly ? -1 : SEGMENT_SIZE_BYTES,
652
- data: [
653
- this.seekHead, // null if append-only
654
- this.segmentInfo,
655
- this.tracksElement,
656
- // Matroska spec says put this at the end of the file, but I think placing it before the first cluster
657
- // makes more sense, and FFmpeg agrees (argumentum ad ffmpegum fallacy)
658
- this.attachmentsElement,
659
- this.tagsElement,
660
- ],
661
- };
662
- this.segment = segment;
663
-
664
- if (this.format._options.onSegmentHeader) {
665
- this.writer.startTrackingWrites();
666
- }
667
-
668
- this.ebmlWriter.writeEBML(segment);
669
-
670
- if (this.format._options.onSegmentHeader) {
671
- const { data, start } = this.writer.stopTrackingWrites();
672
- this.format._options.onSegmentHeader(data, start);
673
- }
674
- }
675
-
676
- private createCues() {
677
- this.cues = { id: EBMLId.Cues, data: [] };
678
- }
679
-
680
- private get segmentDataOffset() {
681
- assert(this.segment);
682
- return this.ebmlWriter.dataOffsets.get(this.segment)!;
683
- }
684
-
685
- private allTracksAreKnown() {
686
- for (const track of this.output._tracks) {
687
- if (!track.source._closed && !this.trackDatas.some(x => x.track === track)) {
688
- return false; // We haven't seen a sample from this open track yet
689
- }
690
- }
691
-
692
- return true;
693
- }
694
-
695
- async getMimeType() {
696
- await this.allTracksKnown.promise;
697
-
698
- const codecStrings = this.trackDatas.map((trackData) => {
699
- if (trackData.type === 'video') {
700
- return trackData.info.decoderConfig.codec;
701
- } else if (trackData.type === 'audio') {
702
- return trackData.info.decoderConfig.codec;
703
- } else {
704
- const map: Record<SubtitleCodec, string> = {
705
- webvtt: 'S_TEXT/WEBVTT',
706
- tx3g: 'S_TEXT/UTF8', // Matroska doesn't have tx3g, convert to SRT
707
- ttml: 'S_TEXT/WEBVTT', // Matroska doesn't have TTML, convert to WebVTT
708
- srt: 'S_TEXT/UTF8',
709
- ass: 'S_TEXT/ASS',
710
- ssa: 'S_TEXT/SSA',
711
- };
712
- return map[trackData.track.source._codec];
713
- }
714
- });
715
-
716
- return buildMatroskaMimeType({
717
- isWebM: this.format instanceof WebMOutputFormat,
718
- hasVideo: this.trackDatas.some(x => x.type === 'video'),
719
- hasAudio: this.trackDatas.some(x => x.type === 'audio'),
720
- codecStrings,
721
- });
722
- }
723
-
724
- private getVideoTrackData(track: OutputVideoTrack, packet: EncodedPacket, meta?: EncodedVideoChunkMetadata) {
725
- const existingTrackData = this.trackDatas.find(x => x.track === track);
726
- if (existingTrackData) {
727
- return existingTrackData as MatroskaVideoTrackData;
728
- }
729
-
730
- validateVideoChunkMetadata(meta);
731
-
732
- assert(meta);
733
- assert(meta.decoderConfig);
734
- assert(meta.decoderConfig.codedWidth !== undefined);
735
- assert(meta.decoderConfig.codedHeight !== undefined);
736
-
737
- const newTrackData: MatroskaVideoTrackData = {
738
- track,
739
- type: 'video',
740
- info: {
741
- width: meta.decoderConfig.codedWidth,
742
- height: meta.decoderConfig.codedHeight,
743
- decoderConfig: meta.decoderConfig,
744
- alphaMode: !!packet.sideData.alpha, // The first packet determines if this track has alpha or not
745
- },
746
- chunkQueue: [],
747
- lastWrittenMsTimestamp: null,
748
- };
749
-
750
- if (track.source._codec === 'vp9') {
751
- // https://www.webmproject.org/docs/container specifies that VP9 "SHOULD" make use of the CodecPrivate
752
- // field. Since WebCodecs makes no use of the description field for VP9, we need to derive it ourselves:
753
- newTrackData.info.decoderConfig = {
754
- ...newTrackData.info.decoderConfig,
755
- description: new Uint8Array(
756
- generateVp9CodecConfigurationFromCodecString(newTrackData.info.decoderConfig.codec),
757
- ),
758
- };
759
- } else if (track.source._codec === 'av1') {
760
- // Per https://github.com/ietf-wg-cellar/matroska-specification/blob/master/codec/av1.md, AV1 requires
761
- // CodecPrivate to be set, but WebCodecs makes no use of the description field for AV1. Thus, let's derive
762
- // it ourselves:
763
- newTrackData.info.decoderConfig = {
764
- ...newTrackData.info.decoderConfig,
765
- description: new Uint8Array(
766
- generateAv1CodecConfigurationFromCodecString(newTrackData.info.decoderConfig.codec),
767
- ),
768
- };
769
- }
770
-
771
- this.trackDatas.push(newTrackData);
772
- this.trackDatas.sort((a, b) => a.track.id - b.track.id);
773
-
774
- if (this.allTracksAreKnown()) {
775
- this.allTracksKnown.resolve();
776
- }
777
-
778
- return newTrackData;
779
- }
780
-
781
- private getAudioTrackData(track: OutputAudioTrack, meta?: EncodedAudioChunkMetadata) {
782
- const existingTrackData = this.trackDatas.find(x => x.track === track);
783
- if (existingTrackData) {
784
- return existingTrackData as MatroskaAudioTrackData;
785
- }
786
-
787
- validateAudioChunkMetadata(meta);
788
-
789
- assert(meta);
790
- assert(meta.decoderConfig);
791
-
792
- const newTrackData: MatroskaAudioTrackData = {
793
- track,
794
- type: 'audio',
795
- info: {
796
- numberOfChannels: meta.decoderConfig.numberOfChannels,
797
- sampleRate: meta.decoderConfig.sampleRate,
798
- decoderConfig: meta.decoderConfig,
799
- },
800
- chunkQueue: [],
801
- lastWrittenMsTimestamp: null,
802
- };
803
-
804
- this.trackDatas.push(newTrackData);
805
- this.trackDatas.sort((a, b) => a.track.id - b.track.id);
806
-
807
- if (this.allTracksAreKnown()) {
808
- this.allTracksKnown.resolve();
809
- }
810
-
811
- return newTrackData;
812
- }
813
-
814
- private getSubtitleTrackData(track: OutputSubtitleTrack, meta?: SubtitleMetadata) {
815
- const existingTrackData = this.trackDatas.find(x => x.track === track);
816
- if (existingTrackData) {
817
- return existingTrackData as MatroskaAudioTrackData;
818
- }
819
-
820
- validateSubtitleMetadata(meta);
821
-
822
- assert(meta);
823
- assert(meta.config);
824
-
825
- const newTrackData: MatroskaSubtitleTrackData = {
826
- track,
827
- type: 'subtitle',
828
- info: {
829
- config: meta.config,
830
- },
831
- chunkQueue: [],
832
- lastWrittenMsTimestamp: null,
833
- };
834
-
835
- this.trackDatas.push(newTrackData);
836
- this.trackDatas.sort((a, b) => a.track.id - b.track.id);
837
-
838
- if (this.allTracksAreKnown()) {
839
- this.allTracksKnown.resolve();
840
- }
841
-
842
- return newTrackData;
843
- }
844
-
845
- async addEncodedVideoPacket(track: OutputVideoTrack, packet: EncodedPacket, meta?: EncodedVideoChunkMetadata) {
846
- const release = await this.mutex.acquire();
847
-
848
- try {
849
- const trackData = this.getVideoTrackData(track, packet, meta);
850
-
851
- const isKeyFrame = packet.type === 'key';
852
- let timestamp = this.validateAndNormalizeTimestamp(trackData.track, packet.timestamp, isKeyFrame);
853
- let duration = packet.duration;
854
-
855
- if (track.metadata.frameRate !== undefined) {
856
- // Constrain the time values to the frame rate
857
- timestamp = roundToMultiple(timestamp, 1 / track.metadata.frameRate);
858
- duration = roundToMultiple(duration, 1 / track.metadata.frameRate);
859
- }
860
-
861
- const additions = trackData.info.alphaMode
862
- ? packet.sideData.alpha ?? null
863
- : null;
864
-
865
- const videoChunk = this.createInternalChunk(packet.data, timestamp, duration, packet.type, additions);
866
- if (track.source._codec === 'vp9') this.fixVP9ColorSpace(trackData, videoChunk);
867
-
868
- trackData.chunkQueue.push(videoChunk);
869
- await this.interleaveChunks();
870
- } finally {
871
- release();
872
- }
873
- }
874
-
875
- async addEncodedAudioPacket(track: OutputAudioTrack, packet: EncodedPacket, meta?: EncodedAudioChunkMetadata) {
876
- const release = await this.mutex.acquire();
877
-
878
- try {
879
- const trackData = this.getAudioTrackData(track, meta);
880
-
881
- const isKeyFrame = packet.type === 'key';
882
- const timestamp = this.validateAndNormalizeTimestamp(trackData.track, packet.timestamp, isKeyFrame);
883
- const audioChunk = this.createInternalChunk(packet.data, timestamp, packet.duration, packet.type);
884
-
885
- trackData.chunkQueue.push(audioChunk);
886
- await this.interleaveChunks();
887
- } finally {
888
- release();
889
- }
890
- }
891
-
892
- async addSubtitleCue(track: OutputSubtitleTrack, cue: SubtitleCue, meta?: SubtitleMetadata) {
893
- const release = await this.mutex.acquire();
894
-
895
- try {
896
- const trackData = this.getSubtitleTrackData(track, meta);
897
-
898
- const timestamp = this.validateAndNormalizeTimestamp(trackData.track, cue.timestamp, true);
899
-
900
- let bodyText = cue.text;
901
- const timestampMs = Math.round(timestamp * 1000);
902
-
903
- if (track.source._codec === 'ass' || track.source._codec === 'ssa') {
904
- bodyText = convertDialogueLineToMkvFormat(bodyText);
905
- } else {
906
- inlineTimestampRegex.lastIndex = 0;
907
- bodyText = bodyText.replace(inlineTimestampRegex, (match) => {
908
- const time = parseSubtitleTimestamp(match.slice(1, -1));
909
- const offsetTime = time - timestampMs;
910
-
911
- return `<${formatSubtitleTimestamp(offsetTime)}>`;
912
- });
913
- }
914
-
915
- const body = textEncoder.encode(bodyText);
916
- const additions = `${cue.settings ?? ''}\n${cue.identifier ?? ''}\n${cue.notes ?? ''}`;
917
-
918
- const subtitleChunk = this.createInternalChunk(
919
- body,
920
- timestamp,
921
- cue.duration,
922
- 'key',
923
- additions.trim() ? textEncoder.encode(additions) : null,
924
- );
925
-
926
- trackData.chunkQueue.push(subtitleChunk);
927
- await this.interleaveChunks();
928
- } finally {
929
- release();
930
- }
931
- }
932
-
933
- private async interleaveChunks(isFinalCall = false) {
934
- if (!isFinalCall && !this.allTracksAreKnown()) {
935
- return; // We can't interleave yet as we don't yet know how many tracks we'll truly have
936
- }
937
-
938
- outer:
939
- while (true) {
940
- let trackWithMinTimestamp: MatroskaTrackData | null = null;
941
- let minTimestamp = Infinity;
942
-
943
- for (const trackData of this.trackDatas) {
944
- if (!isFinalCall && trackData.chunkQueue.length === 0 && !trackData.track.source._closed) {
945
- break outer;
946
- }
947
-
948
- if (trackData.chunkQueue.length > 0 && trackData.chunkQueue[0]!.timestamp < minTimestamp) {
949
- trackWithMinTimestamp = trackData;
950
- minTimestamp = trackData.chunkQueue[0]!.timestamp;
951
- }
952
- }
953
-
954
- if (!trackWithMinTimestamp) {
955
- break;
956
- }
957
-
958
- const chunk = trackWithMinTimestamp.chunkQueue.shift()!;
959
- this.writeBlock(trackWithMinTimestamp, chunk);
960
- }
961
-
962
- if (!isFinalCall) {
963
- await this.writer.flush();
964
- }
965
- }
966
-
967
- /**
968
- * Due to [a bug in Chromium](https://bugs.chromium.org/p/chromium/issues/detail?id=1377842), VP9 streams often
969
- * lack color space information. This method patches in that information.
970
- */
971
- private fixVP9ColorSpace(
972
- trackData: MatroskaVideoTrackData,
973
- chunk: InternalMediaChunk,
974
- ) {
975
- // http://downloads.webmproject.org/docs/vp9/vp9-bitstream_superframe-and-uncompressed-header_v1.0.pdf
976
-
977
- if (chunk.type !== 'key') return;
978
- if (!trackData.info.decoderConfig.colorSpace || !trackData.info.decoderConfig.colorSpace.matrix) return;
979
-
980
- const bitstream = new Bitstream(chunk.data);
981
-
982
- bitstream.skipBits(2);
983
-
984
- const profileLowBit = bitstream.readBits(1);
985
- const profileHighBit = bitstream.readBits(1);
986
- const profile = (profileHighBit << 1) + profileLowBit;
987
-
988
- if (profile === 3) bitstream.skipBits(1);
989
-
990
- const showExistingFrame = bitstream.readBits(1);
991
- if (showExistingFrame) return;
992
-
993
- const frameType = bitstream.readBits(1);
994
- if (frameType !== 0) return; // Just to be sure
995
-
996
- bitstream.skipBits(2);
997
-
998
- const syncCode = bitstream.readBits(24);
999
- if (syncCode !== 0x498342) return;
1000
-
1001
- if (profile >= 2) bitstream.skipBits(1);
1002
-
1003
- const colorSpaceID = {
1004
- rgb: 7,
1005
- bt709: 2,
1006
- bt470bg: 1,
1007
- smpte170m: 3,
1008
- }[trackData.info.decoderConfig.colorSpace.matrix];
1009
-
1010
- // The bitstream position is now at the start of the color space bits.
1011
- // We can use the global writeBits function here as requested.
1012
- writeBits(chunk.data, bitstream.pos, bitstream.pos + 3, colorSpaceID);
1013
- }
1014
-
1015
- /** Converts a read-only external chunk into an internal one for easier use. */
1016
- private createInternalChunk(
1017
- data: Uint8Array,
1018
- timestamp: number,
1019
- duration: number,
1020
- type: 'key' | 'delta',
1021
- additions: Uint8Array | null = null,
1022
- ) {
1023
- const internalChunk: InternalMediaChunk = {
1024
- data,
1025
- type,
1026
- timestamp,
1027
- duration,
1028
- additions,
1029
- };
1030
-
1031
- return internalChunk;
1032
- }
1033
-
1034
- /** Writes a block containing media data to the file. */
1035
- private writeBlock(trackData: MatroskaTrackData, chunk: InternalMediaChunk) {
1036
- // Due to the interlacing algorithm, this code will be run once we've seen one chunk from every media track.
1037
- if (!this.segment) {
1038
- this.createSegment();
1039
- }
1040
-
1041
- const msTimestamp = Math.round(1000 * chunk.timestamp);
1042
-
1043
- // We wanna only finalize this cluster (and begin a new one) if we know that each track will be able to
1044
- // start the new one with a key frame.
1045
- const keyFrameQueuedEverywhere = this.trackDatas.every((otherTrackData) => {
1046
- if (trackData === otherTrackData) {
1047
- return chunk.type === 'key';
1048
- }
1049
-
1050
- const firstQueuedSample = otherTrackData.chunkQueue[0];
1051
- if (firstQueuedSample) {
1052
- return firstQueuedSample.type === 'key';
1053
- }
1054
-
1055
- return otherTrackData.track.source._closed;
1056
- });
1057
-
1058
- let shouldCreateNewCluster = false;
1059
- if (!this.currentCluster) {
1060
- shouldCreateNewCluster = true;
1061
- } else {
1062
- assert(this.currentClusterStartMsTimestamp !== null);
1063
- assert(this.currentClusterMaxMsTimestamp !== null);
1064
-
1065
- const relativeTimestamp = msTimestamp - this.currentClusterStartMsTimestamp;
1066
-
1067
- shouldCreateNewCluster = (
1068
- keyFrameQueuedEverywhere
1069
- // This check is required because that means there is already a block with this timestamp in the
1070
- // CURRENT chunk, meaning that starting the next cluster at the same timestamp is forbidden (since
1071
- // the already-written block would belong into it instead).
1072
- && msTimestamp > this.currentClusterMaxMsTimestamp
1073
- && relativeTimestamp >= 1000 * (this.format._options.minimumClusterDuration ?? 1)
1074
- )
1075
- // The cluster would exceed its maximum allowed length. This puts us in an unfortunate position and forces
1076
- // us to begin the next cluster with a delta frame. Although this is undesirable, it is not forbidden by the
1077
- // spec and is supported by players.
1078
- || relativeTimestamp > MAX_CLUSTER_TIMESTAMP_MS;
1079
- }
1080
-
1081
- if (shouldCreateNewCluster) {
1082
- this.createNewCluster(msTimestamp);
1083
- }
1084
-
1085
- const relativeTimestamp = msTimestamp - this.currentClusterStartMsTimestamp!;
1086
- if (relativeTimestamp < MIN_CLUSTER_TIMESTAMP_MS) {
1087
- // The block lies too far in the past, it's not representable within this cluster
1088
- return;
1089
- }
1090
-
1091
- const prelude = new Uint8Array(4);
1092
- const view = new DataView(prelude.buffer);
1093
- // 0x80 to indicate it's the last byte of a multi-byte number
1094
- view.setUint8(0, 0x80 | trackData.track.id);
1095
- view.setInt16(1, relativeTimestamp, false);
1096
-
1097
- const msDuration = Math.round(1000 * chunk.duration);
1098
-
1099
- if (!chunk.additions) {
1100
- // No additions, we can write out a SimpleBlock
1101
- view.setUint8(3, Number(chunk.type === 'key') << 7); // Flags (keyframe flag only present for SimpleBlock)
1102
-
1103
- const simpleBlock = { id: EBMLId.SimpleBlock, data: [
1104
- prelude,
1105
- chunk.data,
1106
- ] };
1107
- this.ebmlWriter.writeEBML(simpleBlock);
1108
- } else {
1109
- const blockGroup = { id: EBMLId.BlockGroup, data: [
1110
- { id: EBMLId.Block, data: [
1111
- prelude,
1112
- chunk.data,
1113
- ] },
1114
- chunk.type === 'delta'
1115
- ? {
1116
- id: EBMLId.ReferenceBlock,
1117
- data: new EBMLSignedInt(trackData.lastWrittenMsTimestamp! - msTimestamp),
1118
- }
1119
- : null,
1120
- chunk.additions
1121
- ? { id: EBMLId.BlockAdditions, data: [
1122
- { id: EBMLId.BlockMore, data: [
1123
- { id: EBMLId.BlockAddID, data: 1 }, // Some players expect BlockAddID to come first
1124
- { id: EBMLId.BlockAdditional, data: chunk.additions },
1125
- ] },
1126
- ] }
1127
- : null,
1128
- msDuration > 0 ? { id: EBMLId.BlockDuration, data: msDuration } : null,
1129
- ] };
1130
- this.ebmlWriter.writeEBML(blockGroup);
1131
- }
1132
-
1133
- this.duration = Math.max(this.duration, msTimestamp + msDuration);
1134
- trackData.lastWrittenMsTimestamp = msTimestamp;
1135
-
1136
- if (!this.trackDatasInCurrentCluster.has(trackData)) {
1137
- this.trackDatasInCurrentCluster.set(trackData, {
1138
- firstMsTimestamp: msTimestamp,
1139
- });
1140
- }
1141
- this.currentClusterMaxMsTimestamp = Math.max(this.currentClusterMaxMsTimestamp!, msTimestamp);
1142
- }
1143
-
1144
- /** Creates a new Cluster element to contain media chunks. */
1145
- private createNewCluster(msTimestamp: number) {
1146
- if (this.currentCluster) {
1147
- this.finalizeCurrentCluster();
1148
- }
1149
-
1150
- if (this.format._options.onCluster) {
1151
- this.writer.startTrackingWrites();
1152
- }
1153
-
1154
- this.currentCluster = {
1155
- id: EBMLId.Cluster,
1156
- size: this.format._options.appendOnly ? -1 : CLUSTER_SIZE_BYTES,
1157
- data: [
1158
- { id: EBMLId.Timestamp, data: msTimestamp },
1159
- ],
1160
- };
1161
- this.ebmlWriter.writeEBML(this.currentCluster);
1162
-
1163
- this.currentClusterStartMsTimestamp = msTimestamp;
1164
- this.currentClusterMaxMsTimestamp = msTimestamp;
1165
- this.trackDatasInCurrentCluster.clear();
1166
- }
1167
-
1168
- private finalizeCurrentCluster() {
1169
- assert(this.currentCluster);
1170
-
1171
- if (!this.format._options.appendOnly) {
1172
- const clusterSize = this.writer.getPos() - this.ebmlWriter.dataOffsets.get(this.currentCluster)!;
1173
- const endPos = this.writer.getPos();
1174
-
1175
- // Write the size now that we know it
1176
- this.writer.seek(this.ebmlWriter.offsets.get(this.currentCluster)! + 4);
1177
- this.ebmlWriter.writeVarInt(clusterSize, CLUSTER_SIZE_BYTES);
1178
- this.writer.seek(endPos);
1179
- }
1180
-
1181
- if (this.format._options.onCluster) {
1182
- assert(this.currentClusterStartMsTimestamp !== null);
1183
-
1184
- const { data, start } = this.writer.stopTrackingWrites();
1185
- this.format._options.onCluster(data, start, this.currentClusterStartMsTimestamp / 1000);
1186
- }
1187
-
1188
- const clusterOffsetFromSegment
1189
- = this.ebmlWriter.offsets.get(this.currentCluster)! - this.segmentDataOffset;
1190
-
1191
- // Group tracks by their first timestamp and create a CuePoint for each unique timestamp
1192
- const groupedByTimestamp = new Map<number, MatroskaTrackData[]>();
1193
- for (const [trackData, { firstMsTimestamp }] of this.trackDatasInCurrentCluster) {
1194
- if (!groupedByTimestamp.has(firstMsTimestamp)) {
1195
- groupedByTimestamp.set(firstMsTimestamp, []);
1196
- }
1197
- groupedByTimestamp.get(firstMsTimestamp)!.push(trackData);
1198
- }
1199
-
1200
- const groupedAndSortedByTimestamp = [...groupedByTimestamp.entries()].sort((a, b) => a[0] - b[0]);
1201
-
1202
- // Add CuePoints to the Cues element for better seeking
1203
- for (const [msTimestamp, trackDatas] of groupedAndSortedByTimestamp) {
1204
- assert(this.cues);
1205
- (this.cues.data as EBML[]).push({ id: EBMLId.CuePoint, data: [
1206
- { id: EBMLId.CueTime, data: msTimestamp },
1207
- // Create CueTrackPositions for each track that starts at this timestamp
1208
- ...trackDatas.map((trackData) => {
1209
- return { id: EBMLId.CueTrackPositions, data: [
1210
- { id: EBMLId.CueTrack, data: trackData.track.id },
1211
- { id: EBMLId.CueClusterPosition, data: clusterOffsetFromSegment },
1212
- ] };
1213
- }),
1214
- ] });
1215
- }
1216
- }
1217
-
1218
- // eslint-disable-next-line @typescript-eslint/no-misused-promises
1219
- override async onTrackClose() {
1220
- const release = await this.mutex.acquire();
1221
-
1222
- if (this.allTracksAreKnown()) {
1223
- this.allTracksKnown.resolve();
1224
- }
1225
-
1226
- // Since a track is now closed, we may be able to write out chunks that were previously waiting
1227
- await this.interleaveChunks();
1228
-
1229
- release();
1230
- }
1231
-
1232
- /** Finalizes the file, making it ready for use. Must be called after all media chunks have been added. */
1233
- async finalize() {
1234
- const release = await this.mutex.acquire();
1235
-
1236
- this.allTracksKnown.resolve();
1237
-
1238
- if (!this.segment) {
1239
- this.createSegment();
1240
- }
1241
-
1242
- // Flush any remaining queued chunks to the file
1243
- await this.interleaveChunks(true);
1244
-
1245
- if (this.currentCluster) {
1246
- this.finalizeCurrentCluster();
1247
- }
1248
-
1249
- assert(this.cues);
1250
- this.ebmlWriter.writeEBML(this.cues);
1251
-
1252
- if (!this.format._options.appendOnly) {
1253
- const endPos = this.writer.getPos();
1254
-
1255
- // Write the Segment size
1256
- const segmentSize = this.writer.getPos() - this.segmentDataOffset;
1257
- this.writer.seek(this.ebmlWriter.offsets.get(this.segment!)! + 4);
1258
- this.ebmlWriter.writeVarInt(segmentSize, SEGMENT_SIZE_BYTES);
1259
-
1260
- // Write the duration of the media to the Segment
1261
- this.segmentDuration!.data = new EBMLFloat64(this.duration);
1262
- this.writer.seek(this.ebmlWriter.offsets.get(this.segmentDuration!)!);
1263
- this.ebmlWriter.writeEBML(this.segmentDuration);
1264
-
1265
- // Fill in SeekHead position data and write it again
1266
- assert(this.seekHead);
1267
- this.writer.seek(this.ebmlWriter.offsets.get(this.seekHead)!);
1268
- this.maybeCreateSeekHead(true);
1269
- this.ebmlWriter.writeEBML(this.seekHead);
1270
-
1271
- this.writer.seek(endPos);
1272
- }
1273
-
1274
- release();
1275
- }
1276
- }