@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
package/src/output.ts DELETED
@@ -1,488 +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 { AsyncMutex, isIso639Dash2LanguageCode, Rotation } from './misc';
10
- import { MetadataTags, TrackDisposition, validateMetadataTags, validateTrackDisposition } from './metadata';
11
- import { Muxer } from './muxer';
12
- import { OutputFormat } from './output-format';
13
- import { AudioSource, MediaSource, SubtitleSource, VideoSource } from './media-source';
14
- import { Target } from './target';
15
- import { Writer } from './writer';
16
-
17
- /**
18
- * The options for creating an Output object.
19
- * @group Output files
20
- * @public
21
- */
22
- export type OutputOptions<
23
- F extends OutputFormat = OutputFormat,
24
- T extends Target = Target,
25
- > = {
26
- /** The format of the output file. */
27
- format: F;
28
- /** The target to which the file will be written. */
29
- target: T;
30
- };
31
-
32
- /**
33
- * List of all track types.
34
- * @group Miscellaneous
35
- * @public
36
- */
37
- export const ALL_TRACK_TYPES = ['video', 'audio', 'subtitle'] as const;
38
- /**
39
- * Union type of all track types.
40
- * @group Miscellaneous
41
- * @public
42
- */
43
- export type TrackType = typeof ALL_TRACK_TYPES[number];
44
-
45
- export type OutputTrack = {
46
- id: number;
47
- output: Output;
48
- type: TrackType;
49
- } & ({
50
- type: 'video';
51
- source: VideoSource;
52
- metadata: VideoTrackMetadata;
53
- } | {
54
- type: 'audio';
55
- source: AudioSource;
56
- metadata: AudioTrackMetadata;
57
- } | {
58
- type: 'subtitle';
59
- source: SubtitleSource;
60
- metadata: SubtitleTrackMetadata;
61
- });
62
-
63
- export type OutputVideoTrack = OutputTrack & { type: 'video' };
64
- export type OutputAudioTrack = OutputTrack & { type: 'audio' };
65
- export type OutputSubtitleTrack = OutputTrack & { type: 'subtitle' };
66
-
67
- /**
68
- * Base track metadata, applicable to all tracks.
69
- * @group Output files
70
- * @public
71
- */
72
- export type BaseTrackMetadata = {
73
- /** The three-letter, ISO 639-2/T language code specifying the language of this track. */
74
- languageCode?: string;
75
- /** A user-defined name for this track, like "English" or "Director Commentary". */
76
- name?: string;
77
- /** The track's disposition, i.e. information about its intended usage. */
78
- disposition?: Partial<TrackDisposition>;
79
- /**
80
- * The maximum amount of encoded packets that will be added to this track. Setting this field provides the muxer
81
- * with an additional signal that it can use to preallocate space in the file.
82
- *
83
- * When this field is set, it is an error to provide more packets than whatever this field specifies.
84
- *
85
- * Predicting the maximum packet count requires considering both the maximum duration as well as the codec.
86
- * - For video codecs, you can assume one packet per frame.
87
- * - For audio codecs, there is one packet for each "audio chunk", the duration of which depends on the codec. For
88
- * simplicity, you can assume each packet is roughly 10 ms or 512 samples long, whichever is shorter.
89
- * - For subtitles, assume each cue and each gap in the subtitles adds a packet.
90
- *
91
- * If you're not fully sure, make sure to add a buffer of around 33% to make sure you stay below the maximum.
92
- */
93
- maximumPacketCount?: number;
94
- };
95
-
96
- /**
97
- * Additional metadata for video tracks.
98
- * @group Output files
99
- * @public
100
- */
101
- export type VideoTrackMetadata = BaseTrackMetadata & {
102
- /** The angle in degrees by which the track's frames should be rotated (clockwise). */
103
- rotation?: Rotation;
104
- /**
105
- * The expected video frame rate in hertz. If set, all timestamps and durations of this track will be snapped to
106
- * this frame rate. You should avoid adding more frames than the rate allows, as this will lead to multiple frames
107
- * with the same timestamp.
108
- */
109
- frameRate?: number;
110
- };
111
- /**
112
- * Additional metadata for audio tracks.
113
- * @group Output files
114
- * @public
115
- */
116
- export type AudioTrackMetadata = BaseTrackMetadata & {};
117
- /**
118
- * Additional metadata for subtitle tracks.
119
- * @group Output files
120
- * @public
121
- */
122
- export type SubtitleTrackMetadata = BaseTrackMetadata & {};
123
-
124
- const validateBaseTrackMetadata = (metadata: BaseTrackMetadata) => {
125
- if (!metadata || typeof metadata !== 'object') {
126
- throw new TypeError('metadata must be an object.');
127
- }
128
- if (metadata.languageCode !== undefined && !isIso639Dash2LanguageCode(metadata.languageCode)) {
129
- throw new TypeError('metadata.languageCode, when provided, must be a three-letter, ISO 639-2/T language code.');
130
- }
131
- if (metadata.name !== undefined && typeof metadata.name !== 'string') {
132
- throw new TypeError('metadata.name, when provided, must be a string.');
133
- }
134
- if (metadata.disposition !== undefined) {
135
- validateTrackDisposition(metadata.disposition);
136
- }
137
- if (
138
- metadata.maximumPacketCount !== undefined
139
- && (!Number.isInteger(metadata.maximumPacketCount) || metadata.maximumPacketCount < 0)
140
- ) {
141
- throw new TypeError('metadata.maximumPacketCount, when provided, must be a non-negative integer.');
142
- }
143
- };
144
-
145
- /**
146
- * Main class orchestrating the creation of a new media file.
147
- * @group Output files
148
- * @public
149
- */
150
- export class Output<
151
- F extends OutputFormat = OutputFormat,
152
- T extends Target = Target,
153
- > {
154
- /** The format of the output file. */
155
- format: F;
156
- /** The target to which the file will be written. */
157
- target: T;
158
- /** The current state of the output. */
159
- state: 'pending' | 'started' | 'canceled' | 'finalizing' | 'finalized' = 'pending';
160
-
161
- /** @internal */
162
- _muxer: Muxer;
163
- /** @internal */
164
- _writer: Writer;
165
- /** @internal */
166
- _tracks: OutputTrack[] = [];
167
- /** @internal */
168
- _startPromise: Promise<void> | null = null;
169
- /** @internal */
170
- _cancelPromise: Promise<void> | null = null;
171
- /** @internal */
172
- _finalizePromise: Promise<void> | null = null;
173
- /** @internal */
174
- _mutex = new AsyncMutex();
175
- /** @internal */
176
- _metadataTags: MetadataTags = {};
177
-
178
- /**
179
- * Creates a new instance of {@link Output} which can then be used to create a new media file according to the
180
- * specified {@link OutputOptions}.
181
- */
182
- constructor(options: OutputOptions<F, T>) {
183
- if (!options || typeof options !== 'object') {
184
- throw new TypeError('options must be an object.');
185
- }
186
- if (!(options.format instanceof OutputFormat)) {
187
- throw new TypeError('options.format must be an OutputFormat.');
188
- }
189
- if (!(options.target instanceof Target)) {
190
- throw new TypeError('options.target must be a Target.');
191
- }
192
-
193
- if (options.target._output) {
194
- throw new Error('Target is already used for another output.');
195
- }
196
- options.target._output = this;
197
-
198
- this.format = options.format;
199
- this.target = options.target;
200
-
201
- this._writer = options.target._createWriter();
202
- this._muxer = options.format._createMuxer(this);
203
- }
204
-
205
- /** Adds a video track to the output with the given source. Can only be called before the output is started. */
206
- addVideoTrack(source: VideoSource, metadata: VideoTrackMetadata = {}) {
207
- if (!(source instanceof VideoSource)) {
208
- throw new TypeError('source must be a VideoSource.');
209
- }
210
- validateBaseTrackMetadata(metadata);
211
- if (metadata.rotation !== undefined && ![0, 90, 180, 270].includes(metadata.rotation)) {
212
- throw new TypeError(`Invalid video rotation: ${metadata.rotation}. Has to be 0, 90, 180 or 270.`);
213
- }
214
- if (!this.format.supportsVideoRotationMetadata && metadata.rotation) {
215
- throw new Error(`${this.format._name} does not support video rotation metadata.`);
216
- }
217
- if (
218
- metadata.frameRate !== undefined
219
- && (!Number.isFinite(metadata.frameRate) || metadata.frameRate <= 0)
220
- ) {
221
- throw new TypeError(
222
- `Invalid video frame rate: ${metadata.frameRate}. Must be a positive number.`,
223
- );
224
- }
225
-
226
- this._addTrack('video', source, metadata);
227
- }
228
-
229
- /** Adds an audio track to the output with the given source. Can only be called before the output is started. */
230
- addAudioTrack(source: AudioSource, metadata: AudioTrackMetadata = {}) {
231
- if (!(source instanceof AudioSource)) {
232
- throw new TypeError('source must be an AudioSource.');
233
- }
234
- validateBaseTrackMetadata(metadata);
235
-
236
- this._addTrack('audio', source, metadata);
237
- }
238
-
239
- /** Adds a subtitle track to the output with the given source. Can only be called before the output is started. */
240
- addSubtitleTrack(source: SubtitleSource, metadata: SubtitleTrackMetadata = {}) {
241
- if (!(source instanceof SubtitleSource)) {
242
- throw new TypeError('source must be a SubtitleSource.');
243
- }
244
- validateBaseTrackMetadata(metadata);
245
-
246
- this._addTrack('subtitle', source, metadata);
247
- }
248
-
249
- /**
250
- * Sets descriptive metadata tags about the media file, such as title, author, date, or cover art. When called
251
- * multiple times, only the metadata from the last call will be used.
252
- *
253
- * Can only be called before the output is started.
254
- */
255
- setMetadataTags(tags: MetadataTags) {
256
- validateMetadataTags(tags);
257
-
258
- if (this.state !== 'pending') {
259
- throw new Error('Cannot set metadata tags after output has been started or canceled.');
260
- }
261
-
262
- this._metadataTags = tags;
263
- }
264
-
265
- /** @internal */
266
- private _addTrack(type: OutputTrack['type'], source: MediaSource, metadata: object) {
267
- if (this.state !== 'pending') {
268
- throw new Error('Cannot add track after output has been started or canceled.');
269
- }
270
- if (source._connectedTrack) {
271
- throw new Error('Source is already used for a track.');
272
- }
273
-
274
- // Verify maximum track count constraints
275
- const supportedTrackCounts = this.format.getSupportedTrackCounts();
276
- const presentTracksOfThisType = this._tracks.reduce(
277
- (count, track) => count + (track.type === type ? 1 : 0),
278
- 0,
279
- );
280
- const maxCount = supportedTrackCounts[type].max;
281
- if (presentTracksOfThisType === maxCount) {
282
- throw new Error(
283
- maxCount === 0
284
- ? `${this.format._name} does not support ${type} tracks.`
285
- : (`${this.format._name} does not support more than ${maxCount} ${type} track`
286
- + `${maxCount === 1 ? '' : 's'}.`),
287
- );
288
- }
289
- const maxTotalCount = supportedTrackCounts.total.max;
290
- if (this._tracks.length === maxTotalCount) {
291
- throw new Error(
292
- `${this.format._name} does not support more than ${maxTotalCount} tracks`
293
- + `${maxTotalCount === 1 ? '' : 's'} in total.`,
294
- );
295
- }
296
-
297
- const track = {
298
- id: this._tracks.length + 1,
299
- output: this,
300
- type,
301
- source: source as unknown,
302
- metadata,
303
- } as OutputTrack;
304
-
305
- if (track.type === 'video') {
306
- const supportedVideoCodecs = this.format.getSupportedVideoCodecs();
307
-
308
- if (supportedVideoCodecs.length === 0) {
309
- throw new Error(
310
- `${this.format._name} does not support video tracks.`
311
- + this.format._codecUnsupportedHint(track.source._codec),
312
- );
313
- } else if (!supportedVideoCodecs.includes(track.source._codec)) {
314
- throw new Error(
315
- `Codec '${track.source._codec}' cannot be contained within ${this.format._name}. Supported`
316
- + ` video codecs are: ${supportedVideoCodecs.map(codec => `'${codec}'`).join(', ')}.`
317
- + this.format._codecUnsupportedHint(track.source._codec),
318
- );
319
- }
320
- } else if (track.type === 'audio') {
321
- const supportedAudioCodecs = this.format.getSupportedAudioCodecs();
322
-
323
- if (supportedAudioCodecs.length === 0) {
324
- throw new Error(
325
- `${this.format._name} does not support audio tracks.`
326
- + this.format._codecUnsupportedHint(track.source._codec),
327
- );
328
- } else if (!supportedAudioCodecs.includes(track.source._codec)) {
329
- throw new Error(
330
- `Codec '${track.source._codec}' cannot be contained within ${this.format._name}. Supported`
331
- + ` audio codecs are: ${supportedAudioCodecs.map(codec => `'${codec}'`).join(', ')}.`
332
- + this.format._codecUnsupportedHint(track.source._codec),
333
- );
334
- }
335
- } else if (track.type === 'subtitle') {
336
- const supportedSubtitleCodecs = this.format.getSupportedSubtitleCodecs();
337
-
338
- if (supportedSubtitleCodecs.length === 0) {
339
- throw new Error(
340
- `${this.format._name} does not support subtitle tracks.`
341
- + this.format._codecUnsupportedHint(track.source._codec),
342
- );
343
- } else if (!supportedSubtitleCodecs.includes(track.source._codec)) {
344
- throw new Error(
345
- `Codec '${track.source._codec}' cannot be contained within ${this.format._name}. Supported`
346
- + ` subtitle codecs are: ${supportedSubtitleCodecs.map(codec => `'${codec}'`).join(', ')}.`
347
- + this.format._codecUnsupportedHint(track.source._codec),
348
- );
349
- }
350
- }
351
-
352
- this._tracks.push(track);
353
- source._connectedTrack = track;
354
- }
355
-
356
- /**
357
- * Starts the creation of the output file. This method should be called after all tracks have been added. Only after
358
- * the output has started can media samples be added to the tracks.
359
- *
360
- * @returns A promise that resolves when the output has successfully started and is ready to receive media samples.
361
- */
362
- async start() {
363
- // Verify minimum track count constraints
364
- const supportedTrackCounts = this.format.getSupportedTrackCounts();
365
- for (const trackType of ALL_TRACK_TYPES) {
366
- const presentTracksOfThisType = this._tracks.reduce(
367
- (count, track) => count + (track.type === trackType ? 1 : 0),
368
- 0,
369
- );
370
- const minCount = supportedTrackCounts[trackType].min;
371
- if (presentTracksOfThisType < minCount) {
372
- throw new Error(
373
- minCount === supportedTrackCounts[trackType].max
374
- ? (`${this.format._name} requires exactly ${minCount} ${trackType}`
375
- + ` track${minCount === 1 ? '' : 's'}.`)
376
- : (`${this.format._name} requires at least ${minCount} ${trackType}`
377
- + ` track${minCount === 1 ? '' : 's'}.`),
378
- );
379
- }
380
- }
381
- const totalMinCount = supportedTrackCounts.total.min;
382
- if (this._tracks.length < totalMinCount) {
383
- throw new Error(
384
- totalMinCount === supportedTrackCounts.total.max
385
- ? (`${this.format._name} requires exactly ${totalMinCount} track`
386
- + `${totalMinCount === 1 ? '' : 's'}.`)
387
- : (`${this.format._name} requires at least ${totalMinCount} track`
388
- + `${totalMinCount === 1 ? '' : 's'}.`),
389
- );
390
- }
391
-
392
- if (this.state === 'canceled') {
393
- throw new Error('Output has been canceled.');
394
- }
395
-
396
- if (this._startPromise) {
397
- console.warn('Output has already been started.');
398
- return this._startPromise;
399
- }
400
-
401
- return this._startPromise = (async () => {
402
- this.state = 'started';
403
- this._writer.start();
404
-
405
- const release = await this._mutex.acquire();
406
-
407
- await this._muxer.start();
408
-
409
- const promises = this._tracks.map(track => track.source._start());
410
- await Promise.all(promises);
411
-
412
- release();
413
- })();
414
- }
415
-
416
- /**
417
- * Resolves with the full MIME type of the output file, including track codecs.
418
- *
419
- * The returned promise will resolve only once the precise codec strings of all tracks are known.
420
- */
421
- getMimeType() {
422
- return this._muxer.getMimeType();
423
- }
424
-
425
- /**
426
- * Cancels the creation of the output file, releasing internal resources like encoders and preventing further
427
- * samples from being added.
428
- *
429
- * @returns A promise that resolves once all internal resources have been released.
430
- */
431
- async cancel() {
432
- if (this._cancelPromise) {
433
- console.warn('Output has already been canceled.');
434
- return this._cancelPromise;
435
- } else if (this.state === 'finalizing' || this.state === 'finalized') {
436
- console.warn('Output has already been finalized.');
437
- return;
438
- }
439
-
440
- return this._cancelPromise = (async () => {
441
- this.state = 'canceled';
442
-
443
- const release = await this._mutex.acquire();
444
-
445
- const promises = this._tracks.map(x => x.source._flushOrWaitForOngoingClose(true)); // Force close
446
- await Promise.all(promises);
447
-
448
- await this._writer.close();
449
-
450
- release();
451
- })();
452
- }
453
-
454
- /**
455
- * Finalizes the output file. This method must be called after all media samples across all tracks have been added.
456
- * Once the Promise returned by this method completes, the output file is ready.
457
- */
458
- async finalize() {
459
- if (this.state === 'pending') {
460
- throw new Error('Cannot finalize before starting.');
461
- }
462
- if (this.state === 'canceled') {
463
- throw new Error('Cannot finalize after canceling.');
464
- }
465
- if (this._finalizePromise) {
466
- console.warn('Output has already been finalized.');
467
- return this._finalizePromise;
468
- }
469
-
470
- return this._finalizePromise = (async () => {
471
- this.state = 'finalizing';
472
-
473
- const release = await this._mutex.acquire();
474
-
475
- const promises = this._tracks.map(x => x.source._flushOrWaitForOngoingClose(false));
476
- await Promise.all(promises);
477
-
478
- await this._muxer.finalize();
479
-
480
- await this._writer.flush();
481
- await this._writer.finalize();
482
-
483
- this.state = 'finalized';
484
-
485
- release();
486
- })();
487
- }
488
- }