@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.
- package/README.md +1 -1
- package/dist/bundles/{mediabunny.mjs → mediabunny.js} +21963 -21388
- package/dist/bundles/mediabunny.min.js +490 -0
- package/dist/modules/shared/mp3-misc.d.ts.map +1 -1
- package/dist/modules/src/adts/adts-demuxer.d.ts +6 -6
- package/dist/modules/src/adts/adts-demuxer.d.ts.map +1 -1
- package/dist/modules/src/adts/adts-muxer.d.ts +4 -4
- package/dist/modules/src/adts/adts-muxer.d.ts.map +1 -1
- package/dist/modules/src/adts/adts-reader.d.ts +1 -1
- package/dist/modules/src/adts/adts-reader.d.ts.map +1 -1
- package/dist/modules/src/avi/avi-demuxer.d.ts +44 -0
- package/dist/modules/src/avi/avi-demuxer.d.ts.map +1 -0
- package/dist/modules/src/avi/avi-misc.d.ts +88 -0
- package/dist/modules/src/avi/avi-misc.d.ts.map +1 -0
- package/dist/modules/src/avi/avi-muxer.d.ts +45 -0
- package/dist/modules/src/avi/avi-muxer.d.ts.map +1 -0
- package/dist/modules/src/avi/riff-writer.d.ts +26 -0
- package/dist/modules/src/avi/riff-writer.d.ts.map +1 -0
- package/dist/modules/src/codec-data.d.ts +8 -3
- package/dist/modules/src/codec-data.d.ts.map +1 -1
- package/dist/modules/src/codec.d.ts +10 -10
- package/dist/modules/src/codec.d.ts.map +1 -1
- package/dist/modules/src/conversion.d.ts +33 -16
- package/dist/modules/src/conversion.d.ts.map +1 -1
- package/dist/modules/src/custom-coder.d.ts +8 -8
- package/dist/modules/src/custom-coder.d.ts.map +1 -1
- package/dist/modules/src/demuxer.d.ts +3 -3
- package/dist/modules/src/demuxer.d.ts.map +1 -1
- package/dist/modules/src/encode.d.ts +8 -8
- package/dist/modules/src/encode.d.ts.map +1 -1
- package/dist/modules/src/flac/flac-demuxer.d.ts +7 -7
- package/dist/modules/src/flac/flac-demuxer.d.ts.map +1 -1
- package/dist/modules/src/flac/flac-misc.d.ts +3 -3
- package/dist/modules/src/flac/flac-misc.d.ts.map +1 -1
- package/dist/modules/src/flac/flac-muxer.d.ts +5 -5
- package/dist/modules/src/flac/flac-muxer.d.ts.map +1 -1
- package/dist/modules/src/id3.d.ts +3 -3
- package/dist/modules/src/id3.d.ts.map +1 -1
- package/dist/modules/src/index.d.ts +20 -20
- package/dist/modules/src/index.d.ts.map +1 -1
- package/dist/modules/src/input-format.d.ts +22 -0
- package/dist/modules/src/input-format.d.ts.map +1 -1
- package/dist/modules/src/input-track.d.ts +8 -8
- package/dist/modules/src/input-track.d.ts.map +1 -1
- package/dist/modules/src/input.d.ts +12 -12
- package/dist/modules/src/isobmff/isobmff-boxes.d.ts +2 -2
- package/dist/modules/src/isobmff/isobmff-boxes.d.ts.map +1 -1
- package/dist/modules/src/isobmff/isobmff-demuxer.d.ts +12 -12
- package/dist/modules/src/isobmff/isobmff-demuxer.d.ts.map +1 -1
- package/dist/modules/src/isobmff/isobmff-misc.d.ts.map +1 -1
- package/dist/modules/src/isobmff/isobmff-muxer.d.ts +11 -11
- package/dist/modules/src/isobmff/isobmff-muxer.d.ts.map +1 -1
- package/dist/modules/src/isobmff/isobmff-reader.d.ts +2 -2
- package/dist/modules/src/isobmff/isobmff-reader.d.ts.map +1 -1
- package/dist/modules/src/matroska/ebml.d.ts +3 -3
- package/dist/modules/src/matroska/ebml.d.ts.map +1 -1
- package/dist/modules/src/matroska/matroska-demuxer.d.ts +13 -13
- package/dist/modules/src/matroska/matroska-demuxer.d.ts.map +1 -1
- package/dist/modules/src/matroska/matroska-input.d.ts +33 -0
- package/dist/modules/src/matroska/matroska-input.d.ts.map +1 -0
- package/dist/modules/src/matroska/matroska-misc.d.ts.map +1 -1
- package/dist/modules/src/matroska/matroska-muxer.d.ts +5 -5
- package/dist/modules/src/matroska/matroska-muxer.d.ts.map +1 -1
- package/dist/modules/src/media-sink.d.ts +5 -5
- package/dist/modules/src/media-sink.d.ts.map +1 -1
- package/dist/modules/src/media-source.d.ts +22 -4
- package/dist/modules/src/media-source.d.ts.map +1 -1
- package/dist/modules/src/metadata.d.ts +2 -2
- package/dist/modules/src/metadata.d.ts.map +1 -1
- package/dist/modules/src/misc.d.ts +5 -4
- package/dist/modules/src/misc.d.ts.map +1 -1
- package/dist/modules/src/mp3/mp3-demuxer.d.ts +7 -7
- package/dist/modules/src/mp3/mp3-demuxer.d.ts.map +1 -1
- package/dist/modules/src/mp3/mp3-muxer.d.ts +4 -4
- package/dist/modules/src/mp3/mp3-muxer.d.ts.map +1 -1
- package/dist/modules/src/mp3/mp3-reader.d.ts +2 -2
- package/dist/modules/src/mp3/mp3-reader.d.ts.map +1 -1
- package/dist/modules/src/mp3/mp3-writer.d.ts +1 -1
- package/dist/modules/src/mp3/mp3-writer.d.ts.map +1 -1
- package/dist/modules/src/muxer.d.ts +4 -4
- package/dist/modules/src/muxer.d.ts.map +1 -1
- package/dist/modules/src/node.d.ts +1 -1
- package/dist/modules/src/ogg/ogg-demuxer.d.ts +7 -7
- package/dist/modules/src/ogg/ogg-demuxer.d.ts.map +1 -1
- package/dist/modules/src/ogg/ogg-misc.d.ts +1 -1
- package/dist/modules/src/ogg/ogg-misc.d.ts.map +1 -1
- package/dist/modules/src/ogg/ogg-muxer.d.ts +5 -5
- package/dist/modules/src/ogg/ogg-muxer.d.ts.map +1 -1
- package/dist/modules/src/ogg/ogg-reader.d.ts +1 -1
- package/dist/modules/src/ogg/ogg-reader.d.ts.map +1 -1
- package/dist/modules/src/output-format.d.ts +51 -6
- package/dist/modules/src/output-format.d.ts.map +1 -1
- package/dist/modules/src/output.d.ts +13 -13
- package/dist/modules/src/output.d.ts.map +1 -1
- package/dist/modules/src/packet.d.ts +1 -1
- package/dist/modules/src/packet.d.ts.map +1 -1
- package/dist/modules/src/pcm.d.ts.map +1 -1
- package/dist/modules/src/reader.d.ts +2 -2
- package/dist/modules/src/reader.d.ts.map +1 -1
- package/dist/modules/src/sample.d.ts +57 -15
- package/dist/modules/src/sample.d.ts.map +1 -1
- package/dist/modules/src/source.d.ts +3 -3
- package/dist/modules/src/source.d.ts.map +1 -1
- package/dist/modules/src/subtitles.d.ts +1 -1
- package/dist/modules/src/subtitles.d.ts.map +1 -1
- package/dist/modules/src/target.d.ts +2 -2
- package/dist/modules/src/target.d.ts.map +1 -1
- package/dist/modules/src/tsconfig.tsbuildinfo +1 -1
- package/dist/modules/src/wave/riff-writer.d.ts +1 -1
- package/dist/modules/src/wave/riff-writer.d.ts.map +1 -1
- package/dist/modules/src/wave/wave-demuxer.d.ts +6 -6
- package/dist/modules/src/wave/wave-demuxer.d.ts.map +1 -1
- package/dist/modules/src/wave/wave-muxer.d.ts +4 -4
- package/dist/modules/src/wave/wave-muxer.d.ts.map +1 -1
- package/dist/modules/src/writer.d.ts +1 -1
- package/dist/modules/src/writer.d.ts.map +1 -1
- package/dist/packages/eac3/eac3.wasm +0 -0
- package/dist/packages/eac3/mediabunny-eac3.js +1058 -0
- package/dist/packages/eac3/mediabunny-eac3.min.js +44 -0
- package/dist/packages/mp3-encoder/mediabunny-mp3-encoder.js +694 -0
- package/dist/packages/mp3-encoder/mediabunny-mp3-encoder.min.js +58 -0
- package/dist/packages/mpeg4/mediabunny-mpeg4.js +1198 -0
- package/dist/packages/mpeg4/mediabunny-mpeg4.min.js +44 -0
- package/dist/packages/mpeg4/xvid.wasm +0 -0
- package/package.json +18 -57
- package/dist/bundles/mediabunny.cjs +0 -26140
- package/dist/bundles/mediabunny.min.cjs +0 -147
- package/dist/bundles/mediabunny.min.mjs +0 -146
- package/dist/mediabunny.d.ts +0 -3319
- package/dist/modules/shared/mp3-misc.js +0 -147
- package/dist/modules/src/adts/adts-demuxer.js +0 -239
- package/dist/modules/src/adts/adts-muxer.js +0 -80
- package/dist/modules/src/adts/adts-reader.js +0 -63
- package/dist/modules/src/codec-data.js +0 -1730
- package/dist/modules/src/codec.js +0 -869
- package/dist/modules/src/conversion.js +0 -1459
- package/dist/modules/src/custom-coder.js +0 -117
- package/dist/modules/src/demuxer.js +0 -12
- package/dist/modules/src/encode.js +0 -442
- package/dist/modules/src/flac/flac-demuxer.js +0 -504
- package/dist/modules/src/flac/flac-misc.js +0 -135
- package/dist/modules/src/flac/flac-muxer.js +0 -222
- package/dist/modules/src/id3.js +0 -848
- package/dist/modules/src/index.js +0 -28
- package/dist/modules/src/input-format.js +0 -480
- package/dist/modules/src/input-track.js +0 -372
- package/dist/modules/src/input.js +0 -188
- package/dist/modules/src/isobmff/isobmff-boxes.js +0 -1480
- package/dist/modules/src/isobmff/isobmff-demuxer.js +0 -2618
- package/dist/modules/src/isobmff/isobmff-misc.js +0 -20
- package/dist/modules/src/isobmff/isobmff-muxer.js +0 -966
- package/dist/modules/src/isobmff/isobmff-reader.js +0 -72
- package/dist/modules/src/matroska/ebml.js +0 -653
- package/dist/modules/src/matroska/matroska-demuxer.js +0 -2133
- package/dist/modules/src/matroska/matroska-misc.js +0 -20
- package/dist/modules/src/matroska/matroska-muxer.js +0 -1017
- package/dist/modules/src/media-sink.js +0 -1736
- package/dist/modules/src/media-source.js +0 -1825
- package/dist/modules/src/metadata.js +0 -193
- package/dist/modules/src/misc.js +0 -623
- package/dist/modules/src/mp3/mp3-demuxer.js +0 -285
- package/dist/modules/src/mp3/mp3-muxer.js +0 -123
- package/dist/modules/src/mp3/mp3-reader.js +0 -26
- package/dist/modules/src/mp3/mp3-writer.js +0 -78
- package/dist/modules/src/muxer.js +0 -50
- package/dist/modules/src/node.js +0 -9
- package/dist/modules/src/ogg/ogg-demuxer.js +0 -763
- package/dist/modules/src/ogg/ogg-misc.js +0 -78
- package/dist/modules/src/ogg/ogg-muxer.js +0 -353
- package/dist/modules/src/ogg/ogg-reader.js +0 -65
- package/dist/modules/src/output-format.js +0 -527
- package/dist/modules/src/output.js +0 -300
- package/dist/modules/src/packet.js +0 -182
- package/dist/modules/src/pcm.js +0 -85
- package/dist/modules/src/reader.js +0 -236
- package/dist/modules/src/sample.js +0 -1056
- package/dist/modules/src/source.js +0 -1182
- package/dist/modules/src/subtitles.js +0 -575
- package/dist/modules/src/target.js +0 -140
- package/dist/modules/src/wave/riff-writer.js +0 -30
- package/dist/modules/src/wave/wave-demuxer.js +0 -447
- package/dist/modules/src/wave/wave-muxer.js +0 -318
- package/dist/modules/src/writer.js +0 -370
- package/src/adts/adts-demuxer.ts +0 -331
- package/src/adts/adts-muxer.ts +0 -111
- package/src/adts/adts-reader.ts +0 -85
- package/src/codec-data.ts +0 -2078
- package/src/codec.ts +0 -1092
- package/src/conversion.ts +0 -2112
- package/src/custom-coder.ts +0 -197
- package/src/demuxer.ts +0 -24
- package/src/encode.ts +0 -739
- package/src/flac/flac-demuxer.ts +0 -730
- package/src/flac/flac-misc.ts +0 -164
- package/src/flac/flac-muxer.ts +0 -320
- package/src/id3.ts +0 -925
- package/src/index.ts +0 -221
- package/src/input-format.ts +0 -541
- package/src/input-track.ts +0 -529
- package/src/input.ts +0 -235
- package/src/isobmff/isobmff-boxes.ts +0 -1719
- package/src/isobmff/isobmff-demuxer.ts +0 -3190
- package/src/isobmff/isobmff-misc.ts +0 -29
- package/src/isobmff/isobmff-muxer.ts +0 -1348
- package/src/isobmff/isobmff-reader.ts +0 -91
- package/src/matroska/ebml.ts +0 -730
- package/src/matroska/matroska-demuxer.ts +0 -2481
- package/src/matroska/matroska-misc.ts +0 -29
- package/src/matroska/matroska-muxer.ts +0 -1276
- package/src/media-sink.ts +0 -2179
- package/src/media-source.ts +0 -2243
- package/src/metadata.ts +0 -320
- package/src/misc.ts +0 -798
- package/src/mp3/mp3-demuxer.ts +0 -383
- package/src/mp3/mp3-muxer.ts +0 -166
- package/src/mp3/mp3-reader.ts +0 -34
- package/src/mp3/mp3-writer.ts +0 -120
- package/src/muxer.ts +0 -88
- package/src/node.ts +0 -11
- package/src/ogg/ogg-demuxer.ts +0 -1053
- package/src/ogg/ogg-misc.ts +0 -116
- package/src/ogg/ogg-muxer.ts +0 -497
- package/src/ogg/ogg-reader.ts +0 -93
- package/src/output-format.ts +0 -945
- package/src/output.ts +0 -488
- package/src/packet.ts +0 -263
- package/src/pcm.ts +0 -112
- package/src/reader.ts +0 -323
- package/src/sample.ts +0 -1461
- package/src/source.ts +0 -1688
- package/src/subtitles.ts +0 -711
- package/src/target.ts +0 -204
- package/src/tsconfig.json +0 -16
- package/src/wave/riff-writer.ts +0 -36
- package/src/wave/wave-demuxer.ts +0 -529
- package/src/wave/wave-muxer.ts +0 -371
- package/src/writer.ts +0 -490
|
@@ -1,222 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Copyright (c) 2025-present, Vanilagy and contributors
|
|
3
|
-
*
|
|
4
|
-
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
5
|
-
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
6
|
-
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
7
|
-
*/
|
|
8
|
-
import { validateAudioChunkMetadata } from '../codec.js';
|
|
9
|
-
import { createVorbisComments, FlacBlockType } from '../codec-data.js';
|
|
10
|
-
import { assert, Bitstream, textEncoder, toDataView, toUint8Array, } from '../misc.js';
|
|
11
|
-
import { Muxer } from '../muxer.js';
|
|
12
|
-
import { FileSlice, readBytes } from '../reader.js';
|
|
13
|
-
import { metadataTagsAreEmpty } from '../metadata.js';
|
|
14
|
-
import { readBlockSize, getBlockSizeOrUncommon, readCodedNumber, } from './flac-misc.js';
|
|
15
|
-
const FLAC_HEADER = /* #__PURE__ */ new Uint8Array([0x66, 0x4c, 0x61, 0x43]); // 'fLaC'
|
|
16
|
-
const STREAMINFO_SIZE = 38;
|
|
17
|
-
const STREAMINFO_BLOCK_SIZE = 34;
|
|
18
|
-
export class FlacMuxer extends Muxer {
|
|
19
|
-
constructor(output, format) {
|
|
20
|
-
super(output);
|
|
21
|
-
this.metadataWritten = false;
|
|
22
|
-
this.blockSizes = [];
|
|
23
|
-
this.frameSizes = [];
|
|
24
|
-
this.sampleRate = null;
|
|
25
|
-
this.channels = null;
|
|
26
|
-
this.bitsPerSample = null;
|
|
27
|
-
this.writer = output._writer;
|
|
28
|
-
this.format = format;
|
|
29
|
-
}
|
|
30
|
-
async start() {
|
|
31
|
-
this.writer.write(FLAC_HEADER);
|
|
32
|
-
}
|
|
33
|
-
writeHeader({ bitsPerSample, minimumBlockSize, maximumBlockSize, minimumFrameSize, maximumFrameSize, sampleRate, channels, totalSamples, }) {
|
|
34
|
-
assert(this.writer.getPos() === 4);
|
|
35
|
-
const hasMetadata = !metadataTagsAreEmpty(this.output._metadataTags);
|
|
36
|
-
const headerBitstream = new Bitstream(new Uint8Array(4));
|
|
37
|
-
headerBitstream.writeBits(1, Number(!hasMetadata)); // isLastMetadata
|
|
38
|
-
headerBitstream.writeBits(7, FlacBlockType.STREAMINFO); // metaBlockType = streaminfo
|
|
39
|
-
headerBitstream.writeBits(24, STREAMINFO_BLOCK_SIZE); // size
|
|
40
|
-
this.writer.write(headerBitstream.bytes);
|
|
41
|
-
const contentBitstream = new Bitstream(new Uint8Array(18));
|
|
42
|
-
contentBitstream.writeBits(16, minimumBlockSize);
|
|
43
|
-
contentBitstream.writeBits(16, maximumBlockSize);
|
|
44
|
-
contentBitstream.writeBits(24, minimumFrameSize);
|
|
45
|
-
contentBitstream.writeBits(24, maximumFrameSize);
|
|
46
|
-
contentBitstream.writeBits(20, sampleRate);
|
|
47
|
-
contentBitstream.writeBits(3, channels - 1);
|
|
48
|
-
contentBitstream.writeBits(5, bitsPerSample - 1);
|
|
49
|
-
// Bitstream operations are only safe until 32bit, breaks when using 36 bits
|
|
50
|
-
// Splitting up into writing 4 0 bits and then 32 bits is safe
|
|
51
|
-
// This is safe for audio up to (2 ** 32 / 44100 / 3600) -> 27 hours
|
|
52
|
-
// Not implementing support for more than 32 bits now
|
|
53
|
-
if (totalSamples >= 2 ** 32) {
|
|
54
|
-
throw new Error('This muxer only supports writing up to 2 ** 32 samples');
|
|
55
|
-
}
|
|
56
|
-
contentBitstream.writeBits(4, 0);
|
|
57
|
-
contentBitstream.writeBits(32, totalSamples);
|
|
58
|
-
this.writer.write(contentBitstream.bytes);
|
|
59
|
-
// The MD5 hash is calculated from decoded audio data, but we do not have access
|
|
60
|
-
// to it here. We are allowed to set 0:
|
|
61
|
-
// "A value of 0 signifies that the value is not known."
|
|
62
|
-
// https://www.rfc-editor.org/rfc/rfc9639.html#name-streaminfo
|
|
63
|
-
this.writer.write(new Uint8Array(16));
|
|
64
|
-
}
|
|
65
|
-
writePictureBlock(picture) {
|
|
66
|
-
// Header size:
|
|
67
|
-
// 4 bytes: picture type
|
|
68
|
-
// 4 bytes: media type length
|
|
69
|
-
// x bytes: media type
|
|
70
|
-
// 4 bytes: description length
|
|
71
|
-
// y bytes: description
|
|
72
|
-
// 1 bytes: width
|
|
73
|
-
// 1 bytes: height
|
|
74
|
-
// 1 bytes: color depth
|
|
75
|
-
// 1 bytes: number of indexed colors
|
|
76
|
-
// 4 bytes: picture data length
|
|
77
|
-
// z bytes: picture data
|
|
78
|
-
// Total: 20 + x + y + z
|
|
79
|
-
const headerSize = 32
|
|
80
|
-
+ picture.mimeType.length
|
|
81
|
-
+ (picture.description?.length ?? 0)
|
|
82
|
-
+ picture.data.length;
|
|
83
|
-
const header = new Uint8Array(headerSize);
|
|
84
|
-
let offset = 0;
|
|
85
|
-
const dataView = toDataView(header);
|
|
86
|
-
dataView.setUint32(offset, picture.kind === 'coverFront' ? 3 : picture.kind === 'coverBack' ? 4 : 0);
|
|
87
|
-
offset += 4;
|
|
88
|
-
dataView.setUint32(offset, picture.mimeType.length);
|
|
89
|
-
offset += 4;
|
|
90
|
-
header.set(textEncoder.encode(picture.mimeType), 8);
|
|
91
|
-
offset += picture.mimeType.length;
|
|
92
|
-
dataView.setUint32(offset, picture.description?.length ?? 0);
|
|
93
|
-
offset += 4;
|
|
94
|
-
header.set(textEncoder.encode(picture.description ?? ''), offset);
|
|
95
|
-
offset += picture.description?.length ?? 0;
|
|
96
|
-
offset += 4 + 4 + 4 + 4; // setting width, height, color depth, number of indexed colors to 0
|
|
97
|
-
dataView.setUint32(offset, picture.data.length);
|
|
98
|
-
offset += 4;
|
|
99
|
-
header.set(picture.data, offset);
|
|
100
|
-
offset += picture.data.length;
|
|
101
|
-
assert(offset === headerSize);
|
|
102
|
-
const headerBitstream = new Bitstream(new Uint8Array(4));
|
|
103
|
-
headerBitstream.writeBits(1, 0); // Last metadata block -> false, will be continued by vorbis comment
|
|
104
|
-
headerBitstream.writeBits(7, FlacBlockType.PICTURE); // Type -> Picture
|
|
105
|
-
headerBitstream.writeBits(24, headerSize);
|
|
106
|
-
this.writer.write(headerBitstream.bytes);
|
|
107
|
-
this.writer.write(header);
|
|
108
|
-
}
|
|
109
|
-
writeVorbisCommentAndPictureBlock() {
|
|
110
|
-
this.writer.seek(STREAMINFO_SIZE + FLAC_HEADER.byteLength);
|
|
111
|
-
if (metadataTagsAreEmpty(this.output._metadataTags)) {
|
|
112
|
-
this.metadataWritten = true;
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
const pictures = this.output._metadataTags.images ?? [];
|
|
116
|
-
for (const picture of pictures) {
|
|
117
|
-
this.writePictureBlock(picture);
|
|
118
|
-
}
|
|
119
|
-
const vorbisComment = createVorbisComments(new Uint8Array(0), this.output._metadataTags, false);
|
|
120
|
-
const headerBitstream = new Bitstream(new Uint8Array(4));
|
|
121
|
-
headerBitstream.writeBits(1, 1); // Last metadata block -> true
|
|
122
|
-
headerBitstream.writeBits(7, FlacBlockType.VORBIS_COMMENT); // Type -> Vorbis comment
|
|
123
|
-
headerBitstream.writeBits(24, vorbisComment.length);
|
|
124
|
-
this.writer.write(headerBitstream.bytes);
|
|
125
|
-
this.writer.write(vorbisComment);
|
|
126
|
-
this.metadataWritten = true;
|
|
127
|
-
}
|
|
128
|
-
async getMimeType() {
|
|
129
|
-
return 'audio/flac';
|
|
130
|
-
}
|
|
131
|
-
async addEncodedVideoPacket() {
|
|
132
|
-
throw new Error('FLAC does not support video.');
|
|
133
|
-
}
|
|
134
|
-
async addEncodedAudioPacket(track, packet, meta) {
|
|
135
|
-
const release = await this.mutex.acquire();
|
|
136
|
-
validateAudioChunkMetadata(meta);
|
|
137
|
-
assert(meta);
|
|
138
|
-
assert(meta.decoderConfig);
|
|
139
|
-
assert(meta.decoderConfig.description);
|
|
140
|
-
try {
|
|
141
|
-
this.validateAndNormalizeTimestamp(track, packet.timestamp, packet.type === 'key');
|
|
142
|
-
if (this.sampleRate === null) {
|
|
143
|
-
this.sampleRate = meta.decoderConfig.sampleRate;
|
|
144
|
-
}
|
|
145
|
-
if (this.channels === null) {
|
|
146
|
-
this.channels = meta.decoderConfig.numberOfChannels;
|
|
147
|
-
}
|
|
148
|
-
if (this.bitsPerSample === null) {
|
|
149
|
-
const descriptionBitstream = new Bitstream(toUint8Array(meta.decoderConfig.description));
|
|
150
|
-
// skip 'fLaC' + block size + frame size + sample rate + number of channels
|
|
151
|
-
// See demuxer for the exact structure
|
|
152
|
-
descriptionBitstream.skipBits(103 + 64);
|
|
153
|
-
const bitsPerSample = descriptionBitstream.readBits(5) + 1;
|
|
154
|
-
this.bitsPerSample = bitsPerSample;
|
|
155
|
-
}
|
|
156
|
-
if (!this.metadataWritten) {
|
|
157
|
-
this.writeVorbisCommentAndPictureBlock();
|
|
158
|
-
}
|
|
159
|
-
const slice = FileSlice.tempFromBytes(packet.data);
|
|
160
|
-
readBytes(slice, 2);
|
|
161
|
-
const bytes = readBytes(slice, 2);
|
|
162
|
-
const bitstream = new Bitstream(bytes);
|
|
163
|
-
const blockSizeOrUncommon = getBlockSizeOrUncommon(bitstream.readBits(4));
|
|
164
|
-
if (blockSizeOrUncommon === null) {
|
|
165
|
-
throw new Error('Invalid FLAC frame: Invalid block size.');
|
|
166
|
-
}
|
|
167
|
-
readCodedNumber(slice); // num
|
|
168
|
-
const blockSize = readBlockSize(slice, blockSizeOrUncommon);
|
|
169
|
-
this.blockSizes.push(blockSize);
|
|
170
|
-
this.frameSizes.push(packet.data.length);
|
|
171
|
-
const startPos = this.writer.getPos();
|
|
172
|
-
this.writer.write(packet.data);
|
|
173
|
-
if (this.format._options.onFrame) {
|
|
174
|
-
this.format._options.onFrame(packet.data, startPos);
|
|
175
|
-
}
|
|
176
|
-
await this.writer.flush();
|
|
177
|
-
}
|
|
178
|
-
finally {
|
|
179
|
-
release();
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
addSubtitleCue() {
|
|
183
|
-
throw new Error('FLAC does not support subtitles.');
|
|
184
|
-
}
|
|
185
|
-
async finalize() {
|
|
186
|
-
const release = await this.mutex.acquire();
|
|
187
|
-
let minimumBlockSize = Infinity;
|
|
188
|
-
let maximumBlockSize = 0;
|
|
189
|
-
let minimumFrameSize = Infinity;
|
|
190
|
-
let maximumFrameSize = 0;
|
|
191
|
-
let totalSamples = 0;
|
|
192
|
-
for (let i = 0; i < this.blockSizes.length; i++) {
|
|
193
|
-
minimumFrameSize = Math.min(minimumFrameSize, this.frameSizes[i]);
|
|
194
|
-
maximumFrameSize = Math.max(maximumFrameSize, this.frameSizes[i]);
|
|
195
|
-
maximumBlockSize = Math.max(maximumBlockSize, this.blockSizes[i]);
|
|
196
|
-
totalSamples += this.blockSizes[i];
|
|
197
|
-
// Excluding the last frame from block size calculation
|
|
198
|
-
// https://www.rfc-editor.org/rfc/rfc9639.html#name-streaminfo
|
|
199
|
-
// "The minimum block size (in samples) used in the stream, excluding the last block."
|
|
200
|
-
const isLastFrame = i === this.blockSizes.length - 1;
|
|
201
|
-
if (isLastFrame) {
|
|
202
|
-
continue;
|
|
203
|
-
}
|
|
204
|
-
minimumBlockSize = Math.min(minimumBlockSize, this.blockSizes[i]);
|
|
205
|
-
}
|
|
206
|
-
assert(this.sampleRate !== null);
|
|
207
|
-
assert(this.channels !== null);
|
|
208
|
-
assert(this.bitsPerSample !== null);
|
|
209
|
-
this.writer.seek(4);
|
|
210
|
-
this.writeHeader({
|
|
211
|
-
minimumBlockSize,
|
|
212
|
-
maximumBlockSize,
|
|
213
|
-
minimumFrameSize,
|
|
214
|
-
maximumFrameSize,
|
|
215
|
-
sampleRate: this.sampleRate,
|
|
216
|
-
channels: this.channels,
|
|
217
|
-
bitsPerSample: this.bitsPerSample,
|
|
218
|
-
totalSamples,
|
|
219
|
-
});
|
|
220
|
-
release();
|
|
221
|
-
}
|
|
222
|
-
}
|