@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,1348 +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 { Box, free, ftyp, IsobmffBoxWriter, mdat, mfra, moof, moov, vtta, vttc, vtte } from './isobmff-boxes';
10
- import { Muxer } from '../muxer';
11
- import { Output, OutputAudioTrack, OutputSubtitleTrack, OutputTrack, OutputVideoTrack } from '../output';
12
- import { BufferTargetWriter, Writer } from '../writer';
13
- import { assert, computeRationalApproximation, last, promiseWithResolvers } from '../misc';
14
- import { IsobmffOutputFormatOptions, IsobmffOutputFormat, MovOutputFormat } from '../output-format';
15
- import { inlineTimestampRegex, SubtitleConfig, SubtitleCue, SubtitleMetadata } from '../subtitles';
16
- import {
17
- parsePcmCodec,
18
- PCM_AUDIO_CODECS,
19
- PcmAudioCodec,
20
- SubtitleCodec,
21
- validateAudioChunkMetadata,
22
- validateSubtitleMetadata,
23
- validateVideoChunkMetadata,
24
- } from '../codec';
25
- import { BufferTarget } from '../target';
26
- import { EncodedPacket, PacketType } from '../packet';
27
- import {
28
- extractAvcDecoderConfigurationRecord,
29
- extractHevcDecoderConfigurationRecord,
30
- serializeAvcDecoderConfigurationRecord,
31
- serializeHevcDecoderConfigurationRecord,
32
- transformAnnexBToLengthPrefixed,
33
- } from '../codec-data';
34
- import { buildIsobmffMimeType } from './isobmff-misc';
35
- import { MAX_BOX_HEADER_SIZE, MIN_BOX_HEADER_SIZE } from './isobmff-reader';
36
-
37
- export const GLOBAL_TIMESCALE = 1000;
38
- const TIMESTAMP_OFFSET = 2_082_844_800; // Seconds between Jan 1 1904 and Jan 1 1970
39
-
40
- export type Sample = {
41
- timestamp: number;
42
- decodeTimestamp: number;
43
- duration: number;
44
- data: Uint8Array | null;
45
- size: number;
46
- type: PacketType;
47
- timescaleUnitsToNextSample: number;
48
- };
49
-
50
- type Chunk = {
51
- /** The lowest presentation timestamp in this chunk */
52
- startTimestamp: number;
53
- samples: Sample[];
54
- offset: number | null;
55
- // In the case of a fragmented file, this indicates the position of the moof box pointing to the data in this chunk
56
- moofOffset: number | null;
57
- };
58
-
59
- export type IsobmffTrackData = {
60
- muxer: IsobmffMuxer;
61
- timescale: number;
62
- samples: Sample[];
63
- sampleQueue: Sample[]; // For fragmented files
64
- timestampProcessingQueue: Sample[];
65
-
66
- timeToSampleTable: { sampleCount: number; sampleDelta: number }[];
67
- compositionTimeOffsetTable: { sampleCount: number; sampleCompositionTimeOffset: number }[];
68
- lastTimescaleUnits: number | null;
69
- lastSample: Sample | null;
70
-
71
- finalizedChunks: Chunk[];
72
- currentChunk: Chunk | null;
73
- compactlyCodedChunkTable: {
74
- firstChunk: number;
75
- samplesPerChunk: number;
76
- }[];
77
- } & ({
78
- track: OutputVideoTrack;
79
- type: 'video';
80
- info: {
81
- width: number;
82
- height: number;
83
- decoderConfig: VideoDecoderConfig;
84
- /**
85
- * The "Annex B transformation" involves converting the raw packet data from Annex B to
86
- * "MP4" (length-prefixed) format.
87
- * https://stackoverflow.com/questions/24884827
88
- */
89
- requiresAnnexBTransformation: boolean;
90
- };
91
- } | {
92
- track: OutputAudioTrack;
93
- type: 'audio';
94
- info: {
95
- numberOfChannels: number;
96
- sampleRate: number;
97
- decoderConfig: AudioDecoderConfig;
98
- /**
99
- * The "PCM transformation" is making every sample in the sample table be exactly one PCM audio sample long.
100
- * Some players expect this for PCM audio.
101
- */
102
- requiresPcmTransformation: boolean;
103
- };
104
- } | {
105
- track: OutputSubtitleTrack;
106
- type: 'subtitle';
107
- info: {
108
- config: SubtitleConfig;
109
- };
110
- lastCueEndTimestamp: number;
111
- cueQueue: SubtitleCue[];
112
- nextSourceId: number;
113
- cueToSourceId: WeakMap<SubtitleCue, number>;
114
- });
115
-
116
- export type IsobmffVideoTrackData = IsobmffTrackData & { type: 'video' };
117
- export type IsobmffAudioTrackData = IsobmffTrackData & { type: 'audio' };
118
- export type IsobmffSubtitleTrackData = IsobmffTrackData & { type: 'subtitle' };
119
-
120
- export type IsobmffMetadata = {
121
- name?: string;
122
- };
123
-
124
- export const getTrackMetadata = (trackData: IsobmffTrackData) => {
125
- const metadata: IsobmffMetadata = {};
126
- const track = trackData.track as OutputTrack;
127
-
128
- if (track.metadata.name !== undefined) {
129
- metadata.name = track.metadata.name;
130
- }
131
-
132
- return metadata;
133
- };
134
-
135
- export const intoTimescale = (timeInSeconds: number, timescale: number, round = true) => {
136
- const value = timeInSeconds * timescale;
137
- return round ? Math.round(value) : value;
138
- };
139
-
140
- export class IsobmffMuxer extends Muxer {
141
- format: IsobmffOutputFormat;
142
- private writer: Writer;
143
- private boxWriter: IsobmffBoxWriter;
144
- private fastStart: NonNullable<IsobmffOutputFormatOptions['fastStart']>;
145
- isFragmented: boolean;
146
-
147
- isQuickTime: boolean;
148
-
149
- private auxTarget = new BufferTarget();
150
- private auxWriter = this.auxTarget._createWriter();
151
- private auxBoxWriter = new IsobmffBoxWriter(this.auxWriter);
152
-
153
- private mdat: Box | null = null;
154
- private ftypSize: number | null = null;
155
-
156
- trackDatas: IsobmffTrackData[] = [];
157
- private allTracksKnown = promiseWithResolvers();
158
-
159
- creationTime = Math.floor(Date.now() / 1000) + TIMESTAMP_OFFSET;
160
- private finalizedChunks: Chunk[] = [];
161
-
162
- private nextFragmentNumber = 1;
163
- // Only relevant for fragmented files, to make sure new fragments start with the highest timestamp seen so far
164
- private maxWrittenTimestamp = -Infinity;
165
- private minimumFragmentDuration: number;
166
-
167
- constructor(output: Output, format: IsobmffOutputFormat) {
168
- super(output);
169
-
170
- this.format = format;
171
- this.writer = output._writer;
172
- this.boxWriter = new IsobmffBoxWriter(this.writer);
173
-
174
- this.isQuickTime = format instanceof MovOutputFormat;
175
-
176
- // If the fastStart option isn't defined, enable in-memory fast start if the target is an ArrayBuffer, as the
177
- // memory usage remains identical
178
- const fastStartDefault = this.writer instanceof BufferTargetWriter ? 'in-memory' : false;
179
- this.fastStart = format._options.fastStart ?? fastStartDefault;
180
- this.isFragmented = this.fastStart === 'fragmented';
181
-
182
- if (this.fastStart === 'in-memory' || this.isFragmented) {
183
- this.writer.ensureMonotonicity = true;
184
- }
185
-
186
- this.minimumFragmentDuration = format._options.minimumFragmentDuration ?? 1;
187
- }
188
-
189
- async start() {
190
- const release = await this.mutex.acquire();
191
-
192
- const holdsAvc = this.output._tracks.some(x => x.type === 'video' && x.source._codec === 'avc');
193
-
194
- // Write the header
195
- {
196
- if (this.format._options.onFtyp) {
197
- this.writer.startTrackingWrites();
198
- }
199
-
200
- this.boxWriter.writeBox(ftyp({
201
- isQuickTime: this.isQuickTime,
202
- holdsAvc: holdsAvc,
203
- fragmented: this.isFragmented,
204
- }));
205
-
206
- if (this.format._options.onFtyp) {
207
- const { data, start } = this.writer.stopTrackingWrites();
208
- this.format._options.onFtyp(data, start);
209
- }
210
- }
211
-
212
- this.ftypSize = this.writer.getPos();
213
-
214
- if (this.fastStart === 'in-memory') {
215
- // We're write at finalization
216
- } else if (this.fastStart === 'reserve') {
217
- // Validate that all tracks have set maximumPacketCount
218
- for (const track of this.output._tracks) {
219
- if (track.metadata.maximumPacketCount === undefined) {
220
- throw new Error(
221
- 'All tracks must specify maximumPacketCount in their metadata when using'
222
- + ' fastStart: \'reserve\'.',
223
- );
224
- }
225
- }
226
-
227
- // We'll start writing once we know all tracks
228
- } else if (this.isFragmented) {
229
- // We write the moov box once we write out the first fragment to make sure we get the decoder configs
230
- } else {
231
- if (this.format._options.onMdat) {
232
- this.writer.startTrackingWrites();
233
- }
234
-
235
- this.mdat = mdat(true); // Reserve large size by default, can refine this when finalizing.
236
- this.boxWriter.writeBox(this.mdat);
237
- }
238
-
239
- await this.writer.flush();
240
-
241
- release();
242
- }
243
-
244
- private allTracksAreKnown() {
245
- for (const track of this.output._tracks) {
246
- if (!track.source._closed && !this.trackDatas.some(x => x.track === track)) {
247
- return false; // We haven't seen a sample from this open track yet
248
- }
249
- }
250
-
251
- return true;
252
- }
253
-
254
- async getMimeType() {
255
- await this.allTracksKnown.promise;
256
-
257
- const codecStrings = this.trackDatas.map((trackData) => {
258
- if (trackData.type === 'video') {
259
- return trackData.info.decoderConfig.codec;
260
- } else if (trackData.type === 'audio') {
261
- return trackData.info.decoderConfig.codec;
262
- } else {
263
- const map: Record<SubtitleCodec, string> = {
264
- webvtt: 'wvtt',
265
- tx3g: 'tx3g',
266
- ttml: 'stpp',
267
- srt: 'wvtt', // MP4 stores SRT as WebVTT
268
- ass: 'wvtt', // MP4 stores ASS as WebVTT
269
- ssa: 'wvtt', // MP4 stores SSA as WebVTT
270
- };
271
- return map[trackData.track.source._codec];
272
- }
273
- });
274
-
275
- return buildIsobmffMimeType({
276
- isQuickTime: this.isQuickTime,
277
- hasVideo: this.trackDatas.some(x => x.type === 'video'),
278
- hasAudio: this.trackDatas.some(x => x.type === 'audio'),
279
- codecStrings,
280
- });
281
- }
282
-
283
- private getVideoTrackData(track: OutputVideoTrack, packet: EncodedPacket, meta?: EncodedVideoChunkMetadata) {
284
- const existingTrackData = this.trackDatas.find(x => x.track === track);
285
- if (existingTrackData) {
286
- return existingTrackData as IsobmffVideoTrackData;
287
- }
288
-
289
- validateVideoChunkMetadata(meta);
290
-
291
- assert(meta);
292
- assert(meta.decoderConfig);
293
-
294
- const decoderConfig = { ...meta.decoderConfig };
295
- assert(decoderConfig.codedWidth !== undefined);
296
- assert(decoderConfig.codedHeight !== undefined);
297
-
298
- let requiresAnnexBTransformation = false;
299
-
300
- if (track.source._codec === 'avc' && !decoderConfig.description) {
301
- // ISOBMFF can only hold AVC in the AVCC format, not in Annex B, but the missing description indicates
302
- // Annex B. This means we'll need to do some converterino.
303
-
304
- const decoderConfigurationRecord = extractAvcDecoderConfigurationRecord(packet.data);
305
- if (!decoderConfigurationRecord) {
306
- throw new Error(
307
- 'Couldn\'t extract an AVCDecoderConfigurationRecord from the AVC packet. Make sure the packets are'
308
- + ' in Annex B format (as specified in ITU-T-REC-H.264) when not providing a description, or'
309
- + ' provide a description (must be an AVCDecoderConfigurationRecord as specified in ISO 14496-15)'
310
- + ' and ensure the packets are in AVCC format.',
311
- );
312
- }
313
-
314
- decoderConfig.description = serializeAvcDecoderConfigurationRecord(decoderConfigurationRecord);
315
- requiresAnnexBTransformation = true;
316
- } else if (track.source._codec === 'hevc' && !decoderConfig.description) {
317
- // ISOBMFF can only hold HEVC in the HEVC format, not in Annex B, but the missing description indicates
318
- // Annex B. This means we'll need to do some converterino.
319
-
320
- const decoderConfigurationRecord = extractHevcDecoderConfigurationRecord(packet.data);
321
- if (!decoderConfigurationRecord) {
322
- throw new Error(
323
- 'Couldn\'t extract an HEVCDecoderConfigurationRecord from the HEVC packet. Make sure the packets'
324
- + ' are in Annex B format (as specified in ITU-T-REC-H.265) when not providing a description, or'
325
- + ' provide a description (must be an HEVCDecoderConfigurationRecord as specified in ISO 14496-15)'
326
- + ' and ensure the packets are in HEVC format.',
327
- );
328
- }
329
-
330
- decoderConfig.description = serializeHevcDecoderConfigurationRecord(decoderConfigurationRecord);
331
- requiresAnnexBTransformation = true;
332
- }
333
-
334
- // The frame rate set by the user may not be an integer. Since timescale is an integer, we'll approximate the
335
- // frame time (inverse of frame rate) with a rational number, then use that approximation's denominator
336
- // as the timescale.
337
- const timescale = computeRationalApproximation(1 / (track.metadata.frameRate ?? 57600), 1e6).denominator;
338
-
339
- const newTrackData: IsobmffVideoTrackData = {
340
- muxer: this,
341
- track,
342
- type: 'video',
343
- info: {
344
- width: decoderConfig.codedWidth,
345
- height: decoderConfig.codedHeight,
346
- decoderConfig: decoderConfig,
347
- requiresAnnexBTransformation,
348
- },
349
- timescale,
350
- samples: [],
351
- sampleQueue: [],
352
- timestampProcessingQueue: [],
353
- timeToSampleTable: [],
354
- compositionTimeOffsetTable: [],
355
- lastTimescaleUnits: null,
356
- lastSample: null,
357
- finalizedChunks: [],
358
- currentChunk: null,
359
- compactlyCodedChunkTable: [],
360
- };
361
-
362
- this.trackDatas.push(newTrackData);
363
- this.trackDatas.sort((a, b) => a.track.id - b.track.id);
364
-
365
- if (this.allTracksAreKnown()) {
366
- this.allTracksKnown.resolve();
367
- }
368
-
369
- return newTrackData;
370
- }
371
-
372
- private getAudioTrackData(track: OutputAudioTrack, meta?: EncodedAudioChunkMetadata) {
373
- const existingTrackData = this.trackDatas.find(x => x.track === track);
374
- if (existingTrackData) {
375
- return existingTrackData as IsobmffAudioTrackData;
376
- }
377
-
378
- validateAudioChunkMetadata(meta);
379
-
380
- assert(meta);
381
- assert(meta.decoderConfig);
382
-
383
- const newTrackData: IsobmffAudioTrackData = {
384
- muxer: this,
385
- track,
386
- type: 'audio',
387
- info: {
388
- numberOfChannels: meta.decoderConfig.numberOfChannels,
389
- sampleRate: meta.decoderConfig.sampleRate,
390
- decoderConfig: meta.decoderConfig,
391
- requiresPcmTransformation:
392
- !this.isFragmented
393
- && (PCM_AUDIO_CODECS as readonly string[]).includes(track.source._codec),
394
- },
395
- timescale: meta.decoderConfig.sampleRate,
396
- samples: [],
397
- sampleQueue: [],
398
- timestampProcessingQueue: [],
399
- timeToSampleTable: [],
400
- compositionTimeOffsetTable: [],
401
- lastTimescaleUnits: null,
402
- lastSample: null,
403
- finalizedChunks: [],
404
- currentChunk: null,
405
- compactlyCodedChunkTable: [],
406
- };
407
-
408
- this.trackDatas.push(newTrackData);
409
- this.trackDatas.sort((a, b) => a.track.id - b.track.id);
410
-
411
- if (this.allTracksAreKnown()) {
412
- this.allTracksKnown.resolve();
413
- }
414
-
415
- return newTrackData;
416
- }
417
-
418
- private getSubtitleTrackData(track: OutputSubtitleTrack, meta?: SubtitleMetadata) {
419
- const existingTrackData = this.trackDatas.find(x => x.track === track);
420
- if (existingTrackData) {
421
- return existingTrackData as IsobmffSubtitleTrackData;
422
- }
423
-
424
- validateSubtitleMetadata(meta);
425
-
426
- assert(meta);
427
- assert(meta.config);
428
-
429
- const newTrackData: IsobmffSubtitleTrackData = {
430
- muxer: this,
431
- track,
432
- type: 'subtitle',
433
- info: {
434
- config: meta.config,
435
- },
436
- timescale: 1000, // Reasonable
437
- samples: [],
438
- sampleQueue: [],
439
- timestampProcessingQueue: [],
440
- timeToSampleTable: [],
441
- compositionTimeOffsetTable: [],
442
- lastTimescaleUnits: null,
443
- lastSample: null,
444
- finalizedChunks: [],
445
- currentChunk: null,
446
- compactlyCodedChunkTable: [],
447
-
448
- lastCueEndTimestamp: 0,
449
- cueQueue: [],
450
- nextSourceId: 0,
451
- cueToSourceId: new WeakMap(),
452
- };
453
-
454
- this.trackDatas.push(newTrackData);
455
- this.trackDatas.sort((a, b) => a.track.id - b.track.id);
456
-
457
- if (this.allTracksAreKnown()) {
458
- this.allTracksKnown.resolve();
459
- }
460
-
461
- return newTrackData;
462
- }
463
-
464
- async addEncodedVideoPacket(track: OutputVideoTrack, packet: EncodedPacket, meta?: EncodedVideoChunkMetadata) {
465
- const release = await this.mutex.acquire();
466
-
467
- try {
468
- const trackData = this.getVideoTrackData(track, packet, meta);
469
-
470
- let packetData = packet.data;
471
- if (trackData.info.requiresAnnexBTransformation) {
472
- const transformedData = transformAnnexBToLengthPrefixed(packetData);
473
- if (!transformedData) {
474
- throw new Error(
475
- 'Failed to transform packet data. Make sure all packets are provided in Annex B format, as'
476
- + ' specified in ITU-T-REC-H.264 and ITU-T-REC-H.265.',
477
- );
478
- }
479
-
480
- packetData = transformedData;
481
- }
482
-
483
- const timestamp = this.validateAndNormalizeTimestamp(
484
- trackData.track,
485
- packet.timestamp,
486
- packet.type === 'key',
487
- );
488
- const internalSample = this.createSampleForTrack(
489
- trackData,
490
- packetData,
491
- timestamp,
492
- packet.duration,
493
- packet.type,
494
- );
495
-
496
- await this.registerSample(trackData, internalSample);
497
- } finally {
498
- release();
499
- }
500
- }
501
-
502
- async addEncodedAudioPacket(track: OutputAudioTrack, packet: EncodedPacket, meta?: EncodedAudioChunkMetadata) {
503
- const release = await this.mutex.acquire();
504
-
505
- try {
506
- const trackData = this.getAudioTrackData(track, meta);
507
-
508
- const timestamp = this.validateAndNormalizeTimestamp(
509
- trackData.track,
510
- packet.timestamp,
511
- packet.type === 'key',
512
- );
513
- const internalSample = this.createSampleForTrack(
514
- trackData,
515
- packet.data,
516
- timestamp,
517
- packet.duration,
518
- packet.type,
519
- );
520
-
521
- if (trackData.info.requiresPcmTransformation) {
522
- await this.maybePadWithSilence(trackData, timestamp);
523
- }
524
-
525
- await this.registerSample(trackData, internalSample);
526
- } finally {
527
- release();
528
- }
529
- }
530
-
531
- private async maybePadWithSilence(trackData: IsobmffAudioTrackData, untilTimestamp: number) {
532
- // The PCM transformation assumes that all samples are contiguous. This is not something that is enforced, so
533
- // we need to pad the "holes" in between samples (and before the first sample) with additional
534
- // "silence samples".
535
-
536
- const lastSample = last(trackData.samples);
537
- const lastEndTimestamp = lastSample
538
- ? lastSample.timestamp + lastSample.duration
539
- : 0;
540
-
541
- const delta = untilTimestamp - lastEndTimestamp;
542
- const deltaInTimescale = intoTimescale(delta, trackData.timescale);
543
-
544
- if (deltaInTimescale > 0) {
545
- const { sampleSize, silentValue } = parsePcmCodec(
546
- trackData.info.decoderConfig.codec as PcmAudioCodec,
547
- );
548
- const samplesNeeded = deltaInTimescale * trackData.info.numberOfChannels;
549
- const data = new Uint8Array(sampleSize * samplesNeeded).fill(silentValue);
550
-
551
- const paddingSample = this.createSampleForTrack(
552
- trackData,
553
- new Uint8Array(data.buffer),
554
- lastEndTimestamp,
555
- delta,
556
- 'key',
557
- );
558
- await this.registerSample(trackData, paddingSample);
559
- }
560
- }
561
-
562
- async addSubtitleCue(track: OutputSubtitleTrack, cue: SubtitleCue, meta?: SubtitleMetadata) {
563
- const release = await this.mutex.acquire();
564
-
565
- try {
566
- const trackData = this.getSubtitleTrackData(track, meta);
567
-
568
- this.validateAndNormalizeTimestamp(trackData.track, cue.timestamp, true);
569
-
570
- if (track.source._codec === 'webvtt') {
571
- trackData.cueQueue.push(cue);
572
- await this.processWebVTTCues(trackData, cue.timestamp);
573
- } else {
574
- throw new Error(
575
- `${track.source._codec} subtitles are not supported in ${this.format._name}. Only WebVTT is supported.`,
576
- );
577
- }
578
- } finally {
579
- release();
580
- }
581
- }
582
-
583
- private async processWebVTTCues(trackData: IsobmffSubtitleTrackData, until: number) {
584
- // WebVTT cues need to undergo special processing as empty sections need to be padded out with samples, and
585
- // overlapping samples require special logic. The algorithm produces the format specified in ISO 14496-30.
586
-
587
- while (trackData.cueQueue.length > 0) {
588
- const timestamps = new Set<number>([]);
589
- for (const cue of trackData.cueQueue) {
590
- assert(cue.timestamp <= until);
591
- assert(trackData.lastCueEndTimestamp <= cue.timestamp + cue.duration);
592
-
593
- timestamps.add(Math.max(cue.timestamp, trackData.lastCueEndTimestamp)); // Start timestamp
594
- timestamps.add(cue.timestamp + cue.duration); // End timestamp
595
- }
596
-
597
- const sortedTimestamps = [...timestamps].sort((a, b) => a - b);
598
-
599
- // These are the timestamps of the next sample we'll create:
600
- const sampleStart = sortedTimestamps[0]!;
601
- const sampleEnd = sortedTimestamps[1] ?? sampleStart;
602
-
603
- if (until < sampleEnd) {
604
- break;
605
- }
606
-
607
- // We may need to pad out empty space with an vtte box
608
- if (trackData.lastCueEndTimestamp < sampleStart) {
609
- this.auxWriter.seek(0);
610
- const box = vtte();
611
- this.auxBoxWriter.writeBox(box);
612
-
613
- const body = this.auxWriter.getSlice(0, this.auxWriter.getPos());
614
- const sample = this.createSampleForTrack(
615
- trackData,
616
- body,
617
- trackData.lastCueEndTimestamp,
618
- sampleStart - trackData.lastCueEndTimestamp,
619
- 'key',
620
- );
621
-
622
- await this.registerSample(trackData, sample);
623
- trackData.lastCueEndTimestamp = sampleStart;
624
- }
625
-
626
- this.auxWriter.seek(0);
627
-
628
- for (let i = 0; i < trackData.cueQueue.length; i++) {
629
- const cue = trackData.cueQueue[i]!;
630
-
631
- if (cue.timestamp >= sampleEnd) {
632
- break;
633
- }
634
-
635
- inlineTimestampRegex.lastIndex = 0;
636
- const containsTimestamp = inlineTimestampRegex.test(cue.text);
637
-
638
- const endTimestamp = cue.timestamp + cue.duration;
639
- let sourceId = trackData.cueToSourceId.get(cue);
640
- if (sourceId === undefined && sampleEnd < endTimestamp) {
641
- // We know this cue will appear in more than one sample, therefore we need to mark it with a
642
- // unique ID
643
- sourceId = trackData.nextSourceId++;
644
- trackData.cueToSourceId.set(cue, sourceId);
645
- }
646
-
647
- if (cue.notes) {
648
- // Any notes/comments are included in a special vtta box
649
- const box = vtta(cue.notes);
650
- this.auxBoxWriter.writeBox(box);
651
- }
652
-
653
- const box = vttc(
654
- cue.text,
655
- containsTimestamp ? sampleStart : null,
656
- cue.identifier ?? null,
657
- cue.settings ?? null,
658
- sourceId ?? null,
659
- );
660
- this.auxBoxWriter.writeBox(box);
661
-
662
- if (endTimestamp === sampleEnd) {
663
- // The cue won't appear in any future sample, so we're done with it
664
- trackData.cueQueue.splice(i--, 1);
665
- }
666
- }
667
-
668
- const body = this.auxWriter.getSlice(0, this.auxWriter.getPos());
669
- const sample = this.createSampleForTrack(trackData, body, sampleStart, sampleEnd - sampleStart, 'key');
670
-
671
- await this.registerSample(trackData, sample);
672
- trackData.lastCueEndTimestamp = sampleEnd;
673
- }
674
- }
675
-
676
- private createSampleForTrack(
677
- trackData: IsobmffTrackData,
678
- data: Uint8Array,
679
- timestamp: number,
680
- duration: number,
681
- type: PacketType,
682
- ) {
683
- const sample: Sample = {
684
- timestamp,
685
- decodeTimestamp: timestamp, // This may be refined later
686
- duration,
687
- data,
688
- size: data.byteLength,
689
- type,
690
- timescaleUnitsToNextSample: intoTimescale(duration, trackData.timescale), // Will be refined
691
- };
692
-
693
- return sample;
694
- }
695
-
696
- private processTimestamps(trackData: IsobmffTrackData, nextSample?: Sample) {
697
- if (trackData.timestampProcessingQueue.length === 0) {
698
- return;
699
- }
700
-
701
- if (trackData.type === 'audio' && trackData.info.requiresPcmTransformation) {
702
- let totalDuration = 0;
703
-
704
- // Compute the total duration in the track timescale (which is equal to the amount of PCM audio samples)
705
- // and simply say that's how many new samples there are.
706
-
707
- for (let i = 0; i < trackData.timestampProcessingQueue.length; i++) {
708
- const sample = trackData.timestampProcessingQueue[i]!;
709
- const duration = intoTimescale(sample.duration, trackData.timescale);
710
- totalDuration += duration;
711
- }
712
-
713
- if (trackData.timeToSampleTable.length === 0) {
714
- trackData.timeToSampleTable.push({
715
- sampleCount: totalDuration,
716
- sampleDelta: 1,
717
- });
718
- } else {
719
- const lastEntry = last(trackData.timeToSampleTable)!;
720
- lastEntry.sampleCount += totalDuration;
721
- }
722
-
723
- trackData.timestampProcessingQueue.length = 0;
724
- return;
725
- }
726
-
727
- const sortedTimestamps = trackData.timestampProcessingQueue.map(x => x.timestamp).sort((a, b) => a - b);
728
-
729
- for (let i = 0; i < trackData.timestampProcessingQueue.length; i++) {
730
- const sample = trackData.timestampProcessingQueue[i]!;
731
-
732
- // Since the user only supplies presentation time, but these may be out of order, we reverse-engineer from
733
- // that a sensible decode timestamp. The notion of a decode timestamp doesn't really make sense
734
- // (presentation timestamp & decode order are all you need), but it is a concept in ISOBMFF so we need to
735
- // model it.
736
- sample.decodeTimestamp = sortedTimestamps[i]!;
737
-
738
- if (!this.isFragmented && trackData.lastTimescaleUnits === null) {
739
- // In non-fragmented files, the first decode timestamp is always zero. If the first presentation
740
- // timestamp isn't zero, we'll simply use the composition time offset to achieve it.
741
- sample.decodeTimestamp = 0;
742
- }
743
-
744
- const sampleCompositionTimeOffset
745
- = intoTimescale(sample.timestamp - sample.decodeTimestamp, trackData.timescale);
746
- const durationInTimescale = intoTimescale(sample.duration, trackData.timescale);
747
-
748
- if (trackData.lastTimescaleUnits !== null) {
749
- assert(trackData.lastSample);
750
-
751
- const timescaleUnits = intoTimescale(sample.decodeTimestamp, trackData.timescale, false);
752
- const delta = Math.round(timescaleUnits - trackData.lastTimescaleUnits);
753
- assert(delta >= 0);
754
-
755
- trackData.lastTimescaleUnits += delta;
756
- trackData.lastSample.timescaleUnitsToNextSample = delta;
757
-
758
- if (!this.isFragmented) {
759
- let lastTableEntry = last(trackData.timeToSampleTable);
760
- assert(lastTableEntry);
761
-
762
- if (lastTableEntry.sampleCount === 1) {
763
- lastTableEntry.sampleDelta = delta;
764
-
765
- const entryBefore = trackData.timeToSampleTable[trackData.timeToSampleTable.length - 2];
766
- if (entryBefore && entryBefore.sampleDelta === delta) {
767
- // If the delta is the same as the previous one, merge the two entries
768
- entryBefore.sampleCount++;
769
- trackData.timeToSampleTable.pop();
770
- lastTableEntry = entryBefore;
771
- }
772
- } else if (lastTableEntry.sampleDelta !== delta) {
773
- // The delta has changed, so we need a new entry to reach the current sample
774
- lastTableEntry.sampleCount--;
775
- trackData.timeToSampleTable.push(lastTableEntry = {
776
- sampleCount: 1,
777
- sampleDelta: delta,
778
- });
779
- }
780
-
781
- if (lastTableEntry.sampleDelta === durationInTimescale) {
782
- // The sample's duration matches the delta, so we can increment the count
783
- lastTableEntry.sampleCount++;
784
- } else {
785
- // Add a new entry in order to maintain the last sample's true duration
786
- trackData.timeToSampleTable.push({
787
- sampleCount: 1,
788
- sampleDelta: durationInTimescale,
789
- });
790
- }
791
-
792
- const lastCompositionTimeOffsetTableEntry = last(trackData.compositionTimeOffsetTable);
793
- assert(lastCompositionTimeOffsetTableEntry);
794
-
795
- if (
796
- lastCompositionTimeOffsetTableEntry.sampleCompositionTimeOffset === sampleCompositionTimeOffset
797
- ) {
798
- // Simply increment the count
799
- lastCompositionTimeOffsetTableEntry.sampleCount++;
800
- } else {
801
- // The composition time offset has changed, so create a new entry with the new composition time
802
- // offset
803
- trackData.compositionTimeOffsetTable.push({
804
- sampleCount: 1,
805
- sampleCompositionTimeOffset: sampleCompositionTimeOffset,
806
- });
807
- }
808
- }
809
- } else {
810
- // Decode timestamp of the first sample
811
- trackData.lastTimescaleUnits = intoTimescale(sample.decodeTimestamp, trackData.timescale, false);
812
-
813
- if (!this.isFragmented) {
814
- trackData.timeToSampleTable.push({
815
- sampleCount: 1,
816
- sampleDelta: durationInTimescale,
817
- });
818
- trackData.compositionTimeOffsetTable.push({
819
- sampleCount: 1,
820
- sampleCompositionTimeOffset: sampleCompositionTimeOffset,
821
- });
822
- }
823
- }
824
-
825
- trackData.lastSample = sample;
826
- }
827
-
828
- trackData.timestampProcessingQueue.length = 0;
829
-
830
- assert(trackData.lastSample);
831
- assert(trackData.lastTimescaleUnits !== null);
832
-
833
- if (nextSample !== undefined && trackData.lastSample.timescaleUnitsToNextSample === 0) {
834
- assert(nextSample.type === 'key');
835
-
836
- // Given the next sample, we can make a guess about the duration of the last sample. This avoids having
837
- // the last sample's duration in each fragment be "0" for fragmented files. The guess we make here is
838
- // actually correct most of the time, since typically, no delta frame with a lower timestamp follows the key
839
- // frame (although it can happen).
840
- const timescaleUnits = intoTimescale(nextSample.timestamp, trackData.timescale, false);
841
- const delta = Math.round(timescaleUnits - trackData.lastTimescaleUnits);
842
- trackData.lastSample.timescaleUnitsToNextSample = delta;
843
- }
844
- }
845
-
846
- private async registerSample(trackData: IsobmffTrackData, sample: Sample) {
847
- if (sample.type === 'key') {
848
- this.processTimestamps(trackData, sample);
849
- }
850
- trackData.timestampProcessingQueue.push(sample);
851
-
852
- if (this.isFragmented) {
853
- trackData.sampleQueue.push(sample);
854
- await this.interleaveSamples();
855
- } else if (this.fastStart === 'reserve') {
856
- await this.registerSampleFastStartReserve(trackData, sample);
857
- } else {
858
- await this.addSampleToTrack(trackData, sample);
859
- }
860
- }
861
-
862
- private async addSampleToTrack(trackData: IsobmffTrackData, sample: Sample) {
863
- if (!this.isFragmented) {
864
- trackData.samples.push(sample);
865
-
866
- if (this.fastStart === 'reserve') {
867
- const maximumPacketCount = trackData.track.metadata.maximumPacketCount;
868
- assert(maximumPacketCount !== undefined);
869
-
870
- if (trackData.samples.length > maximumPacketCount) {
871
- throw new Error(
872
- `Track #${trackData.track.id} has already reached the maximum packet count`
873
- + ` (${maximumPacketCount}). Either add less packets or increase the maximum packet count.`,
874
- );
875
- }
876
- }
877
- }
878
-
879
- let beginNewChunk = false;
880
- if (!trackData.currentChunk) {
881
- beginNewChunk = true;
882
- } else {
883
- // Timestamp don't need to be monotonic (think B-frames), so we may need to update the start timestamp of
884
- // the chunk
885
- trackData.currentChunk.startTimestamp = Math.min(
886
- trackData.currentChunk.startTimestamp,
887
- sample.timestamp,
888
- );
889
-
890
- const currentChunkDuration = sample.timestamp - trackData.currentChunk.startTimestamp;
891
-
892
- if (this.isFragmented) {
893
- // We can only finalize this fragment (and begin a new one) if we know that each track will be able to
894
- // start the new one with a key frame.
895
- const keyFrameQueuedEverywhere = this.trackDatas.every((otherTrackData) => {
896
- if (trackData === otherTrackData) {
897
- return sample.type === 'key';
898
- }
899
-
900
- const firstQueuedSample = otherTrackData.sampleQueue[0];
901
- if (firstQueuedSample) {
902
- return firstQueuedSample.type === 'key';
903
- }
904
-
905
- return otherTrackData.track.source._closed;
906
- });
907
-
908
- if (
909
- currentChunkDuration >= this.minimumFragmentDuration
910
- && keyFrameQueuedEverywhere
911
- && sample.timestamp > this.maxWrittenTimestamp
912
- ) {
913
- beginNewChunk = true;
914
- await this.finalizeFragment();
915
- }
916
- } else {
917
- beginNewChunk = currentChunkDuration >= 0.5; // Chunk is long enough, we need a new one
918
- }
919
- }
920
-
921
- if (beginNewChunk) {
922
- if (trackData.currentChunk) {
923
- await this.finalizeCurrentChunk(trackData);
924
- }
925
-
926
- trackData.currentChunk = {
927
- startTimestamp: sample.timestamp,
928
- samples: [],
929
- offset: null,
930
- moofOffset: null,
931
- };
932
- }
933
-
934
- assert(trackData.currentChunk);
935
- trackData.currentChunk.samples.push(sample);
936
-
937
- if (this.isFragmented) {
938
- this.maxWrittenTimestamp = Math.max(this.maxWrittenTimestamp, sample.timestamp);
939
- }
940
- }
941
-
942
- private async finalizeCurrentChunk(trackData: IsobmffTrackData) {
943
- assert(!this.isFragmented);
944
-
945
- if (!trackData.currentChunk) return;
946
-
947
- trackData.finalizedChunks.push(trackData.currentChunk);
948
- this.finalizedChunks.push(trackData.currentChunk);
949
-
950
- let sampleCount = trackData.currentChunk.samples.length;
951
- if (trackData.type === 'audio' && trackData.info.requiresPcmTransformation) {
952
- sampleCount = trackData.currentChunk.samples
953
- .reduce((acc, sample) => acc + intoTimescale(sample.duration, trackData.timescale), 0);
954
- }
955
-
956
- if (
957
- trackData.compactlyCodedChunkTable.length === 0
958
- || last(trackData.compactlyCodedChunkTable)!.samplesPerChunk !== sampleCount
959
- ) {
960
- trackData.compactlyCodedChunkTable.push({
961
- firstChunk: trackData.finalizedChunks.length, // 1-indexed
962
- samplesPerChunk: sampleCount,
963
- });
964
- }
965
-
966
- if (this.fastStart === 'in-memory') {
967
- trackData.currentChunk.offset = 0; // We'll compute the proper offset when finalizing
968
- return;
969
- }
970
-
971
- // Write out the data
972
- trackData.currentChunk.offset = this.writer.getPos();
973
- for (const sample of trackData.currentChunk.samples) {
974
- assert(sample.data);
975
- this.writer.write(sample.data);
976
- sample.data = null; // Can be GC'd
977
- }
978
-
979
- await this.writer.flush();
980
- }
981
-
982
- private async interleaveSamples(isFinalCall = false) {
983
- assert(this.isFragmented);
984
-
985
- if (!isFinalCall && !this.allTracksAreKnown()) {
986
- return; // We can't interleave yet as we don't yet know how many tracks we'll truly have
987
- }
988
-
989
- outer:
990
- while (true) {
991
- let trackWithMinTimestamp: IsobmffTrackData | null = null;
992
- let minTimestamp = Infinity;
993
-
994
- for (const trackData of this.trackDatas) {
995
- if (!isFinalCall && trackData.sampleQueue.length === 0 && !trackData.track.source._closed) {
996
- break outer;
997
- }
998
-
999
- if (trackData.sampleQueue.length > 0 && trackData.sampleQueue[0]!.timestamp < minTimestamp) {
1000
- trackWithMinTimestamp = trackData;
1001
- minTimestamp = trackData.sampleQueue[0]!.timestamp;
1002
- }
1003
- }
1004
-
1005
- if (!trackWithMinTimestamp) {
1006
- break;
1007
- }
1008
-
1009
- const sample = trackWithMinTimestamp.sampleQueue.shift()!;
1010
- await this.addSampleToTrack(trackWithMinTimestamp, sample);
1011
- }
1012
- }
1013
-
1014
- private async finalizeFragment(flushWriter = true) {
1015
- assert(this.isFragmented);
1016
-
1017
- const fragmentNumber = this.nextFragmentNumber++;
1018
-
1019
- if (fragmentNumber === 1) {
1020
- if (this.format._options.onMoov) {
1021
- this.writer.startTrackingWrites();
1022
- }
1023
-
1024
- // Write the moov box now that we have all decoder configs
1025
- const movieBox = moov(this);
1026
- this.boxWriter.writeBox(movieBox);
1027
-
1028
- if (this.format._options.onMoov) {
1029
- const { data, start } = this.writer.stopTrackingWrites();
1030
- this.format._options.onMoov(data, start);
1031
- }
1032
- }
1033
-
1034
- // Not all tracks need to be present in every fragment
1035
- const tracksInFragment = this.trackDatas.filter(x => x.currentChunk);
1036
-
1037
- // Create an initial moof box and measure it; we need this to know where the following mdat box will begin
1038
- const moofBox = moof(fragmentNumber, tracksInFragment);
1039
- const moofOffset = this.writer.getPos();
1040
- const mdatStartPos = moofOffset + this.boxWriter.measureBox(moofBox);
1041
-
1042
- let currentPos = mdatStartPos + MIN_BOX_HEADER_SIZE;
1043
- let fragmentStartTimestamp = Infinity;
1044
- for (const trackData of tracksInFragment) {
1045
- trackData.currentChunk!.offset = currentPos;
1046
- trackData.currentChunk!.moofOffset = moofOffset;
1047
-
1048
- for (const sample of trackData.currentChunk!.samples) {
1049
- currentPos += sample.size;
1050
- }
1051
-
1052
- fragmentStartTimestamp = Math.min(fragmentStartTimestamp, trackData.currentChunk!.startTimestamp);
1053
- }
1054
-
1055
- const mdatSize = currentPos - mdatStartPos;
1056
- const needsLargeMdatSize = mdatSize >= 2 ** 32;
1057
-
1058
- if (needsLargeMdatSize) {
1059
- // Shift all offsets by 8. Previously, all chunks were shifted assuming the large box size, but due to what
1060
- // I suspect is a bug in WebKit, it failed in Safari (when livestreaming with MSE, not for static playback).
1061
- for (const trackData of tracksInFragment) {
1062
- trackData.currentChunk!.offset! += MAX_BOX_HEADER_SIZE - MIN_BOX_HEADER_SIZE;
1063
- }
1064
- }
1065
-
1066
- if (this.format._options.onMoof) {
1067
- this.writer.startTrackingWrites();
1068
- }
1069
-
1070
- const newMoofBox = moof(fragmentNumber, tracksInFragment);
1071
- this.boxWriter.writeBox(newMoofBox);
1072
-
1073
- if (this.format._options.onMoof) {
1074
- const { data, start } = this.writer.stopTrackingWrites();
1075
- this.format._options.onMoof(data, start, fragmentStartTimestamp);
1076
- }
1077
-
1078
- assert(this.writer.getPos() === mdatStartPos);
1079
-
1080
- if (this.format._options.onMdat) {
1081
- this.writer.startTrackingWrites();
1082
- }
1083
-
1084
- const mdatBox = mdat(needsLargeMdatSize);
1085
- mdatBox.size = mdatSize;
1086
- this.boxWriter.writeBox(mdatBox);
1087
-
1088
- this.writer.seek(mdatStartPos + (needsLargeMdatSize ? MAX_BOX_HEADER_SIZE : MIN_BOX_HEADER_SIZE));
1089
-
1090
- // Write sample data
1091
- for (const trackData of tracksInFragment) {
1092
- for (const sample of trackData.currentChunk!.samples) {
1093
- this.writer.write(sample.data!);
1094
- sample.data = null; // Can be GC'd
1095
- }
1096
- }
1097
-
1098
- if (this.format._options.onMdat) {
1099
- const { data, start } = this.writer.stopTrackingWrites();
1100
- this.format._options.onMdat(data, start);
1101
- }
1102
-
1103
- for (const trackData of tracksInFragment) {
1104
- trackData.finalizedChunks.push(trackData.currentChunk!);
1105
- this.finalizedChunks.push(trackData.currentChunk!);
1106
- trackData.currentChunk = null;
1107
- }
1108
-
1109
- if (flushWriter) {
1110
- await this.writer.flush();
1111
- }
1112
- }
1113
-
1114
- private async registerSampleFastStartReserve(trackData: IsobmffTrackData, sample: Sample) {
1115
- if (this.allTracksAreKnown()) {
1116
- if (!this.mdat) {
1117
- // We finally know all tracks, let's reserve space for the moov box
1118
- const moovBox = moov(this);
1119
- const moovSize = this.boxWriter.measureBox(moovBox);
1120
-
1121
- const reservedSize = moovSize
1122
- + this.computeSampleTableSizeUpperBound()
1123
- + 4096; // Just a little extra headroom
1124
-
1125
- assert(this.ftypSize !== null);
1126
- this.writer.seek(this.ftypSize + reservedSize);
1127
-
1128
- if (this.format._options.onMdat) {
1129
- this.writer.startTrackingWrites();
1130
- }
1131
-
1132
- this.mdat = mdat(true);
1133
- this.boxWriter.writeBox(this.mdat);
1134
-
1135
- // Now write everything that was queued
1136
- for (const trackData of this.trackDatas) {
1137
- for (const sample of trackData.sampleQueue) {
1138
- await this.addSampleToTrack(trackData, sample);
1139
- }
1140
- trackData.sampleQueue.length = 0;
1141
- }
1142
- }
1143
-
1144
- await this.addSampleToTrack(trackData, sample);
1145
- } else {
1146
- // Queue it for when we know all tracks
1147
- trackData.sampleQueue.push(sample);
1148
- }
1149
- }
1150
-
1151
- private computeSampleTableSizeUpperBound() {
1152
- assert(this.fastStart === 'reserve');
1153
-
1154
- let upperBound = 0;
1155
-
1156
- for (const trackData of this.trackDatas) {
1157
- const n = trackData.track.metadata.maximumPacketCount;
1158
- assert(n !== undefined); // We validated this earlier
1159
-
1160
- // Given the max allowed packet count, compute the space they'll take up in the Sample Table Box, assuming
1161
- // the worst case for each individual box:
1162
-
1163
- // stts box - since it is compactly coded, the maximum length of this table will be 2/3n
1164
- upperBound += (4 + 4) * Math.ceil(2 / 3 * n);
1165
- // stss box - 1 entry per sample
1166
- upperBound += 4 * n;
1167
- // ctts box - since it is compactly coded, the maximum length of this table will be 2/3n
1168
- upperBound += (4 + 4) * Math.ceil(2 / 3 * n);
1169
- // stsc box - since it is compactly coded, the maximum length of this table will be 2/3n
1170
- upperBound += (4 + 4 + 4) * Math.ceil(2 / 3 * n);
1171
- // stsz box - 1 entry per sample
1172
- upperBound += 4 * n;
1173
- // co64 box - we assume 1 sample per chunk and 64-bit chunk offsets (co64 instead of stco)
1174
- upperBound += 8 * n;
1175
- }
1176
-
1177
- return upperBound;
1178
- }
1179
-
1180
- // eslint-disable-next-line @typescript-eslint/no-misused-promises
1181
- override async onTrackClose(track: OutputTrack) {
1182
- const release = await this.mutex.acquire();
1183
-
1184
- if (track.type === 'subtitle' && track.source._codec === 'webvtt') {
1185
- const trackData = this.trackDatas.find(x => x.track === track) as IsobmffSubtitleTrackData;
1186
- if (trackData) {
1187
- await this.processWebVTTCues(trackData, Infinity);
1188
- }
1189
- }
1190
-
1191
- if (this.allTracksAreKnown()) {
1192
- this.allTracksKnown.resolve();
1193
- }
1194
-
1195
- if (this.isFragmented) {
1196
- // Since a track is now closed, we may be able to write out chunks that were previously waiting
1197
- await this.interleaveSamples();
1198
- }
1199
-
1200
- release();
1201
- }
1202
-
1203
- /** Finalizes the file, making it ready for use. Must be called after all video and audio chunks have been added. */
1204
- async finalize() {
1205
- const release = await this.mutex.acquire();
1206
-
1207
- this.allTracksKnown.resolve();
1208
-
1209
- for (const trackData of this.trackDatas) {
1210
- if (trackData.type === 'subtitle' && trackData.track.source._codec === 'webvtt') {
1211
- await this.processWebVTTCues(trackData, Infinity);
1212
- }
1213
- }
1214
-
1215
- if (this.isFragmented) {
1216
- await this.interleaveSamples(true);
1217
-
1218
- for (const trackData of this.trackDatas) {
1219
- this.processTimestamps(trackData);
1220
- }
1221
-
1222
- await this.finalizeFragment(false); // Don't flush the last fragment as we will flush it with the mfra box
1223
- } else {
1224
- for (const trackData of this.trackDatas) {
1225
- this.processTimestamps(trackData);
1226
- await this.finalizeCurrentChunk(trackData);
1227
- }
1228
- }
1229
-
1230
- if (this.fastStart === 'in-memory') {
1231
- this.mdat = mdat(false);
1232
- let mdatSize: number;
1233
-
1234
- // We know how many chunks there are, but computing the chunk positions requires an iterative approach:
1235
- // In order to know where the first chunk should go, we first need to know the size of the moov box. But we
1236
- // cannot write a proper moov box without first knowing all chunk positions. So, we generate a tentative
1237
- // moov box with placeholder values (0) for the chunk offsets to be able to compute its size. If it then
1238
- // turns out that appending all chunks exceeds 4 GiB, we need to repeat this process, now with the co64 box
1239
- // being used in the moov box instead, which will make it larger. After that, we definitely know the final
1240
- // size of the moov box and can compute the proper chunk positions.
1241
-
1242
- for (let i = 0; i < 2; i++) {
1243
- const movieBox = moov(this);
1244
- const movieBoxSize = this.boxWriter.measureBox(movieBox);
1245
- mdatSize = this.boxWriter.measureBox(this.mdat);
1246
- let currentChunkPos = this.writer.getPos() + movieBoxSize + mdatSize;
1247
-
1248
- for (const chunk of this.finalizedChunks) {
1249
- chunk.offset = currentChunkPos;
1250
- for (const { data } of chunk.samples) {
1251
- assert(data);
1252
- currentChunkPos += data.byteLength;
1253
- mdatSize += data.byteLength;
1254
- }
1255
- }
1256
-
1257
- if (currentChunkPos < 2 ** 32) break;
1258
- if (mdatSize >= 2 ** 32) this.mdat.largeSize = true;
1259
- }
1260
-
1261
- if (this.format._options.onMoov) {
1262
- this.writer.startTrackingWrites();
1263
- }
1264
-
1265
- const movieBox = moov(this);
1266
- this.boxWriter.writeBox(movieBox);
1267
-
1268
- if (this.format._options.onMoov) {
1269
- const { data, start } = this.writer.stopTrackingWrites();
1270
- this.format._options.onMoov(data, start);
1271
- }
1272
-
1273
- if (this.format._options.onMdat) {
1274
- this.writer.startTrackingWrites();
1275
- }
1276
-
1277
- this.mdat.size = mdatSize!;
1278
- this.boxWriter.writeBox(this.mdat);
1279
-
1280
- for (const chunk of this.finalizedChunks) {
1281
- for (const sample of chunk.samples) {
1282
- assert(sample.data);
1283
- this.writer.write(sample.data);
1284
- sample.data = null;
1285
- }
1286
- }
1287
-
1288
- if (this.format._options.onMdat) {
1289
- const { data, start } = this.writer.stopTrackingWrites();
1290
- this.format._options.onMdat(data, start);
1291
- }
1292
- } else if (this.isFragmented) {
1293
- // Append the mfra box to the end of the file for better random access
1294
- const startPos = this.writer.getPos();
1295
- const mfraBox = mfra(this.trackDatas);
1296
- this.boxWriter.writeBox(mfraBox);
1297
-
1298
- // Patch the 'size' field of the mfro box at the end of the mfra box now that we know its actual size
1299
- const mfraBoxSize = this.writer.getPos() - startPos;
1300
- this.writer.seek(this.writer.getPos() - 4);
1301
- this.boxWriter.writeU32(mfraBoxSize);
1302
- } else {
1303
- assert(this.mdat);
1304
-
1305
- const mdatPos = this.boxWriter.offsets.get(this.mdat);
1306
- assert(mdatPos !== undefined);
1307
- const mdatSize = this.writer.getPos() - mdatPos;
1308
- this.mdat.size = mdatSize;
1309
- this.mdat.largeSize = mdatSize >= 2 ** 32; // Only use the large size if we need it
1310
- this.boxWriter.patchBox(this.mdat);
1311
-
1312
- if (this.format._options.onMdat) {
1313
- const { data, start } = this.writer.stopTrackingWrites();
1314
- this.format._options.onMdat(data, start);
1315
- }
1316
-
1317
- const movieBox = moov(this);
1318
-
1319
- if (this.fastStart === 'reserve') {
1320
- assert(this.ftypSize !== null);
1321
- this.writer.seek(this.ftypSize);
1322
-
1323
- if (this.format._options.onMoov) {
1324
- this.writer.startTrackingWrites();
1325
- }
1326
-
1327
- this.boxWriter.writeBox(movieBox);
1328
-
1329
- // Fill the remaining space with a free box. If there are less than 8 bytes left, sucks I guess
1330
- const remainingSpace = this.boxWriter.offsets.get(this.mdat)! - this.writer.getPos();
1331
- this.boxWriter.writeBox(free(remainingSpace));
1332
- } else {
1333
- if (this.format._options.onMoov) {
1334
- this.writer.startTrackingWrites();
1335
- }
1336
-
1337
- this.boxWriter.writeBox(movieBox);
1338
- }
1339
-
1340
- if (this.format._options.onMoov) {
1341
- const { data, start } = this.writer.stopTrackingWrites();
1342
- this.format._options.onMoov(data, start);
1343
- }
1344
- }
1345
-
1346
- release();
1347
- }
1348
- }