@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,300 +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, isIso639Dash2LanguageCode } from './misc.js';
9
- import { validateMetadataTags, validateTrackDisposition } from './metadata.js';
10
- import { OutputFormat } from './output-format.js';
11
- import { AudioSource, SubtitleSource, VideoSource } from './media-source.js';
12
- import { Target } from './target.js';
13
- /**
14
- * List of all track types.
15
- * @group Miscellaneous
16
- * @public
17
- */
18
- export const ALL_TRACK_TYPES = ['video', 'audio', 'subtitle'];
19
- const validateBaseTrackMetadata = (metadata) => {
20
- if (!metadata || typeof metadata !== 'object') {
21
- throw new TypeError('metadata must be an object.');
22
- }
23
- if (metadata.languageCode !== undefined && !isIso639Dash2LanguageCode(metadata.languageCode)) {
24
- throw new TypeError('metadata.languageCode, when provided, must be a three-letter, ISO 639-2/T language code.');
25
- }
26
- if (metadata.name !== undefined && typeof metadata.name !== 'string') {
27
- throw new TypeError('metadata.name, when provided, must be a string.');
28
- }
29
- if (metadata.disposition !== undefined) {
30
- validateTrackDisposition(metadata.disposition);
31
- }
32
- if (metadata.maximumPacketCount !== undefined
33
- && (!Number.isInteger(metadata.maximumPacketCount) || metadata.maximumPacketCount < 0)) {
34
- throw new TypeError('metadata.maximumPacketCount, when provided, must be a non-negative integer.');
35
- }
36
- };
37
- /**
38
- * Main class orchestrating the creation of a new media file.
39
- * @group Output files
40
- * @public
41
- */
42
- export class Output {
43
- /**
44
- * Creates a new instance of {@link Output} which can then be used to create a new media file according to the
45
- * specified {@link OutputOptions}.
46
- */
47
- constructor(options) {
48
- /** The current state of the output. */
49
- this.state = 'pending';
50
- /** @internal */
51
- this._tracks = [];
52
- /** @internal */
53
- this._startPromise = null;
54
- /** @internal */
55
- this._cancelPromise = null;
56
- /** @internal */
57
- this._finalizePromise = null;
58
- /** @internal */
59
- this._mutex = new AsyncMutex();
60
- /** @internal */
61
- this._metadataTags = {};
62
- if (!options || typeof options !== 'object') {
63
- throw new TypeError('options must be an object.');
64
- }
65
- if (!(options.format instanceof OutputFormat)) {
66
- throw new TypeError('options.format must be an OutputFormat.');
67
- }
68
- if (!(options.target instanceof Target)) {
69
- throw new TypeError('options.target must be a Target.');
70
- }
71
- if (options.target._output) {
72
- throw new Error('Target is already used for another output.');
73
- }
74
- options.target._output = this;
75
- this.format = options.format;
76
- this.target = options.target;
77
- this._writer = options.target._createWriter();
78
- this._muxer = options.format._createMuxer(this);
79
- }
80
- /** Adds a video track to the output with the given source. Can only be called before the output is started. */
81
- addVideoTrack(source, metadata = {}) {
82
- if (!(source instanceof VideoSource)) {
83
- throw new TypeError('source must be a VideoSource.');
84
- }
85
- validateBaseTrackMetadata(metadata);
86
- if (metadata.rotation !== undefined && ![0, 90, 180, 270].includes(metadata.rotation)) {
87
- throw new TypeError(`Invalid video rotation: ${metadata.rotation}. Has to be 0, 90, 180 or 270.`);
88
- }
89
- if (!this.format.supportsVideoRotationMetadata && metadata.rotation) {
90
- throw new Error(`${this.format._name} does not support video rotation metadata.`);
91
- }
92
- if (metadata.frameRate !== undefined
93
- && (!Number.isFinite(metadata.frameRate) || metadata.frameRate <= 0)) {
94
- throw new TypeError(`Invalid video frame rate: ${metadata.frameRate}. Must be a positive number.`);
95
- }
96
- this._addTrack('video', source, metadata);
97
- }
98
- /** Adds an audio track to the output with the given source. Can only be called before the output is started. */
99
- addAudioTrack(source, metadata = {}) {
100
- if (!(source instanceof AudioSource)) {
101
- throw new TypeError('source must be an AudioSource.');
102
- }
103
- validateBaseTrackMetadata(metadata);
104
- this._addTrack('audio', source, metadata);
105
- }
106
- /** Adds a subtitle track to the output with the given source. Can only be called before the output is started. */
107
- addSubtitleTrack(source, metadata = {}) {
108
- if (!(source instanceof SubtitleSource)) {
109
- throw new TypeError('source must be a SubtitleSource.');
110
- }
111
- validateBaseTrackMetadata(metadata);
112
- this._addTrack('subtitle', source, metadata);
113
- }
114
- /**
115
- * Sets descriptive metadata tags about the media file, such as title, author, date, or cover art. When called
116
- * multiple times, only the metadata from the last call will be used.
117
- *
118
- * Can only be called before the output is started.
119
- */
120
- setMetadataTags(tags) {
121
- validateMetadataTags(tags);
122
- if (this.state !== 'pending') {
123
- throw new Error('Cannot set metadata tags after output has been started or canceled.');
124
- }
125
- this._metadataTags = tags;
126
- }
127
- /** @internal */
128
- _addTrack(type, source, metadata) {
129
- if (this.state !== 'pending') {
130
- throw new Error('Cannot add track after output has been started or canceled.');
131
- }
132
- if (source._connectedTrack) {
133
- throw new Error('Source is already used for a track.');
134
- }
135
- // Verify maximum track count constraints
136
- const supportedTrackCounts = this.format.getSupportedTrackCounts();
137
- const presentTracksOfThisType = this._tracks.reduce((count, track) => count + (track.type === type ? 1 : 0), 0);
138
- const maxCount = supportedTrackCounts[type].max;
139
- if (presentTracksOfThisType === maxCount) {
140
- throw new Error(maxCount === 0
141
- ? `${this.format._name} does not support ${type} tracks.`
142
- : (`${this.format._name} does not support more than ${maxCount} ${type} track`
143
- + `${maxCount === 1 ? '' : 's'}.`));
144
- }
145
- const maxTotalCount = supportedTrackCounts.total.max;
146
- if (this._tracks.length === maxTotalCount) {
147
- throw new Error(`${this.format._name} does not support more than ${maxTotalCount} tracks`
148
- + `${maxTotalCount === 1 ? '' : 's'} in total.`);
149
- }
150
- const track = {
151
- id: this._tracks.length + 1,
152
- output: this,
153
- type,
154
- source: source,
155
- metadata,
156
- };
157
- if (track.type === 'video') {
158
- const supportedVideoCodecs = this.format.getSupportedVideoCodecs();
159
- if (supportedVideoCodecs.length === 0) {
160
- throw new Error(`${this.format._name} does not support video tracks.`
161
- + this.format._codecUnsupportedHint(track.source._codec));
162
- }
163
- else if (!supportedVideoCodecs.includes(track.source._codec)) {
164
- throw new Error(`Codec '${track.source._codec}' cannot be contained within ${this.format._name}. Supported`
165
- + ` video codecs are: ${supportedVideoCodecs.map(codec => `'${codec}'`).join(', ')}.`
166
- + this.format._codecUnsupportedHint(track.source._codec));
167
- }
168
- }
169
- else if (track.type === 'audio') {
170
- const supportedAudioCodecs = this.format.getSupportedAudioCodecs();
171
- if (supportedAudioCodecs.length === 0) {
172
- throw new Error(`${this.format._name} does not support audio tracks.`
173
- + this.format._codecUnsupportedHint(track.source._codec));
174
- }
175
- else if (!supportedAudioCodecs.includes(track.source._codec)) {
176
- throw new Error(`Codec '${track.source._codec}' cannot be contained within ${this.format._name}. Supported`
177
- + ` audio codecs are: ${supportedAudioCodecs.map(codec => `'${codec}'`).join(', ')}.`
178
- + this.format._codecUnsupportedHint(track.source._codec));
179
- }
180
- }
181
- else if (track.type === 'subtitle') {
182
- const supportedSubtitleCodecs = this.format.getSupportedSubtitleCodecs();
183
- if (supportedSubtitleCodecs.length === 0) {
184
- throw new Error(`${this.format._name} does not support subtitle tracks.`
185
- + this.format._codecUnsupportedHint(track.source._codec));
186
- }
187
- else if (!supportedSubtitleCodecs.includes(track.source._codec)) {
188
- throw new Error(`Codec '${track.source._codec}' cannot be contained within ${this.format._name}. Supported`
189
- + ` subtitle codecs are: ${supportedSubtitleCodecs.map(codec => `'${codec}'`).join(', ')}.`
190
- + this.format._codecUnsupportedHint(track.source._codec));
191
- }
192
- }
193
- this._tracks.push(track);
194
- source._connectedTrack = track;
195
- }
196
- /**
197
- * Starts the creation of the output file. This method should be called after all tracks have been added. Only after
198
- * the output has started can media samples be added to the tracks.
199
- *
200
- * @returns A promise that resolves when the output has successfully started and is ready to receive media samples.
201
- */
202
- async start() {
203
- // Verify minimum track count constraints
204
- const supportedTrackCounts = this.format.getSupportedTrackCounts();
205
- for (const trackType of ALL_TRACK_TYPES) {
206
- const presentTracksOfThisType = this._tracks.reduce((count, track) => count + (track.type === trackType ? 1 : 0), 0);
207
- const minCount = supportedTrackCounts[trackType].min;
208
- if (presentTracksOfThisType < minCount) {
209
- throw new Error(minCount === supportedTrackCounts[trackType].max
210
- ? (`${this.format._name} requires exactly ${minCount} ${trackType}`
211
- + ` track${minCount === 1 ? '' : 's'}.`)
212
- : (`${this.format._name} requires at least ${minCount} ${trackType}`
213
- + ` track${minCount === 1 ? '' : 's'}.`));
214
- }
215
- }
216
- const totalMinCount = supportedTrackCounts.total.min;
217
- if (this._tracks.length < totalMinCount) {
218
- throw new Error(totalMinCount === supportedTrackCounts.total.max
219
- ? (`${this.format._name} requires exactly ${totalMinCount} track`
220
- + `${totalMinCount === 1 ? '' : 's'}.`)
221
- : (`${this.format._name} requires at least ${totalMinCount} track`
222
- + `${totalMinCount === 1 ? '' : 's'}.`));
223
- }
224
- if (this.state === 'canceled') {
225
- throw new Error('Output has been canceled.');
226
- }
227
- if (this._startPromise) {
228
- console.warn('Output has already been started.');
229
- return this._startPromise;
230
- }
231
- return this._startPromise = (async () => {
232
- this.state = 'started';
233
- this._writer.start();
234
- const release = await this._mutex.acquire();
235
- await this._muxer.start();
236
- const promises = this._tracks.map(track => track.source._start());
237
- await Promise.all(promises);
238
- release();
239
- })();
240
- }
241
- /**
242
- * Resolves with the full MIME type of the output file, including track codecs.
243
- *
244
- * The returned promise will resolve only once the precise codec strings of all tracks are known.
245
- */
246
- getMimeType() {
247
- return this._muxer.getMimeType();
248
- }
249
- /**
250
- * Cancels the creation of the output file, releasing internal resources like encoders and preventing further
251
- * samples from being added.
252
- *
253
- * @returns A promise that resolves once all internal resources have been released.
254
- */
255
- async cancel() {
256
- if (this._cancelPromise) {
257
- console.warn('Output has already been canceled.');
258
- return this._cancelPromise;
259
- }
260
- else if (this.state === 'finalizing' || this.state === 'finalized') {
261
- console.warn('Output has already been finalized.');
262
- return;
263
- }
264
- return this._cancelPromise = (async () => {
265
- this.state = 'canceled';
266
- const release = await this._mutex.acquire();
267
- const promises = this._tracks.map(x => x.source._flushOrWaitForOngoingClose(true)); // Force close
268
- await Promise.all(promises);
269
- await this._writer.close();
270
- release();
271
- })();
272
- }
273
- /**
274
- * Finalizes the output file. This method must be called after all media samples across all tracks have been added.
275
- * Once the Promise returned by this method completes, the output file is ready.
276
- */
277
- async finalize() {
278
- if (this.state === 'pending') {
279
- throw new Error('Cannot finalize before starting.');
280
- }
281
- if (this.state === 'canceled') {
282
- throw new Error('Cannot finalize after canceling.');
283
- }
284
- if (this._finalizePromise) {
285
- console.warn('Output has already been finalized.');
286
- return this._finalizePromise;
287
- }
288
- return this._finalizePromise = (async () => {
289
- this.state = 'finalizing';
290
- const release = await this._mutex.acquire();
291
- const promises = this._tracks.map(x => x.source._flushOrWaitForOngoingClose(false));
292
- await Promise.all(promises);
293
- await this._muxer.finalize();
294
- await this._writer.flush();
295
- await this._writer.finalize();
296
- this.state = 'finalized';
297
- release();
298
- })();
299
- }
300
- }
@@ -1,182 +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 { SECOND_TO_MICROSECOND_FACTOR } from './misc.js';
9
- export const PLACEHOLDER_DATA = /* #__PURE__ */ new Uint8Array(0);
10
- /**
11
- * Represents an encoded chunk of media. Mainly used as an expressive wrapper around WebCodecs API's
12
- * [`EncodedVideoChunk`](https://developer.mozilla.org/en-US/docs/Web/API/EncodedVideoChunk) and
13
- * [`EncodedAudioChunk`](https://developer.mozilla.org/en-US/docs/Web/API/EncodedAudioChunk), but can also be used
14
- * standalone.
15
- * @group Packets
16
- * @public
17
- */
18
- export class EncodedPacket {
19
- /** Creates a new {@link EncodedPacket} from raw bytes and timing information. */
20
- constructor(
21
- /** The encoded data of this packet. */
22
- data,
23
- /** The type of this packet. */
24
- type,
25
- /**
26
- * The presentation timestamp of this packet in seconds. May be negative. Samples with negative end timestamps
27
- * should not be presented.
28
- */
29
- timestamp,
30
- /** The duration of this packet in seconds. */
31
- duration,
32
- /**
33
- * The sequence number indicates the decode order of the packets. Packet A must be decoded before packet B if A
34
- * has a lower sequence number than B. If two packets have the same sequence number, they are the same packet.
35
- * Otherwise, sequence numbers are arbitrary and are not guaranteed to have any meaning besides their relative
36
- * ordering. Negative sequence numbers mean the sequence number is undefined.
37
- */
38
- sequenceNumber = -1, byteLength, sideData) {
39
- this.data = data;
40
- this.type = type;
41
- this.timestamp = timestamp;
42
- this.duration = duration;
43
- this.sequenceNumber = sequenceNumber;
44
- if (data === PLACEHOLDER_DATA && byteLength === undefined) {
45
- throw new Error('Internal error: byteLength must be explicitly provided when constructing metadata-only packets.');
46
- }
47
- if (byteLength === undefined) {
48
- byteLength = data.byteLength;
49
- }
50
- if (!(data instanceof Uint8Array)) {
51
- throw new TypeError('data must be a Uint8Array.');
52
- }
53
- if (type !== 'key' && type !== 'delta') {
54
- throw new TypeError('type must be either "key" or "delta".');
55
- }
56
- if (!Number.isFinite(timestamp)) {
57
- throw new TypeError('timestamp must be a number.');
58
- }
59
- if (!Number.isFinite(duration) || duration < 0) {
60
- throw new TypeError('duration must be a non-negative number.');
61
- }
62
- if (!Number.isFinite(sequenceNumber)) {
63
- throw new TypeError('sequenceNumber must be a number.');
64
- }
65
- if (!Number.isInteger(byteLength) || byteLength < 0) {
66
- throw new TypeError('byteLength must be a non-negative integer.');
67
- }
68
- if (sideData !== undefined && (typeof sideData !== 'object' || !sideData)) {
69
- throw new TypeError('sideData, when provided, must be an object.');
70
- }
71
- if (sideData?.alpha !== undefined && !(sideData.alpha instanceof Uint8Array)) {
72
- throw new TypeError('sideData.alpha, when provided, must be a Uint8Array.');
73
- }
74
- if (sideData?.alphaByteLength !== undefined
75
- && (!Number.isInteger(sideData.alphaByteLength) || sideData.alphaByteLength < 0)) {
76
- throw new TypeError('sideData.alphaByteLength, when provided, must be a non-negative integer.');
77
- }
78
- this.byteLength = byteLength;
79
- this.sideData = sideData ?? {};
80
- if (this.sideData.alpha && this.sideData.alphaByteLength === undefined) {
81
- this.sideData.alphaByteLength = this.sideData.alpha.byteLength;
82
- }
83
- }
84
- /**
85
- * If this packet is a metadata-only packet. Metadata-only packets don't contain their packet data. They are the
86
- * result of retrieving packets with {@link PacketRetrievalOptions.metadataOnly} set to `true`.
87
- */
88
- get isMetadataOnly() {
89
- return this.data === PLACEHOLDER_DATA;
90
- }
91
- /** The timestamp of this packet in microseconds. */
92
- get microsecondTimestamp() {
93
- return Math.trunc(SECOND_TO_MICROSECOND_FACTOR * this.timestamp);
94
- }
95
- /** The duration of this packet in microseconds. */
96
- get microsecondDuration() {
97
- return Math.trunc(SECOND_TO_MICROSECOND_FACTOR * this.duration);
98
- }
99
- /** Converts this packet to an
100
- * [`EncodedVideoChunk`](https://developer.mozilla.org/en-US/docs/Web/API/EncodedVideoChunk) for use with the
101
- * WebCodecs API. */
102
- toEncodedVideoChunk() {
103
- if (this.isMetadataOnly) {
104
- throw new TypeError('Metadata-only packets cannot be converted to a video chunk.');
105
- }
106
- if (typeof EncodedVideoChunk === 'undefined') {
107
- throw new Error('Your browser does not support EncodedVideoChunk.');
108
- }
109
- return new EncodedVideoChunk({
110
- data: this.data,
111
- type: this.type,
112
- timestamp: this.microsecondTimestamp,
113
- duration: this.microsecondDuration,
114
- });
115
- }
116
- /**
117
- * Converts this packet to an
118
- * [`EncodedVideoChunk`](https://developer.mozilla.org/en-US/docs/Web/API/EncodedVideoChunk) for use with the
119
- * WebCodecs API, using the alpha side data instead of the color data. Throws if no alpha side data is defined.
120
- */
121
- alphaToEncodedVideoChunk(type = this.type) {
122
- if (!this.sideData.alpha) {
123
- throw new TypeError('This packet does not contain alpha side data.');
124
- }
125
- if (this.isMetadataOnly) {
126
- throw new TypeError('Metadata-only packets cannot be converted to a video chunk.');
127
- }
128
- if (typeof EncodedVideoChunk === 'undefined') {
129
- throw new Error('Your browser does not support EncodedVideoChunk.');
130
- }
131
- return new EncodedVideoChunk({
132
- data: this.sideData.alpha,
133
- type,
134
- timestamp: this.microsecondTimestamp,
135
- duration: this.microsecondDuration,
136
- });
137
- }
138
- /** Converts this packet to an
139
- * [`EncodedAudioChunk`](https://developer.mozilla.org/en-US/docs/Web/API/EncodedAudioChunk) for use with the
140
- * WebCodecs API. */
141
- toEncodedAudioChunk() {
142
- if (this.isMetadataOnly) {
143
- throw new TypeError('Metadata-only packets cannot be converted to an audio chunk.');
144
- }
145
- if (typeof EncodedAudioChunk === 'undefined') {
146
- throw new Error('Your browser does not support EncodedAudioChunk.');
147
- }
148
- return new EncodedAudioChunk({
149
- data: this.data,
150
- type: this.type,
151
- timestamp: this.microsecondTimestamp,
152
- duration: this.microsecondDuration,
153
- });
154
- }
155
- /**
156
- * Creates an {@link EncodedPacket} from an
157
- * [`EncodedVideoChunk`](https://developer.mozilla.org/en-US/docs/Web/API/EncodedVideoChunk) or
158
- * [`EncodedAudioChunk`](https://developer.mozilla.org/en-US/docs/Web/API/EncodedAudioChunk). This method is useful
159
- * for converting chunks from the WebCodecs API to `EncodedPacket` instances.
160
- */
161
- static fromEncodedChunk(chunk, sideData) {
162
- if (!(chunk instanceof EncodedVideoChunk || chunk instanceof EncodedAudioChunk)) {
163
- throw new TypeError('chunk must be an EncodedVideoChunk or EncodedAudioChunk.');
164
- }
165
- const data = new Uint8Array(chunk.byteLength);
166
- chunk.copyTo(data);
167
- return new EncodedPacket(data, chunk.type, chunk.timestamp / 1e6, (chunk.duration ?? 0) / 1e6, undefined, undefined, sideData);
168
- }
169
- /** Clones this packet while optionally updating timing information. */
170
- clone(options) {
171
- if (options !== undefined && (typeof options !== 'object' || options === null)) {
172
- throw new TypeError('options, when provided, must be an object.');
173
- }
174
- if (options?.timestamp !== undefined && !Number.isFinite(options.timestamp)) {
175
- throw new TypeError('options.timestamp, when provided, must be a number.');
176
- }
177
- if (options?.duration !== undefined && !Number.isFinite(options.duration)) {
178
- throw new TypeError('options.duration, when provided, must be a number.');
179
- }
180
- return new EncodedPacket(this.data, this.type, options?.timestamp ?? this.timestamp, options?.duration ?? this.duration, this.sequenceNumber, this.byteLength);
181
- }
182
- }
@@ -1,85 +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
- // https://github.com/dystopiancode/pcm-g711/blob/master/pcm-g711/g711.c
9
- export const toUlaw = (s16) => {
10
- const MULAW_MAX = 0x1FFF;
11
- const MULAW_BIAS = 33;
12
- let number = s16;
13
- let mask = 0x1000;
14
- let sign = 0;
15
- let position = 12;
16
- let lsb = 0;
17
- if (number < 0) {
18
- number = -number;
19
- sign = 0x80;
20
- }
21
- number += MULAW_BIAS;
22
- if (number > MULAW_MAX) {
23
- number = MULAW_MAX;
24
- }
25
- while ((number & mask) !== mask && position >= 5) {
26
- mask >>= 1;
27
- position--;
28
- }
29
- lsb = (number >> (position - 4)) & 0x0f;
30
- return ~(sign | ((position - 5) << 4) | lsb) & 0xFF;
31
- };
32
- export const fromUlaw = (u8) => {
33
- const MULAW_BIAS = 33;
34
- let sign = 0;
35
- let position = 0;
36
- let number = ~u8;
37
- if (number & 0x80) {
38
- number &= ~(1 << 7);
39
- sign = -1;
40
- }
41
- position = ((number & 0xF0) >> 4) + 5;
42
- const decoded = ((1 << position) | ((number & 0x0F) << (position - 4))
43
- | (1 << (position - 5))) - MULAW_BIAS;
44
- return (sign === 0) ? decoded : -decoded;
45
- };
46
- export const toAlaw = (s16) => {
47
- const ALAW_MAX = 0xFFF;
48
- let mask = 0x800;
49
- let sign = 0;
50
- let position = 11;
51
- let lsb = 0;
52
- let number = s16;
53
- if (number < 0) {
54
- number = -number;
55
- sign = 0x80;
56
- }
57
- if (number > ALAW_MAX) {
58
- number = ALAW_MAX;
59
- }
60
- while ((number & mask) !== mask && position >= 5) {
61
- mask >>= 1;
62
- position--;
63
- }
64
- lsb = (number >> ((position === 4) ? 1 : (position - 4))) & 0x0f;
65
- return (sign | ((position - 4) << 4) | lsb) ^ 0x55;
66
- };
67
- export const fromAlaw = (u8) => {
68
- let sign = 0x00;
69
- let position = 0;
70
- let number = u8 ^ 0x55;
71
- if (number & 0x80) {
72
- number &= ~(1 << 7);
73
- sign = -1;
74
- }
75
- position = ((number & 0xF0) >> 4) + 4;
76
- let decoded = 0;
77
- if (position !== 4) {
78
- decoded = ((1 << position) | ((number & 0x0F) << (position - 4))
79
- | (1 << (position - 5)));
80
- }
81
- else {
82
- decoded = (number << 1) | 1;
83
- }
84
- return (sign === 0) ? decoded : -decoded;
85
- };