@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
@@ -1,285 +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
- import { Demuxer } from '../demuxer.js';
9
- import { InputAudioTrack } from '../input-track.js';
10
- import { DEFAULT_TRACK_DISPOSITION } from '../metadata.js';
11
- import { assert, AsyncMutex, binarySearchExact, binarySearchLessOrEqual, UNDETERMINED_LANGUAGE } from '../misc.js';
12
- import { EncodedPacket, PLACEHOLDER_DATA } from '../packet.js';
13
- import { getXingOffset, INFO, XING } from '../../shared/mp3-misc.js';
14
- import { ID3_V1_TAG_SIZE, ID3_V2_HEADER_SIZE, parseId3V1Tag, parseId3V2Tag, readId3V2Header, } from '../id3.js';
15
- import { readNextFrameHeader } from './mp3-reader.js';
16
- import { readAscii, readBytes, readU32Be } from '../reader.js';
17
- export class Mp3Demuxer extends Demuxer {
18
- constructor(input) {
19
- super(input);
20
- this.metadataPromise = null;
21
- this.firstFrameHeader = null;
22
- this.loadedSamples = []; // All samples from the start of the file to lastLoadedPos
23
- this.metadataTags = null;
24
- this.tracks = [];
25
- this.readingMutex = new AsyncMutex();
26
- this.lastSampleLoaded = false;
27
- this.lastLoadedPos = 0;
28
- this.nextTimestampInSamples = 0;
29
- this.reader = input._reader;
30
- }
31
- async readMetadata() {
32
- return this.metadataPromise ??= (async () => {
33
- // Keep loading until we find the first frame header
34
- while (!this.firstFrameHeader && !this.lastSampleLoaded) {
35
- await this.advanceReader();
36
- }
37
- if (!this.firstFrameHeader) {
38
- throw new Error('No valid MP3 frame found.');
39
- }
40
- this.tracks = [new InputAudioTrack(this.input, new Mp3AudioTrackBacking(this))];
41
- })();
42
- }
43
- async advanceReader() {
44
- if (this.lastLoadedPos === 0) {
45
- // Let's skip all ID3v2 tags at the start of the file
46
- while (true) {
47
- let slice = this.reader.requestSlice(this.lastLoadedPos, ID3_V2_HEADER_SIZE);
48
- if (slice instanceof Promise)
49
- slice = await slice;
50
- if (!slice) {
51
- this.lastSampleLoaded = true;
52
- return;
53
- }
54
- const id3V2Header = readId3V2Header(slice);
55
- if (!id3V2Header) {
56
- break;
57
- }
58
- this.lastLoadedPos = slice.filePos + id3V2Header.size;
59
- }
60
- }
61
- const result = await readNextFrameHeader(this.reader, this.lastLoadedPos, this.reader.fileSize);
62
- if (!result) {
63
- this.lastSampleLoaded = true;
64
- return;
65
- }
66
- const header = result.header;
67
- this.lastLoadedPos = result.startPos + header.totalSize - 1; // -1 in case the frame is 1 byte too short
68
- const xingOffset = getXingOffset(header.mpegVersionId, header.channel);
69
- let slice = this.reader.requestSlice(result.startPos + xingOffset, 4);
70
- if (slice instanceof Promise)
71
- slice = await slice;
72
- if (slice) {
73
- const word = readU32Be(slice);
74
- const isXing = word === XING || word === INFO;
75
- if (isXing) {
76
- // There's no actual audio data in this frame, so let's skip it
77
- return;
78
- }
79
- }
80
- if (!this.firstFrameHeader) {
81
- this.firstFrameHeader = header;
82
- }
83
- if (header.sampleRate !== this.firstFrameHeader.sampleRate) {
84
- console.warn(`MP3 changed sample rate mid-file: ${this.firstFrameHeader.sampleRate} Hz to ${header.sampleRate} Hz.`
85
- + ` Might be a bug, so please report this file.`);
86
- }
87
- const sampleDuration = header.audioSamplesInFrame / this.firstFrameHeader.sampleRate;
88
- const sample = {
89
- timestamp: this.nextTimestampInSamples / this.firstFrameHeader.sampleRate,
90
- duration: sampleDuration,
91
- dataStart: result.startPos,
92
- dataSize: header.totalSize,
93
- };
94
- this.loadedSamples.push(sample);
95
- this.nextTimestampInSamples += header.audioSamplesInFrame;
96
- return;
97
- }
98
- async getMimeType() {
99
- return 'audio/mpeg';
100
- }
101
- async getTracks() {
102
- await this.readMetadata();
103
- return this.tracks;
104
- }
105
- async computeDuration() {
106
- await this.readMetadata();
107
- const track = this.tracks[0];
108
- assert(track);
109
- return track.computeDuration();
110
- }
111
- async getMetadataTags() {
112
- const release = await this.readingMutex.acquire();
113
- try {
114
- await this.readMetadata();
115
- if (this.metadataTags) {
116
- return this.metadataTags;
117
- }
118
- this.metadataTags = {};
119
- let currentPos = 0;
120
- let id3V2HeaderFound = false;
121
- while (true) {
122
- let headerSlice = this.reader.requestSlice(currentPos, ID3_V2_HEADER_SIZE);
123
- if (headerSlice instanceof Promise)
124
- headerSlice = await headerSlice;
125
- if (!headerSlice)
126
- break;
127
- const id3V2Header = readId3V2Header(headerSlice);
128
- if (!id3V2Header) {
129
- break;
130
- }
131
- id3V2HeaderFound = true;
132
- let contentSlice = this.reader.requestSlice(headerSlice.filePos, id3V2Header.size);
133
- if (contentSlice instanceof Promise)
134
- contentSlice = await contentSlice;
135
- if (!contentSlice)
136
- break;
137
- parseId3V2Tag(contentSlice, id3V2Header, this.metadataTags);
138
- currentPos = headerSlice.filePos + id3V2Header.size;
139
- }
140
- if (!id3V2HeaderFound && this.reader.fileSize !== null && this.reader.fileSize >= ID3_V1_TAG_SIZE) {
141
- // Try reading an ID3v1 tag at the end of the file
142
- let slice = this.reader.requestSlice(this.reader.fileSize - ID3_V1_TAG_SIZE, ID3_V1_TAG_SIZE);
143
- if (slice instanceof Promise)
144
- slice = await slice;
145
- assert(slice);
146
- const tag = readAscii(slice, 3);
147
- if (tag === 'TAG') {
148
- parseId3V1Tag(slice, this.metadataTags);
149
- }
150
- }
151
- return this.metadataTags;
152
- }
153
- finally {
154
- release();
155
- }
156
- }
157
- }
158
- class Mp3AudioTrackBacking {
159
- constructor(demuxer) {
160
- this.demuxer = demuxer;
161
- }
162
- getId() {
163
- return 1;
164
- }
165
- async getFirstTimestamp() {
166
- return 0;
167
- }
168
- getTimeResolution() {
169
- assert(this.demuxer.firstFrameHeader);
170
- return this.demuxer.firstFrameHeader.sampleRate / this.demuxer.firstFrameHeader.audioSamplesInFrame;
171
- }
172
- async computeDuration() {
173
- const lastPacket = await this.getPacket(Infinity, { metadataOnly: true });
174
- return (lastPacket?.timestamp ?? 0) + (lastPacket?.duration ?? 0);
175
- }
176
- getName() {
177
- return null;
178
- }
179
- getLanguageCode() {
180
- return UNDETERMINED_LANGUAGE;
181
- }
182
- getCodec() {
183
- return 'mp3';
184
- }
185
- getInternalCodecId() {
186
- return null;
187
- }
188
- getNumberOfChannels() {
189
- assert(this.demuxer.firstFrameHeader);
190
- return this.demuxer.firstFrameHeader.channel === 3 ? 1 : 2;
191
- }
192
- getSampleRate() {
193
- assert(this.demuxer.firstFrameHeader);
194
- return this.demuxer.firstFrameHeader.sampleRate;
195
- }
196
- getDisposition() {
197
- return {
198
- ...DEFAULT_TRACK_DISPOSITION,
199
- };
200
- }
201
- async getDecoderConfig() {
202
- assert(this.demuxer.firstFrameHeader);
203
- return {
204
- codec: 'mp3',
205
- numberOfChannels: this.demuxer.firstFrameHeader.channel === 3 ? 1 : 2,
206
- sampleRate: this.demuxer.firstFrameHeader.sampleRate,
207
- };
208
- }
209
- async getPacketAtIndex(sampleIndex, options) {
210
- if (sampleIndex === -1) {
211
- return null;
212
- }
213
- const rawSample = this.demuxer.loadedSamples[sampleIndex];
214
- if (!rawSample) {
215
- return null;
216
- }
217
- let data;
218
- if (options.metadataOnly) {
219
- data = PLACEHOLDER_DATA;
220
- }
221
- else {
222
- let slice = this.demuxer.reader.requestSlice(rawSample.dataStart, rawSample.dataSize);
223
- if (slice instanceof Promise)
224
- slice = await slice;
225
- if (!slice) {
226
- return null; // Data didn't fit into the rest of the file
227
- }
228
- data = readBytes(slice, rawSample.dataSize);
229
- }
230
- return new EncodedPacket(data, 'key', rawSample.timestamp, rawSample.duration, sampleIndex, rawSample.dataSize);
231
- }
232
- getFirstPacket(options) {
233
- return this.getPacketAtIndex(0, options);
234
- }
235
- async getNextPacket(packet, options) {
236
- const release = await this.demuxer.readingMutex.acquire();
237
- try {
238
- const sampleIndex = binarySearchExact(this.demuxer.loadedSamples, packet.timestamp, x => x.timestamp);
239
- if (sampleIndex === -1) {
240
- throw new Error('Packet was not created from this track.');
241
- }
242
- const nextIndex = sampleIndex + 1;
243
- // Ensure the next sample exists
244
- while (nextIndex >= this.demuxer.loadedSamples.length
245
- && !this.demuxer.lastSampleLoaded) {
246
- await this.demuxer.advanceReader();
247
- }
248
- return this.getPacketAtIndex(nextIndex, options);
249
- }
250
- finally {
251
- release();
252
- }
253
- }
254
- async getPacket(timestamp, options) {
255
- const release = await this.demuxer.readingMutex.acquire();
256
- try {
257
- while (true) {
258
- const index = binarySearchLessOrEqual(this.demuxer.loadedSamples, timestamp, x => x.timestamp);
259
- if (index === -1 && this.demuxer.loadedSamples.length > 0) {
260
- // We're before the first sample
261
- return null;
262
- }
263
- if (this.demuxer.lastSampleLoaded) {
264
- // All data is loaded, return what we found
265
- return this.getPacketAtIndex(index, options);
266
- }
267
- if (index >= 0 && index + 1 < this.demuxer.loadedSamples.length) {
268
- // The next packet also exists, we're done
269
- return this.getPacketAtIndex(index, options);
270
- }
271
- // Otherwise, keep loading data
272
- await this.demuxer.advanceReader();
273
- }
274
- }
275
- finally {
276
- release();
277
- }
278
- }
279
- getKeyPacket(timestamp, options) {
280
- return this.getPacket(timestamp, options);
281
- }
282
- getNextKeyPacket(packet, options) {
283
- return this.getNextPacket(packet, options);
284
- }
285
- }
@@ -1,123 +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
- import { assert, toDataView } from '../misc.js';
9
- import { metadataTagsAreEmpty } from '../metadata.js';
10
- import { Muxer } from '../muxer.js';
11
- import { getXingOffset, INFO, readFrameHeader, XING } from '../../shared/mp3-misc.js';
12
- import { Mp3Writer } from './mp3-writer.js';
13
- import { Id3V2Writer } from '../id3.js';
14
- export class Mp3Muxer extends Muxer {
15
- constructor(output, format) {
16
- super(output);
17
- this.xingFrameData = null;
18
- this.frameCount = 0;
19
- this.framePositions = [];
20
- this.xingFramePos = null;
21
- this.format = format;
22
- this.writer = output._writer;
23
- this.mp3Writer = new Mp3Writer(output._writer);
24
- }
25
- async start() {
26
- if (!metadataTagsAreEmpty(this.output._metadataTags)) {
27
- const id3Writer = new Id3V2Writer(this.writer);
28
- id3Writer.writeId3V2Tag(this.output._metadataTags);
29
- }
30
- }
31
- async getMimeType() {
32
- return 'audio/mpeg';
33
- }
34
- async addEncodedVideoPacket() {
35
- throw new Error('MP3 does not support video.');
36
- }
37
- async addEncodedAudioPacket(track, packet) {
38
- const release = await this.mutex.acquire();
39
- try {
40
- const writeXingHeader = this.format._options.xingHeader !== false;
41
- if (!this.xingFrameData && writeXingHeader) {
42
- const view = toDataView(packet.data);
43
- if (view.byteLength < 4) {
44
- throw new Error('Invalid MP3 header in sample.');
45
- }
46
- const word = view.getUint32(0, false);
47
- const header = readFrameHeader(word, null).header;
48
- if (!header) {
49
- throw new Error('Invalid MP3 header in sample.');
50
- }
51
- const xingOffset = getXingOffset(header.mpegVersionId, header.channel);
52
- if (view.byteLength >= xingOffset + 4) {
53
- const word = view.getUint32(xingOffset, false);
54
- const isXing = word === XING || word === INFO;
55
- if (isXing) {
56
- // This is not a data frame, so let's completely ignore this sample
57
- return;
58
- }
59
- }
60
- this.xingFrameData = {
61
- mpegVersionId: header.mpegVersionId,
62
- layer: header.layer,
63
- frequencyIndex: header.frequencyIndex,
64
- sampleRate: header.sampleRate,
65
- channel: header.channel,
66
- modeExtension: header.modeExtension,
67
- copyright: header.copyright,
68
- original: header.original,
69
- emphasis: header.emphasis,
70
- frameCount: null,
71
- fileSize: null,
72
- toc: null,
73
- };
74
- // Write a Xing frame because this muxer doesn't make any bitrate constraints, meaning we don't know if
75
- // this will be a constant or variable bitrate file. Therefore, always write the Xing frame.
76
- this.xingFramePos = this.writer.getPos();
77
- this.mp3Writer.writeXingFrame(this.xingFrameData);
78
- this.frameCount++;
79
- }
80
- this.validateAndNormalizeTimestamp(track, packet.timestamp, packet.type === 'key');
81
- this.writer.write(packet.data);
82
- this.frameCount++;
83
- await this.writer.flush();
84
- if (writeXingHeader) {
85
- this.framePositions.push(this.writer.getPos());
86
- }
87
- }
88
- finally {
89
- release();
90
- }
91
- }
92
- async addSubtitleCue() {
93
- throw new Error('MP3 does not support subtitles.');
94
- }
95
- async finalize() {
96
- if (!this.xingFrameData || this.xingFramePos === null) {
97
- return;
98
- }
99
- const release = await this.mutex.acquire();
100
- const endPos = this.writer.getPos();
101
- this.writer.seek(this.xingFramePos);
102
- const toc = new Uint8Array(100);
103
- for (let i = 0; i < 100; i++) {
104
- const index = Math.floor(this.framePositions.length * (i / 100));
105
- assert(index !== -1 && index < this.framePositions.length);
106
- const byteOffset = this.framePositions[index];
107
- toc[i] = 256 * (byteOffset / endPos);
108
- }
109
- this.xingFrameData.frameCount = this.frameCount;
110
- this.xingFrameData.fileSize = endPos;
111
- this.xingFrameData.toc = toc;
112
- if (this.format._options.onXingFrame) {
113
- this.writer.startTrackingWrites();
114
- }
115
- this.mp3Writer.writeXingFrame(this.xingFrameData);
116
- if (this.format._options.onXingFrame) {
117
- const { data, start } = this.writer.stopTrackingWrites();
118
- this.format._options.onXingFrame(data, start);
119
- }
120
- this.writer.seek(endPos);
121
- release();
122
- }
123
- }
@@ -1,26 +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
- import { FRAME_HEADER_SIZE, readFrameHeader } from '../../shared/mp3-misc.js';
9
- import { readU32Be } from '../reader.js';
10
- export const readNextFrameHeader = async (reader, startPos, until) => {
11
- let currentPos = startPos;
12
- while (until === null || currentPos < until) {
13
- let slice = reader.requestSlice(currentPos, FRAME_HEADER_SIZE);
14
- if (slice instanceof Promise)
15
- slice = await slice;
16
- if (!slice)
17
- break;
18
- const word = readU32Be(slice);
19
- const result = readFrameHeader(word, reader.fileSize !== null ? reader.fileSize - currentPos : null);
20
- if (result.header) {
21
- return { header: result.header, startPos: currentPos };
22
- }
23
- currentPos += result.bytesAdvanced;
24
- }
25
- return null;
26
- };
@@ -1,78 +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
- import { computeMp3FrameSize, getXingOffset, KILOBIT_RATES, XING, } from '../../shared/mp3-misc.js';
9
- export class Mp3Writer {
10
- constructor(writer) {
11
- this.writer = writer;
12
- this.helper = new Uint8Array(8);
13
- this.helperView = new DataView(this.helper.buffer);
14
- }
15
- writeU32(value) {
16
- this.helperView.setUint32(0, value, false);
17
- this.writer.write(this.helper.subarray(0, 4));
18
- }
19
- writeXingFrame(data) {
20
- const startPos = this.writer.getPos();
21
- const firstByte = 0xff;
22
- const secondByte = 0xe0 | (data.mpegVersionId << 3) | (data.layer << 1);
23
- let lowSamplingFrequency;
24
- if (data.mpegVersionId & 2) {
25
- lowSamplingFrequency = (data.mpegVersionId & 1) ? 0 : 1;
26
- }
27
- else {
28
- lowSamplingFrequency = 1;
29
- }
30
- const padding = 0;
31
- const neededBytes = 155;
32
- let bitrateIndex = -1;
33
- const bitrateOffset = lowSamplingFrequency * 16 * 4 + data.layer * 16;
34
- // Let's find the lowest bitrate for which the frame size is sufficiently large to fit all the data
35
- for (let i = 0; i < 16; i++) {
36
- const kbr = KILOBIT_RATES[bitrateOffset + i];
37
- const size = computeMp3FrameSize(lowSamplingFrequency, data.layer, 1000 * kbr, data.sampleRate, padding);
38
- if (size >= neededBytes) {
39
- bitrateIndex = i;
40
- break;
41
- }
42
- }
43
- if (bitrateIndex === -1) {
44
- throw new Error('No suitable bitrate found.');
45
- }
46
- const thirdByte = (bitrateIndex << 4) | (data.frequencyIndex << 2) | padding << 1;
47
- const fourthByte = (data.channel << 6)
48
- | (data.modeExtension << 4)
49
- | (data.copyright << 3)
50
- | (data.original << 2)
51
- | data.emphasis;
52
- this.helper[0] = firstByte;
53
- this.helper[1] = secondByte;
54
- this.helper[2] = thirdByte;
55
- this.helper[3] = fourthByte;
56
- this.writer.write(this.helper.subarray(0, 4));
57
- const xingOffset = getXingOffset(data.mpegVersionId, data.channel);
58
- this.writer.seek(startPos + xingOffset);
59
- this.writeU32(XING);
60
- let flags = 0;
61
- if (data.frameCount !== null) {
62
- flags |= 1;
63
- }
64
- if (data.fileSize !== null) {
65
- flags |= 2;
66
- }
67
- if (data.toc !== null) {
68
- flags |= 4;
69
- }
70
- this.writeU32(flags);
71
- this.writeU32(data.frameCount ?? 0);
72
- this.writeU32(data.fileSize ?? 0);
73
- this.writer.write(data.toc ?? new Uint8Array(100));
74
- const kilobitRate = KILOBIT_RATES[bitrateOffset + bitrateIndex];
75
- const frameSize = computeMp3FrameSize(lowSamplingFrequency, data.layer, 1000 * kilobitRate, data.sampleRate, padding);
76
- this.writer.seek(startPos + frameSize);
77
- }
78
- }
@@ -1,50 +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
- import { AsyncMutex } from './misc.js';
9
- export class Muxer {
10
- constructor(output) {
11
- this.mutex = new AsyncMutex();
12
- /**
13
- * This field is used to synchronize multiple MediaStreamTracks. They use the same time coordinate system across
14
- * tracks, and to ensure correct audio-video sync, we must use the same offset for all of them. The reason an offset
15
- * is needed at all is because the timestamps typically don't start at zero.
16
- */
17
- this.firstMediaStreamTimestamp = null;
18
- this.trackTimestampInfo = new WeakMap();
19
- this.output = output;
20
- }
21
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
22
- onTrackClose(track) { }
23
- validateAndNormalizeTimestamp(track, timestampInSeconds, isKeyPacket) {
24
- timestampInSeconds += track.source._timestampOffset;
25
- let timestampInfo = this.trackTimestampInfo.get(track);
26
- if (!timestampInfo) {
27
- if (!isKeyPacket) {
28
- throw new Error('First packet must be a key packet.');
29
- }
30
- timestampInfo = {
31
- maxTimestamp: timestampInSeconds,
32
- maxTimestampBeforeLastKeyPacket: timestampInSeconds,
33
- };
34
- this.trackTimestampInfo.set(track, timestampInfo);
35
- }
36
- if (timestampInSeconds < 0) {
37
- throw new Error(`Timestamps must be non-negative (got ${timestampInSeconds}s).`);
38
- }
39
- if (isKeyPacket) {
40
- timestampInfo.maxTimestampBeforeLastKeyPacket = timestampInfo.maxTimestamp;
41
- }
42
- if (timestampInSeconds < timestampInfo.maxTimestampBeforeLastKeyPacket) {
43
- throw new Error(`Timestamps cannot be smaller than the largest timestamp of the previous GOP (a GOP begins with a key`
44
- + ` packet and ends right before the next key packet). Got ${timestampInSeconds}s, but largest`
45
- + ` timestamp is ${timestampInfo.maxTimestampBeforeLastKeyPacket}s.`);
46
- }
47
- timestampInfo.maxTimestamp = Math.max(timestampInfo.maxTimestamp, timestampInSeconds);
48
- return timestampInSeconds;
49
- }
50
- }
@@ -1,9 +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
- // This file contains Node.js-specific code that does not run in a browser.
9
- export * as fs from 'node:fs/promises';