@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.
- package/README.md +1 -1
- package/dist/bundles/{mediabunny.mjs → mediabunny.js} +21963 -21390
- 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/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.d.ts +0 -9
- package/dist/modules/src/node.d.ts.map +0 -1
- 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,1017 +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 { Bitstream, COLOR_PRIMARIES_MAP, MATRIX_COEFFICIENTS_MAP, TRANSFER_CHARACTERISTICS_MAP, UNDETERMINED_LANGUAGE, assert, assertNever, colorSpaceIsComplete, imageMimeTypeToExtension, keyValueIterator, normalizeRotation, promiseWithResolvers, roundToMultiple, textEncoder, toUint8Array, uint8ArraysAreEqual, writeBits, } from '../misc.js';
|
|
9
|
-
import { CODEC_STRING_MAP, EBMLFloat32, EBMLFloat64, EBMLId, EBMLSignedInt, EBMLUnicodeString, EBMLWriter, } from './ebml.js';
|
|
10
|
-
import { buildMatroskaMimeType } from './matroska-misc.js';
|
|
11
|
-
import { WebMOutputFormat } from '../output-format.js';
|
|
12
|
-
import { formatSubtitleTimestamp, inlineTimestampRegex, parseSubtitleTimestamp, convertDialogueLineToMkvFormat, } from '../subtitles.js';
|
|
13
|
-
import { OPUS_SAMPLE_RATE, PCM_AUDIO_CODECS, generateAv1CodecConfigurationFromCodecString, generateVp9CodecConfigurationFromCodecString, parsePcmCodec, validateAudioChunkMetadata, validateSubtitleMetadata, validateVideoChunkMetadata, } from '../codec.js';
|
|
14
|
-
import { Muxer } from '../muxer.js';
|
|
15
|
-
import { parseOpusIdentificationHeader } from '../codec-data.js';
|
|
16
|
-
import { AttachedFile } from '../metadata.js';
|
|
17
|
-
const MIN_CLUSTER_TIMESTAMP_MS = /* #__PURE__ */ -(2 ** 15);
|
|
18
|
-
const MAX_CLUSTER_TIMESTAMP_MS = /* #__PURE__ */ 2 ** 15 - 1;
|
|
19
|
-
const APP_NAME = 'Mediabunny';
|
|
20
|
-
const SEGMENT_SIZE_BYTES = 6;
|
|
21
|
-
const CLUSTER_SIZE_BYTES = 5;
|
|
22
|
-
const TRACK_TYPE_MAP = {
|
|
23
|
-
video: 1,
|
|
24
|
-
audio: 2,
|
|
25
|
-
subtitle: 17,
|
|
26
|
-
};
|
|
27
|
-
export class MatroskaMuxer extends Muxer {
|
|
28
|
-
constructor(output, format) {
|
|
29
|
-
super(output);
|
|
30
|
-
this.trackDatas = [];
|
|
31
|
-
this.allTracksKnown = promiseWithResolvers();
|
|
32
|
-
this.segment = null;
|
|
33
|
-
this.segmentInfo = null;
|
|
34
|
-
this.seekHead = null;
|
|
35
|
-
this.tracksElement = null;
|
|
36
|
-
this.tagsElement = null;
|
|
37
|
-
this.attachmentsElement = null;
|
|
38
|
-
this.segmentDuration = null;
|
|
39
|
-
this.cues = null;
|
|
40
|
-
this.currentCluster = null;
|
|
41
|
-
this.currentClusterStartMsTimestamp = null;
|
|
42
|
-
this.currentClusterMaxMsTimestamp = null;
|
|
43
|
-
this.trackDatasInCurrentCluster = new Map();
|
|
44
|
-
this.duration = 0;
|
|
45
|
-
this.writer = output._writer;
|
|
46
|
-
this.format = format;
|
|
47
|
-
this.ebmlWriter = new EBMLWriter(this.writer);
|
|
48
|
-
if (this.format._options.appendOnly) {
|
|
49
|
-
this.writer.ensureMonotonicity = true;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
async start() {
|
|
53
|
-
const release = await this.mutex.acquire();
|
|
54
|
-
this.writeEBMLHeader();
|
|
55
|
-
this.createSegmentInfo();
|
|
56
|
-
this.createCues();
|
|
57
|
-
await this.writer.flush();
|
|
58
|
-
release();
|
|
59
|
-
}
|
|
60
|
-
writeEBMLHeader() {
|
|
61
|
-
if (this.format._options.onEbmlHeader) {
|
|
62
|
-
this.writer.startTrackingWrites();
|
|
63
|
-
}
|
|
64
|
-
const ebmlHeader = { id: EBMLId.EBML, data: [
|
|
65
|
-
{ id: EBMLId.EBMLVersion, data: 1 },
|
|
66
|
-
{ id: EBMLId.EBMLReadVersion, data: 1 },
|
|
67
|
-
{ id: EBMLId.EBMLMaxIDLength, data: 4 },
|
|
68
|
-
{ id: EBMLId.EBMLMaxSizeLength, data: 8 },
|
|
69
|
-
{ id: EBMLId.DocType, data: this.format instanceof WebMOutputFormat ? 'webm' : 'matroska' },
|
|
70
|
-
{ id: EBMLId.DocTypeVersion, data: 2 },
|
|
71
|
-
{ id: EBMLId.DocTypeReadVersion, data: 2 },
|
|
72
|
-
] };
|
|
73
|
-
this.ebmlWriter.writeEBML(ebmlHeader);
|
|
74
|
-
if (this.format._options.onEbmlHeader) {
|
|
75
|
-
const { data, start } = this.writer.stopTrackingWrites(); // start should be 0
|
|
76
|
-
this.format._options.onEbmlHeader(data, start);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Creates a SeekHead element which is positioned near the start of the file and allows the media player to seek to
|
|
81
|
-
* relevant sections more easily. Since we don't know the positions of those sections yet, we'll set them later.
|
|
82
|
-
*/
|
|
83
|
-
maybeCreateSeekHead(writeOffsets) {
|
|
84
|
-
if (this.format._options.appendOnly) {
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
const kaxCues = new Uint8Array([0x1c, 0x53, 0xbb, 0x6b]);
|
|
88
|
-
const kaxInfo = new Uint8Array([0x15, 0x49, 0xa9, 0x66]);
|
|
89
|
-
const kaxTracks = new Uint8Array([0x16, 0x54, 0xae, 0x6b]);
|
|
90
|
-
const kaxAttachments = new Uint8Array([0x19, 0x41, 0xa4, 0x69]);
|
|
91
|
-
const kaxTags = new Uint8Array([0x12, 0x54, 0xc3, 0x67]);
|
|
92
|
-
const seekHead = { id: EBMLId.SeekHead, data: [
|
|
93
|
-
{ id: EBMLId.Seek, data: [
|
|
94
|
-
{ id: EBMLId.SeekID, data: kaxCues },
|
|
95
|
-
{
|
|
96
|
-
id: EBMLId.SeekPosition,
|
|
97
|
-
size: 5,
|
|
98
|
-
data: writeOffsets
|
|
99
|
-
? this.ebmlWriter.offsets.get(this.cues) - this.segmentDataOffset
|
|
100
|
-
: 0,
|
|
101
|
-
},
|
|
102
|
-
] },
|
|
103
|
-
{ id: EBMLId.Seek, data: [
|
|
104
|
-
{ id: EBMLId.SeekID, data: kaxInfo },
|
|
105
|
-
{
|
|
106
|
-
id: EBMLId.SeekPosition,
|
|
107
|
-
size: 5,
|
|
108
|
-
data: writeOffsets
|
|
109
|
-
? this.ebmlWriter.offsets.get(this.segmentInfo) - this.segmentDataOffset
|
|
110
|
-
: 0,
|
|
111
|
-
},
|
|
112
|
-
] },
|
|
113
|
-
{ id: EBMLId.Seek, data: [
|
|
114
|
-
{ id: EBMLId.SeekID, data: kaxTracks },
|
|
115
|
-
{
|
|
116
|
-
id: EBMLId.SeekPosition,
|
|
117
|
-
size: 5,
|
|
118
|
-
data: writeOffsets
|
|
119
|
-
? this.ebmlWriter.offsets.get(this.tracksElement) - this.segmentDataOffset
|
|
120
|
-
: 0,
|
|
121
|
-
},
|
|
122
|
-
] },
|
|
123
|
-
this.attachmentsElement
|
|
124
|
-
? { id: EBMLId.Seek, data: [
|
|
125
|
-
{ id: EBMLId.SeekID, data: kaxAttachments },
|
|
126
|
-
{
|
|
127
|
-
id: EBMLId.SeekPosition,
|
|
128
|
-
size: 5,
|
|
129
|
-
data: writeOffsets
|
|
130
|
-
? this.ebmlWriter.offsets.get(this.attachmentsElement) - this.segmentDataOffset
|
|
131
|
-
: 0,
|
|
132
|
-
},
|
|
133
|
-
] }
|
|
134
|
-
: null,
|
|
135
|
-
this.tagsElement
|
|
136
|
-
? { id: EBMLId.Seek, data: [
|
|
137
|
-
{ id: EBMLId.SeekID, data: kaxTags },
|
|
138
|
-
{
|
|
139
|
-
id: EBMLId.SeekPosition,
|
|
140
|
-
size: 5,
|
|
141
|
-
data: writeOffsets
|
|
142
|
-
? this.ebmlWriter.offsets.get(this.tagsElement) - this.segmentDataOffset
|
|
143
|
-
: 0,
|
|
144
|
-
},
|
|
145
|
-
] }
|
|
146
|
-
: null,
|
|
147
|
-
] };
|
|
148
|
-
this.seekHead = seekHead;
|
|
149
|
-
}
|
|
150
|
-
createSegmentInfo() {
|
|
151
|
-
const segmentDuration = { id: EBMLId.Duration, data: new EBMLFloat64(0) };
|
|
152
|
-
this.segmentDuration = segmentDuration;
|
|
153
|
-
const segmentInfo = { id: EBMLId.Info, data: [
|
|
154
|
-
{ id: EBMLId.TimestampScale, data: 1e6 },
|
|
155
|
-
{ id: EBMLId.MuxingApp, data: APP_NAME },
|
|
156
|
-
{ id: EBMLId.WritingApp, data: APP_NAME },
|
|
157
|
-
!this.format._options.appendOnly ? segmentDuration : null,
|
|
158
|
-
] };
|
|
159
|
-
this.segmentInfo = segmentInfo;
|
|
160
|
-
}
|
|
161
|
-
createTracks() {
|
|
162
|
-
const tracksElement = { id: EBMLId.Tracks, data: [] };
|
|
163
|
-
this.tracksElement = tracksElement;
|
|
164
|
-
for (const trackData of this.trackDatas) {
|
|
165
|
-
const codecId = CODEC_STRING_MAP[trackData.track.source._codec];
|
|
166
|
-
assert(codecId);
|
|
167
|
-
let seekPreRollNs = 0;
|
|
168
|
-
if (trackData.type === 'audio' && trackData.track.source._codec === 'opus') {
|
|
169
|
-
seekPreRollNs = 1e6 * 80; // In "Matroska ticks" (nanoseconds)
|
|
170
|
-
const description = trackData.info.decoderConfig.description;
|
|
171
|
-
if (description) {
|
|
172
|
-
const bytes = toUint8Array(description);
|
|
173
|
-
const header = parseOpusIdentificationHeader(bytes);
|
|
174
|
-
// Use the preSkip value from the header
|
|
175
|
-
seekPreRollNs = Math.round(1e9 * (header.preSkip / OPUS_SAMPLE_RATE));
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
tracksElement.data.push({ id: EBMLId.TrackEntry, data: [
|
|
179
|
-
{ id: EBMLId.TrackNumber, data: trackData.track.id },
|
|
180
|
-
{ id: EBMLId.TrackUID, data: trackData.track.id },
|
|
181
|
-
{ id: EBMLId.TrackType, data: TRACK_TYPE_MAP[trackData.type] },
|
|
182
|
-
trackData.track.metadata.disposition?.default === false
|
|
183
|
-
? { id: EBMLId.FlagDefault, data: 0 }
|
|
184
|
-
: null,
|
|
185
|
-
trackData.track.metadata.disposition?.forced
|
|
186
|
-
? { id: EBMLId.FlagForced, data: 1 }
|
|
187
|
-
: null,
|
|
188
|
-
trackData.track.metadata.disposition?.hearingImpaired
|
|
189
|
-
? { id: EBMLId.FlagHearingImpaired, data: 1 }
|
|
190
|
-
: null,
|
|
191
|
-
trackData.track.metadata.disposition?.visuallyImpaired
|
|
192
|
-
? { id: EBMLId.FlagVisualImpaired, data: 1 }
|
|
193
|
-
: null,
|
|
194
|
-
trackData.track.metadata.disposition?.original
|
|
195
|
-
? { id: EBMLId.FlagOriginal, data: 1 }
|
|
196
|
-
: null,
|
|
197
|
-
trackData.track.metadata.disposition?.commentary
|
|
198
|
-
? { id: EBMLId.FlagCommentary, data: 1 }
|
|
199
|
-
: null,
|
|
200
|
-
{ id: EBMLId.FlagLacing, data: 0 },
|
|
201
|
-
{ id: EBMLId.Language, data: trackData.track.metadata.languageCode ?? UNDETERMINED_LANGUAGE },
|
|
202
|
-
{ id: EBMLId.CodecID, data: codecId },
|
|
203
|
-
{ id: EBMLId.CodecDelay, data: 0 },
|
|
204
|
-
{ id: EBMLId.SeekPreRoll, data: seekPreRollNs },
|
|
205
|
-
trackData.track.metadata.name !== undefined
|
|
206
|
-
? { id: EBMLId.Name, data: new EBMLUnicodeString(trackData.track.metadata.name) }
|
|
207
|
-
: null,
|
|
208
|
-
(trackData.type === 'video' ? this.videoSpecificTrackInfo(trackData) : null),
|
|
209
|
-
(trackData.type === 'audio' ? this.audioSpecificTrackInfo(trackData) : null),
|
|
210
|
-
(trackData.type === 'subtitle' ? this.subtitleSpecificTrackInfo(trackData) : null),
|
|
211
|
-
] });
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
videoSpecificTrackInfo(trackData) {
|
|
215
|
-
const { frameRate, rotation } = trackData.track.metadata;
|
|
216
|
-
const elements = [
|
|
217
|
-
(trackData.info.decoderConfig.description
|
|
218
|
-
? {
|
|
219
|
-
id: EBMLId.CodecPrivate,
|
|
220
|
-
data: toUint8Array(trackData.info.decoderConfig.description),
|
|
221
|
-
}
|
|
222
|
-
: null),
|
|
223
|
-
(frameRate
|
|
224
|
-
? {
|
|
225
|
-
id: EBMLId.DefaultDuration,
|
|
226
|
-
data: 1e9 / frameRate,
|
|
227
|
-
}
|
|
228
|
-
: null),
|
|
229
|
-
];
|
|
230
|
-
// Convert from clockwise to counter-clockwise
|
|
231
|
-
const flippedRotation = rotation ? normalizeRotation(-rotation) : 0;
|
|
232
|
-
const colorSpace = trackData.info.decoderConfig.colorSpace;
|
|
233
|
-
const videoElement = { id: EBMLId.Video, data: [
|
|
234
|
-
{ id: EBMLId.PixelWidth, data: trackData.info.width },
|
|
235
|
-
{ id: EBMLId.PixelHeight, data: trackData.info.height },
|
|
236
|
-
trackData.info.alphaMode ? { id: EBMLId.AlphaMode, data: 1 } : null,
|
|
237
|
-
(colorSpaceIsComplete(colorSpace)
|
|
238
|
-
? {
|
|
239
|
-
id: EBMLId.Colour,
|
|
240
|
-
data: [
|
|
241
|
-
{
|
|
242
|
-
id: EBMLId.MatrixCoefficients,
|
|
243
|
-
data: MATRIX_COEFFICIENTS_MAP[colorSpace.matrix],
|
|
244
|
-
},
|
|
245
|
-
{
|
|
246
|
-
id: EBMLId.TransferCharacteristics,
|
|
247
|
-
data: TRANSFER_CHARACTERISTICS_MAP[colorSpace.transfer],
|
|
248
|
-
},
|
|
249
|
-
{
|
|
250
|
-
id: EBMLId.Primaries,
|
|
251
|
-
data: COLOR_PRIMARIES_MAP[colorSpace.primaries],
|
|
252
|
-
},
|
|
253
|
-
{
|
|
254
|
-
id: EBMLId.Range,
|
|
255
|
-
data: colorSpace.fullRange ? 2 : 1,
|
|
256
|
-
},
|
|
257
|
-
],
|
|
258
|
-
}
|
|
259
|
-
: null),
|
|
260
|
-
(flippedRotation
|
|
261
|
-
? {
|
|
262
|
-
id: EBMLId.Projection,
|
|
263
|
-
data: [
|
|
264
|
-
{
|
|
265
|
-
id: EBMLId.ProjectionType,
|
|
266
|
-
data: 0, // rectangular
|
|
267
|
-
},
|
|
268
|
-
{
|
|
269
|
-
id: EBMLId.ProjectionPoseRoll,
|
|
270
|
-
data: new EBMLFloat32((flippedRotation + 180) % 360 - 180), // [0, 270] -> [-180, 90]
|
|
271
|
-
},
|
|
272
|
-
],
|
|
273
|
-
}
|
|
274
|
-
: null),
|
|
275
|
-
] };
|
|
276
|
-
elements.push(videoElement);
|
|
277
|
-
return elements;
|
|
278
|
-
}
|
|
279
|
-
audioSpecificTrackInfo(trackData) {
|
|
280
|
-
const pcmInfo = PCM_AUDIO_CODECS.includes(trackData.track.source._codec)
|
|
281
|
-
? parsePcmCodec(trackData.track.source._codec)
|
|
282
|
-
: null;
|
|
283
|
-
return [
|
|
284
|
-
(trackData.info.decoderConfig.description
|
|
285
|
-
? {
|
|
286
|
-
id: EBMLId.CodecPrivate,
|
|
287
|
-
data: toUint8Array(trackData.info.decoderConfig.description),
|
|
288
|
-
}
|
|
289
|
-
: null),
|
|
290
|
-
{ id: EBMLId.Audio, data: [
|
|
291
|
-
{ id: EBMLId.SamplingFrequency, data: new EBMLFloat32(trackData.info.sampleRate) },
|
|
292
|
-
{ id: EBMLId.Channels, data: trackData.info.numberOfChannels },
|
|
293
|
-
pcmInfo ? { id: EBMLId.BitDepth, data: 8 * pcmInfo.sampleSize } : null,
|
|
294
|
-
] },
|
|
295
|
-
];
|
|
296
|
-
}
|
|
297
|
-
subtitleSpecificTrackInfo(trackData) {
|
|
298
|
-
return [
|
|
299
|
-
{ id: EBMLId.CodecPrivate, data: textEncoder.encode(trackData.info.config.description) },
|
|
300
|
-
];
|
|
301
|
-
}
|
|
302
|
-
maybeCreateTags() {
|
|
303
|
-
const simpleTags = [];
|
|
304
|
-
const addSimpleTag = (key, value) => {
|
|
305
|
-
simpleTags.push({ id: EBMLId.SimpleTag, data: [
|
|
306
|
-
{ id: EBMLId.TagName, data: new EBMLUnicodeString(key) },
|
|
307
|
-
typeof value === 'string'
|
|
308
|
-
? { id: EBMLId.TagString, data: new EBMLUnicodeString(value) }
|
|
309
|
-
: { id: EBMLId.TagBinary, data: value },
|
|
310
|
-
] });
|
|
311
|
-
};
|
|
312
|
-
const metadataTags = this.output._metadataTags;
|
|
313
|
-
const writtenTags = new Set();
|
|
314
|
-
for (const { key, value } of keyValueIterator(metadataTags)) {
|
|
315
|
-
switch (key) {
|
|
316
|
-
case 'title':
|
|
317
|
-
{
|
|
318
|
-
addSimpleTag('TITLE', value);
|
|
319
|
-
writtenTags.add('TITLE');
|
|
320
|
-
}
|
|
321
|
-
;
|
|
322
|
-
break;
|
|
323
|
-
case 'description':
|
|
324
|
-
{
|
|
325
|
-
addSimpleTag('DESCRIPTION', value);
|
|
326
|
-
writtenTags.add('DESCRIPTION');
|
|
327
|
-
}
|
|
328
|
-
;
|
|
329
|
-
break;
|
|
330
|
-
case 'artist':
|
|
331
|
-
{
|
|
332
|
-
addSimpleTag('ARTIST', value);
|
|
333
|
-
writtenTags.add('ARTIST');
|
|
334
|
-
}
|
|
335
|
-
;
|
|
336
|
-
break;
|
|
337
|
-
case 'album':
|
|
338
|
-
{
|
|
339
|
-
addSimpleTag('ALBUM', value);
|
|
340
|
-
writtenTags.add('ALBUM');
|
|
341
|
-
}
|
|
342
|
-
;
|
|
343
|
-
break;
|
|
344
|
-
case 'albumArtist':
|
|
345
|
-
{
|
|
346
|
-
addSimpleTag('ALBUM_ARTIST', value);
|
|
347
|
-
writtenTags.add('ALBUM_ARTIST');
|
|
348
|
-
}
|
|
349
|
-
;
|
|
350
|
-
break;
|
|
351
|
-
case 'genre':
|
|
352
|
-
{
|
|
353
|
-
addSimpleTag('GENRE', value);
|
|
354
|
-
writtenTags.add('GENRE');
|
|
355
|
-
}
|
|
356
|
-
;
|
|
357
|
-
break;
|
|
358
|
-
case 'comment':
|
|
359
|
-
{
|
|
360
|
-
addSimpleTag('COMMENT', value);
|
|
361
|
-
writtenTags.add('COMMENT');
|
|
362
|
-
}
|
|
363
|
-
;
|
|
364
|
-
break;
|
|
365
|
-
case 'lyrics':
|
|
366
|
-
{
|
|
367
|
-
addSimpleTag('LYRICS', value);
|
|
368
|
-
writtenTags.add('LYRICS');
|
|
369
|
-
}
|
|
370
|
-
;
|
|
371
|
-
break;
|
|
372
|
-
case 'date':
|
|
373
|
-
{
|
|
374
|
-
addSimpleTag('DATE', value.toISOString().slice(0, 10));
|
|
375
|
-
writtenTags.add('DATE');
|
|
376
|
-
}
|
|
377
|
-
;
|
|
378
|
-
break;
|
|
379
|
-
case 'trackNumber':
|
|
380
|
-
{
|
|
381
|
-
const string = metadataTags.tracksTotal !== undefined
|
|
382
|
-
? `${value}/${metadataTags.tracksTotal}`
|
|
383
|
-
: value.toString();
|
|
384
|
-
addSimpleTag('PART_NUMBER', string);
|
|
385
|
-
writtenTags.add('PART_NUMBER');
|
|
386
|
-
}
|
|
387
|
-
;
|
|
388
|
-
break;
|
|
389
|
-
case 'discNumber':
|
|
390
|
-
{
|
|
391
|
-
const string = metadataTags.discsTotal !== undefined
|
|
392
|
-
? `${value}/${metadataTags.discsTotal}`
|
|
393
|
-
: value.toString();
|
|
394
|
-
addSimpleTag('DISC', string);
|
|
395
|
-
writtenTags.add('DISC');
|
|
396
|
-
}
|
|
397
|
-
;
|
|
398
|
-
break;
|
|
399
|
-
case 'tracksTotal':
|
|
400
|
-
case 'discsTotal':
|
|
401
|
-
{
|
|
402
|
-
// Handled with trackNumber and discNumber respectively
|
|
403
|
-
}
|
|
404
|
-
;
|
|
405
|
-
break;
|
|
406
|
-
case 'images':
|
|
407
|
-
case 'raw':
|
|
408
|
-
{
|
|
409
|
-
// Handled elsewhere
|
|
410
|
-
}
|
|
411
|
-
;
|
|
412
|
-
break;
|
|
413
|
-
default: assertNever(key);
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
if (metadataTags.raw) {
|
|
417
|
-
for (const key in metadataTags.raw) {
|
|
418
|
-
const value = metadataTags.raw[key];
|
|
419
|
-
if (value == null || writtenTags.has(key)) {
|
|
420
|
-
continue;
|
|
421
|
-
}
|
|
422
|
-
if (typeof value === 'string' || value instanceof Uint8Array) {
|
|
423
|
-
addSimpleTag(key, value);
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
if (simpleTags.length === 0) {
|
|
428
|
-
return;
|
|
429
|
-
}
|
|
430
|
-
this.tagsElement = {
|
|
431
|
-
id: EBMLId.Tags,
|
|
432
|
-
data: [{ id: EBMLId.Tag, data: [
|
|
433
|
-
{ id: EBMLId.Targets, data: [
|
|
434
|
-
{ id: EBMLId.TargetTypeValue, data: 50 },
|
|
435
|
-
{ id: EBMLId.TargetType, data: 'MOVIE' },
|
|
436
|
-
] },
|
|
437
|
-
...simpleTags,
|
|
438
|
-
] }],
|
|
439
|
-
};
|
|
440
|
-
}
|
|
441
|
-
maybeCreateAttachments() {
|
|
442
|
-
const metadataTags = this.output._metadataTags;
|
|
443
|
-
const elements = [];
|
|
444
|
-
const existingFileUids = new Set();
|
|
445
|
-
const images = metadataTags.images ?? [];
|
|
446
|
-
for (const image of images) {
|
|
447
|
-
let imageName = image.name;
|
|
448
|
-
if (imageName === undefined) {
|
|
449
|
-
const baseName = image.kind === 'coverFront' ? 'cover' : image.kind === 'coverBack' ? 'back' : 'image';
|
|
450
|
-
imageName = baseName + (imageMimeTypeToExtension(image.mimeType) ?? '');
|
|
451
|
-
}
|
|
452
|
-
let fileUid;
|
|
453
|
-
while (true) {
|
|
454
|
-
// Generate a random 64-bit unsigned integer
|
|
455
|
-
fileUid = 0n;
|
|
456
|
-
for (let i = 0; i < 8; i++) {
|
|
457
|
-
fileUid <<= 8n;
|
|
458
|
-
fileUid |= BigInt(Math.floor(Math.random() * 256));
|
|
459
|
-
}
|
|
460
|
-
if (fileUid !== 0n && !existingFileUids.has(fileUid)) {
|
|
461
|
-
break;
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
existingFileUids.add(fileUid);
|
|
465
|
-
elements.push({
|
|
466
|
-
id: EBMLId.AttachedFile,
|
|
467
|
-
data: [
|
|
468
|
-
image.description !== undefined
|
|
469
|
-
? { id: EBMLId.FileDescription, data: new EBMLUnicodeString(image.description) }
|
|
470
|
-
: null,
|
|
471
|
-
{ id: EBMLId.FileName, data: new EBMLUnicodeString(imageName) },
|
|
472
|
-
{ id: EBMLId.FileMediaType, data: image.mimeType },
|
|
473
|
-
{ id: EBMLId.FileData, data: image.data },
|
|
474
|
-
{ id: EBMLId.FileUID, data: fileUid },
|
|
475
|
-
],
|
|
476
|
-
});
|
|
477
|
-
}
|
|
478
|
-
// Add all AttachedFiles from the raw metadata
|
|
479
|
-
for (const [key, value] of Object.entries(metadataTags.raw ?? {})) {
|
|
480
|
-
if (!(value instanceof AttachedFile)) {
|
|
481
|
-
continue;
|
|
482
|
-
}
|
|
483
|
-
const keyIsNumeric = /^\d+$/.test(key);
|
|
484
|
-
if (!keyIsNumeric) {
|
|
485
|
-
continue;
|
|
486
|
-
}
|
|
487
|
-
if (images.find(x => x.mimeType === value.mimeType && uint8ArraysAreEqual(x.data, value.data))) {
|
|
488
|
-
// This attached file has very likely already been added as an image above
|
|
489
|
-
// (happens when remuxing Matroska)
|
|
490
|
-
continue;
|
|
491
|
-
}
|
|
492
|
-
elements.push({
|
|
493
|
-
id: EBMLId.AttachedFile,
|
|
494
|
-
data: [
|
|
495
|
-
value.description !== undefined
|
|
496
|
-
? { id: EBMLId.FileDescription, data: new EBMLUnicodeString(value.description) }
|
|
497
|
-
: null,
|
|
498
|
-
{ id: EBMLId.FileName, data: new EBMLUnicodeString(value.name ?? '') },
|
|
499
|
-
{ id: EBMLId.FileMediaType, data: value.mimeType ?? '' },
|
|
500
|
-
{ id: EBMLId.FileData, data: value.data },
|
|
501
|
-
{ id: EBMLId.FileUID, data: BigInt(key) },
|
|
502
|
-
],
|
|
503
|
-
});
|
|
504
|
-
}
|
|
505
|
-
if (elements.length === 0) {
|
|
506
|
-
return;
|
|
507
|
-
}
|
|
508
|
-
this.attachmentsElement = { id: EBMLId.Attachments, data: elements };
|
|
509
|
-
}
|
|
510
|
-
createSegment() {
|
|
511
|
-
this.createTracks();
|
|
512
|
-
this.maybeCreateTags();
|
|
513
|
-
this.maybeCreateAttachments();
|
|
514
|
-
this.maybeCreateSeekHead(false);
|
|
515
|
-
const segment = {
|
|
516
|
-
id: EBMLId.Segment,
|
|
517
|
-
size: this.format._options.appendOnly ? -1 : SEGMENT_SIZE_BYTES,
|
|
518
|
-
data: [
|
|
519
|
-
this.seekHead, // null if append-only
|
|
520
|
-
this.segmentInfo,
|
|
521
|
-
this.tracksElement,
|
|
522
|
-
// Matroska spec says put this at the end of the file, but I think placing it before the first cluster
|
|
523
|
-
// makes more sense, and FFmpeg agrees (argumentum ad ffmpegum fallacy)
|
|
524
|
-
this.attachmentsElement,
|
|
525
|
-
this.tagsElement,
|
|
526
|
-
],
|
|
527
|
-
};
|
|
528
|
-
this.segment = segment;
|
|
529
|
-
if (this.format._options.onSegmentHeader) {
|
|
530
|
-
this.writer.startTrackingWrites();
|
|
531
|
-
}
|
|
532
|
-
this.ebmlWriter.writeEBML(segment);
|
|
533
|
-
if (this.format._options.onSegmentHeader) {
|
|
534
|
-
const { data, start } = this.writer.stopTrackingWrites();
|
|
535
|
-
this.format._options.onSegmentHeader(data, start);
|
|
536
|
-
}
|
|
537
|
-
}
|
|
538
|
-
createCues() {
|
|
539
|
-
this.cues = { id: EBMLId.Cues, data: [] };
|
|
540
|
-
}
|
|
541
|
-
get segmentDataOffset() {
|
|
542
|
-
assert(this.segment);
|
|
543
|
-
return this.ebmlWriter.dataOffsets.get(this.segment);
|
|
544
|
-
}
|
|
545
|
-
allTracksAreKnown() {
|
|
546
|
-
for (const track of this.output._tracks) {
|
|
547
|
-
if (!track.source._closed && !this.trackDatas.some(x => x.track === track)) {
|
|
548
|
-
return false; // We haven't seen a sample from this open track yet
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
return true;
|
|
552
|
-
}
|
|
553
|
-
async getMimeType() {
|
|
554
|
-
await this.allTracksKnown.promise;
|
|
555
|
-
const codecStrings = this.trackDatas.map((trackData) => {
|
|
556
|
-
if (trackData.type === 'video') {
|
|
557
|
-
return trackData.info.decoderConfig.codec;
|
|
558
|
-
}
|
|
559
|
-
else if (trackData.type === 'audio') {
|
|
560
|
-
return trackData.info.decoderConfig.codec;
|
|
561
|
-
}
|
|
562
|
-
else {
|
|
563
|
-
const map = {
|
|
564
|
-
webvtt: 'S_TEXT/WEBVTT',
|
|
565
|
-
tx3g: 'S_TEXT/UTF8', // Matroska doesn't have tx3g, convert to SRT
|
|
566
|
-
ttml: 'S_TEXT/WEBVTT', // Matroska doesn't have TTML, convert to WebVTT
|
|
567
|
-
srt: 'S_TEXT/UTF8',
|
|
568
|
-
ass: 'S_TEXT/ASS',
|
|
569
|
-
ssa: 'S_TEXT/SSA',
|
|
570
|
-
};
|
|
571
|
-
return map[trackData.track.source._codec];
|
|
572
|
-
}
|
|
573
|
-
});
|
|
574
|
-
return buildMatroskaMimeType({
|
|
575
|
-
isWebM: this.format instanceof WebMOutputFormat,
|
|
576
|
-
hasVideo: this.trackDatas.some(x => x.type === 'video'),
|
|
577
|
-
hasAudio: this.trackDatas.some(x => x.type === 'audio'),
|
|
578
|
-
codecStrings,
|
|
579
|
-
});
|
|
580
|
-
}
|
|
581
|
-
getVideoTrackData(track, packet, meta) {
|
|
582
|
-
const existingTrackData = this.trackDatas.find(x => x.track === track);
|
|
583
|
-
if (existingTrackData) {
|
|
584
|
-
return existingTrackData;
|
|
585
|
-
}
|
|
586
|
-
validateVideoChunkMetadata(meta);
|
|
587
|
-
assert(meta);
|
|
588
|
-
assert(meta.decoderConfig);
|
|
589
|
-
assert(meta.decoderConfig.codedWidth !== undefined);
|
|
590
|
-
assert(meta.decoderConfig.codedHeight !== undefined);
|
|
591
|
-
const newTrackData = {
|
|
592
|
-
track,
|
|
593
|
-
type: 'video',
|
|
594
|
-
info: {
|
|
595
|
-
width: meta.decoderConfig.codedWidth,
|
|
596
|
-
height: meta.decoderConfig.codedHeight,
|
|
597
|
-
decoderConfig: meta.decoderConfig,
|
|
598
|
-
alphaMode: !!packet.sideData.alpha, // The first packet determines if this track has alpha or not
|
|
599
|
-
},
|
|
600
|
-
chunkQueue: [],
|
|
601
|
-
lastWrittenMsTimestamp: null,
|
|
602
|
-
};
|
|
603
|
-
if (track.source._codec === 'vp9') {
|
|
604
|
-
// https://www.webmproject.org/docs/container specifies that VP9 "SHOULD" make use of the CodecPrivate
|
|
605
|
-
// field. Since WebCodecs makes no use of the description field for VP9, we need to derive it ourselves:
|
|
606
|
-
newTrackData.info.decoderConfig = {
|
|
607
|
-
...newTrackData.info.decoderConfig,
|
|
608
|
-
description: new Uint8Array(generateVp9CodecConfigurationFromCodecString(newTrackData.info.decoderConfig.codec)),
|
|
609
|
-
};
|
|
610
|
-
}
|
|
611
|
-
else if (track.source._codec === 'av1') {
|
|
612
|
-
// Per https://github.com/ietf-wg-cellar/matroska-specification/blob/master/codec/av1.md, AV1 requires
|
|
613
|
-
// CodecPrivate to be set, but WebCodecs makes no use of the description field for AV1. Thus, let's derive
|
|
614
|
-
// it ourselves:
|
|
615
|
-
newTrackData.info.decoderConfig = {
|
|
616
|
-
...newTrackData.info.decoderConfig,
|
|
617
|
-
description: new Uint8Array(generateAv1CodecConfigurationFromCodecString(newTrackData.info.decoderConfig.codec)),
|
|
618
|
-
};
|
|
619
|
-
}
|
|
620
|
-
this.trackDatas.push(newTrackData);
|
|
621
|
-
this.trackDatas.sort((a, b) => a.track.id - b.track.id);
|
|
622
|
-
if (this.allTracksAreKnown()) {
|
|
623
|
-
this.allTracksKnown.resolve();
|
|
624
|
-
}
|
|
625
|
-
return newTrackData;
|
|
626
|
-
}
|
|
627
|
-
getAudioTrackData(track, meta) {
|
|
628
|
-
const existingTrackData = this.trackDatas.find(x => x.track === track);
|
|
629
|
-
if (existingTrackData) {
|
|
630
|
-
return existingTrackData;
|
|
631
|
-
}
|
|
632
|
-
validateAudioChunkMetadata(meta);
|
|
633
|
-
assert(meta);
|
|
634
|
-
assert(meta.decoderConfig);
|
|
635
|
-
const newTrackData = {
|
|
636
|
-
track,
|
|
637
|
-
type: 'audio',
|
|
638
|
-
info: {
|
|
639
|
-
numberOfChannels: meta.decoderConfig.numberOfChannels,
|
|
640
|
-
sampleRate: meta.decoderConfig.sampleRate,
|
|
641
|
-
decoderConfig: meta.decoderConfig,
|
|
642
|
-
},
|
|
643
|
-
chunkQueue: [],
|
|
644
|
-
lastWrittenMsTimestamp: null,
|
|
645
|
-
};
|
|
646
|
-
this.trackDatas.push(newTrackData);
|
|
647
|
-
this.trackDatas.sort((a, b) => a.track.id - b.track.id);
|
|
648
|
-
if (this.allTracksAreKnown()) {
|
|
649
|
-
this.allTracksKnown.resolve();
|
|
650
|
-
}
|
|
651
|
-
return newTrackData;
|
|
652
|
-
}
|
|
653
|
-
getSubtitleTrackData(track, meta) {
|
|
654
|
-
const existingTrackData = this.trackDatas.find(x => x.track === track);
|
|
655
|
-
if (existingTrackData) {
|
|
656
|
-
return existingTrackData;
|
|
657
|
-
}
|
|
658
|
-
validateSubtitleMetadata(meta);
|
|
659
|
-
assert(meta);
|
|
660
|
-
assert(meta.config);
|
|
661
|
-
const newTrackData = {
|
|
662
|
-
track,
|
|
663
|
-
type: 'subtitle',
|
|
664
|
-
info: {
|
|
665
|
-
config: meta.config,
|
|
666
|
-
},
|
|
667
|
-
chunkQueue: [],
|
|
668
|
-
lastWrittenMsTimestamp: null,
|
|
669
|
-
};
|
|
670
|
-
this.trackDatas.push(newTrackData);
|
|
671
|
-
this.trackDatas.sort((a, b) => a.track.id - b.track.id);
|
|
672
|
-
if (this.allTracksAreKnown()) {
|
|
673
|
-
this.allTracksKnown.resolve();
|
|
674
|
-
}
|
|
675
|
-
return newTrackData;
|
|
676
|
-
}
|
|
677
|
-
async addEncodedVideoPacket(track, packet, meta) {
|
|
678
|
-
const release = await this.mutex.acquire();
|
|
679
|
-
try {
|
|
680
|
-
const trackData = this.getVideoTrackData(track, packet, meta);
|
|
681
|
-
const isKeyFrame = packet.type === 'key';
|
|
682
|
-
let timestamp = this.validateAndNormalizeTimestamp(trackData.track, packet.timestamp, isKeyFrame);
|
|
683
|
-
let duration = packet.duration;
|
|
684
|
-
if (track.metadata.frameRate !== undefined) {
|
|
685
|
-
// Constrain the time values to the frame rate
|
|
686
|
-
timestamp = roundToMultiple(timestamp, 1 / track.metadata.frameRate);
|
|
687
|
-
duration = roundToMultiple(duration, 1 / track.metadata.frameRate);
|
|
688
|
-
}
|
|
689
|
-
const additions = trackData.info.alphaMode
|
|
690
|
-
? packet.sideData.alpha ?? null
|
|
691
|
-
: null;
|
|
692
|
-
const videoChunk = this.createInternalChunk(packet.data, timestamp, duration, packet.type, additions);
|
|
693
|
-
if (track.source._codec === 'vp9')
|
|
694
|
-
this.fixVP9ColorSpace(trackData, videoChunk);
|
|
695
|
-
trackData.chunkQueue.push(videoChunk);
|
|
696
|
-
await this.interleaveChunks();
|
|
697
|
-
}
|
|
698
|
-
finally {
|
|
699
|
-
release();
|
|
700
|
-
}
|
|
701
|
-
}
|
|
702
|
-
async addEncodedAudioPacket(track, packet, meta) {
|
|
703
|
-
const release = await this.mutex.acquire();
|
|
704
|
-
try {
|
|
705
|
-
const trackData = this.getAudioTrackData(track, meta);
|
|
706
|
-
const isKeyFrame = packet.type === 'key';
|
|
707
|
-
const timestamp = this.validateAndNormalizeTimestamp(trackData.track, packet.timestamp, isKeyFrame);
|
|
708
|
-
const audioChunk = this.createInternalChunk(packet.data, timestamp, packet.duration, packet.type);
|
|
709
|
-
trackData.chunkQueue.push(audioChunk);
|
|
710
|
-
await this.interleaveChunks();
|
|
711
|
-
}
|
|
712
|
-
finally {
|
|
713
|
-
release();
|
|
714
|
-
}
|
|
715
|
-
}
|
|
716
|
-
async addSubtitleCue(track, cue, meta) {
|
|
717
|
-
const release = await this.mutex.acquire();
|
|
718
|
-
try {
|
|
719
|
-
const trackData = this.getSubtitleTrackData(track, meta);
|
|
720
|
-
const timestamp = this.validateAndNormalizeTimestamp(trackData.track, cue.timestamp, true);
|
|
721
|
-
let bodyText = cue.text;
|
|
722
|
-
const timestampMs = Math.round(timestamp * 1000);
|
|
723
|
-
if (track.source._codec === 'ass' || track.source._codec === 'ssa') {
|
|
724
|
-
bodyText = convertDialogueLineToMkvFormat(bodyText);
|
|
725
|
-
}
|
|
726
|
-
else {
|
|
727
|
-
inlineTimestampRegex.lastIndex = 0;
|
|
728
|
-
bodyText = bodyText.replace(inlineTimestampRegex, (match) => {
|
|
729
|
-
const time = parseSubtitleTimestamp(match.slice(1, -1));
|
|
730
|
-
const offsetTime = time - timestampMs;
|
|
731
|
-
return `<${formatSubtitleTimestamp(offsetTime)}>`;
|
|
732
|
-
});
|
|
733
|
-
}
|
|
734
|
-
const body = textEncoder.encode(bodyText);
|
|
735
|
-
const additions = `${cue.settings ?? ''}\n${cue.identifier ?? ''}\n${cue.notes ?? ''}`;
|
|
736
|
-
const subtitleChunk = this.createInternalChunk(body, timestamp, cue.duration, 'key', additions.trim() ? textEncoder.encode(additions) : null);
|
|
737
|
-
trackData.chunkQueue.push(subtitleChunk);
|
|
738
|
-
await this.interleaveChunks();
|
|
739
|
-
}
|
|
740
|
-
finally {
|
|
741
|
-
release();
|
|
742
|
-
}
|
|
743
|
-
}
|
|
744
|
-
async interleaveChunks(isFinalCall = false) {
|
|
745
|
-
if (!isFinalCall && !this.allTracksAreKnown()) {
|
|
746
|
-
return; // We can't interleave yet as we don't yet know how many tracks we'll truly have
|
|
747
|
-
}
|
|
748
|
-
outer: while (true) {
|
|
749
|
-
let trackWithMinTimestamp = null;
|
|
750
|
-
let minTimestamp = Infinity;
|
|
751
|
-
for (const trackData of this.trackDatas) {
|
|
752
|
-
if (!isFinalCall && trackData.chunkQueue.length === 0 && !trackData.track.source._closed) {
|
|
753
|
-
break outer;
|
|
754
|
-
}
|
|
755
|
-
if (trackData.chunkQueue.length > 0 && trackData.chunkQueue[0].timestamp < minTimestamp) {
|
|
756
|
-
trackWithMinTimestamp = trackData;
|
|
757
|
-
minTimestamp = trackData.chunkQueue[0].timestamp;
|
|
758
|
-
}
|
|
759
|
-
}
|
|
760
|
-
if (!trackWithMinTimestamp) {
|
|
761
|
-
break;
|
|
762
|
-
}
|
|
763
|
-
const chunk = trackWithMinTimestamp.chunkQueue.shift();
|
|
764
|
-
this.writeBlock(trackWithMinTimestamp, chunk);
|
|
765
|
-
}
|
|
766
|
-
if (!isFinalCall) {
|
|
767
|
-
await this.writer.flush();
|
|
768
|
-
}
|
|
769
|
-
}
|
|
770
|
-
/**
|
|
771
|
-
* Due to [a bug in Chromium](https://bugs.chromium.org/p/chromium/issues/detail?id=1377842), VP9 streams often
|
|
772
|
-
* lack color space information. This method patches in that information.
|
|
773
|
-
*/
|
|
774
|
-
fixVP9ColorSpace(trackData, chunk) {
|
|
775
|
-
// http://downloads.webmproject.org/docs/vp9/vp9-bitstream_superframe-and-uncompressed-header_v1.0.pdf
|
|
776
|
-
if (chunk.type !== 'key')
|
|
777
|
-
return;
|
|
778
|
-
if (!trackData.info.decoderConfig.colorSpace || !trackData.info.decoderConfig.colorSpace.matrix)
|
|
779
|
-
return;
|
|
780
|
-
const bitstream = new Bitstream(chunk.data);
|
|
781
|
-
bitstream.skipBits(2);
|
|
782
|
-
const profileLowBit = bitstream.readBits(1);
|
|
783
|
-
const profileHighBit = bitstream.readBits(1);
|
|
784
|
-
const profile = (profileHighBit << 1) + profileLowBit;
|
|
785
|
-
if (profile === 3)
|
|
786
|
-
bitstream.skipBits(1);
|
|
787
|
-
const showExistingFrame = bitstream.readBits(1);
|
|
788
|
-
if (showExistingFrame)
|
|
789
|
-
return;
|
|
790
|
-
const frameType = bitstream.readBits(1);
|
|
791
|
-
if (frameType !== 0)
|
|
792
|
-
return; // Just to be sure
|
|
793
|
-
bitstream.skipBits(2);
|
|
794
|
-
const syncCode = bitstream.readBits(24);
|
|
795
|
-
if (syncCode !== 0x498342)
|
|
796
|
-
return;
|
|
797
|
-
if (profile >= 2)
|
|
798
|
-
bitstream.skipBits(1);
|
|
799
|
-
const colorSpaceID = {
|
|
800
|
-
rgb: 7,
|
|
801
|
-
bt709: 2,
|
|
802
|
-
bt470bg: 1,
|
|
803
|
-
smpte170m: 3,
|
|
804
|
-
}[trackData.info.decoderConfig.colorSpace.matrix];
|
|
805
|
-
// The bitstream position is now at the start of the color space bits.
|
|
806
|
-
// We can use the global writeBits function here as requested.
|
|
807
|
-
writeBits(chunk.data, bitstream.pos, bitstream.pos + 3, colorSpaceID);
|
|
808
|
-
}
|
|
809
|
-
/** Converts a read-only external chunk into an internal one for easier use. */
|
|
810
|
-
createInternalChunk(data, timestamp, duration, type, additions = null) {
|
|
811
|
-
const internalChunk = {
|
|
812
|
-
data,
|
|
813
|
-
type,
|
|
814
|
-
timestamp,
|
|
815
|
-
duration,
|
|
816
|
-
additions,
|
|
817
|
-
};
|
|
818
|
-
return internalChunk;
|
|
819
|
-
}
|
|
820
|
-
/** Writes a block containing media data to the file. */
|
|
821
|
-
writeBlock(trackData, chunk) {
|
|
822
|
-
// Due to the interlacing algorithm, this code will be run once we've seen one chunk from every media track.
|
|
823
|
-
if (!this.segment) {
|
|
824
|
-
this.createSegment();
|
|
825
|
-
}
|
|
826
|
-
const msTimestamp = Math.round(1000 * chunk.timestamp);
|
|
827
|
-
// We wanna only finalize this cluster (and begin a new one) if we know that each track will be able to
|
|
828
|
-
// start the new one with a key frame.
|
|
829
|
-
const keyFrameQueuedEverywhere = this.trackDatas.every((otherTrackData) => {
|
|
830
|
-
if (trackData === otherTrackData) {
|
|
831
|
-
return chunk.type === 'key';
|
|
832
|
-
}
|
|
833
|
-
const firstQueuedSample = otherTrackData.chunkQueue[0];
|
|
834
|
-
if (firstQueuedSample) {
|
|
835
|
-
return firstQueuedSample.type === 'key';
|
|
836
|
-
}
|
|
837
|
-
return otherTrackData.track.source._closed;
|
|
838
|
-
});
|
|
839
|
-
let shouldCreateNewCluster = false;
|
|
840
|
-
if (!this.currentCluster) {
|
|
841
|
-
shouldCreateNewCluster = true;
|
|
842
|
-
}
|
|
843
|
-
else {
|
|
844
|
-
assert(this.currentClusterStartMsTimestamp !== null);
|
|
845
|
-
assert(this.currentClusterMaxMsTimestamp !== null);
|
|
846
|
-
const relativeTimestamp = msTimestamp - this.currentClusterStartMsTimestamp;
|
|
847
|
-
shouldCreateNewCluster = (keyFrameQueuedEverywhere
|
|
848
|
-
// This check is required because that means there is already a block with this timestamp in the
|
|
849
|
-
// CURRENT chunk, meaning that starting the next cluster at the same timestamp is forbidden (since
|
|
850
|
-
// the already-written block would belong into it instead).
|
|
851
|
-
&& msTimestamp > this.currentClusterMaxMsTimestamp
|
|
852
|
-
&& relativeTimestamp >= 1000 * (this.format._options.minimumClusterDuration ?? 1))
|
|
853
|
-
// The cluster would exceed its maximum allowed length. This puts us in an unfortunate position and forces
|
|
854
|
-
// us to begin the next cluster with a delta frame. Although this is undesirable, it is not forbidden by the
|
|
855
|
-
// spec and is supported by players.
|
|
856
|
-
|| relativeTimestamp > MAX_CLUSTER_TIMESTAMP_MS;
|
|
857
|
-
}
|
|
858
|
-
if (shouldCreateNewCluster) {
|
|
859
|
-
this.createNewCluster(msTimestamp);
|
|
860
|
-
}
|
|
861
|
-
const relativeTimestamp = msTimestamp - this.currentClusterStartMsTimestamp;
|
|
862
|
-
if (relativeTimestamp < MIN_CLUSTER_TIMESTAMP_MS) {
|
|
863
|
-
// The block lies too far in the past, it's not representable within this cluster
|
|
864
|
-
return;
|
|
865
|
-
}
|
|
866
|
-
const prelude = new Uint8Array(4);
|
|
867
|
-
const view = new DataView(prelude.buffer);
|
|
868
|
-
// 0x80 to indicate it's the last byte of a multi-byte number
|
|
869
|
-
view.setUint8(0, 0x80 | trackData.track.id);
|
|
870
|
-
view.setInt16(1, relativeTimestamp, false);
|
|
871
|
-
const msDuration = Math.round(1000 * chunk.duration);
|
|
872
|
-
if (!chunk.additions) {
|
|
873
|
-
// No additions, we can write out a SimpleBlock
|
|
874
|
-
view.setUint8(3, Number(chunk.type === 'key') << 7); // Flags (keyframe flag only present for SimpleBlock)
|
|
875
|
-
const simpleBlock = { id: EBMLId.SimpleBlock, data: [
|
|
876
|
-
prelude,
|
|
877
|
-
chunk.data,
|
|
878
|
-
] };
|
|
879
|
-
this.ebmlWriter.writeEBML(simpleBlock);
|
|
880
|
-
}
|
|
881
|
-
else {
|
|
882
|
-
const blockGroup = { id: EBMLId.BlockGroup, data: [
|
|
883
|
-
{ id: EBMLId.Block, data: [
|
|
884
|
-
prelude,
|
|
885
|
-
chunk.data,
|
|
886
|
-
] },
|
|
887
|
-
chunk.type === 'delta'
|
|
888
|
-
? {
|
|
889
|
-
id: EBMLId.ReferenceBlock,
|
|
890
|
-
data: new EBMLSignedInt(trackData.lastWrittenMsTimestamp - msTimestamp),
|
|
891
|
-
}
|
|
892
|
-
: null,
|
|
893
|
-
chunk.additions
|
|
894
|
-
? { id: EBMLId.BlockAdditions, data: [
|
|
895
|
-
{ id: EBMLId.BlockMore, data: [
|
|
896
|
-
{ id: EBMLId.BlockAddID, data: 1 }, // Some players expect BlockAddID to come first
|
|
897
|
-
{ id: EBMLId.BlockAdditional, data: chunk.additions },
|
|
898
|
-
] },
|
|
899
|
-
] }
|
|
900
|
-
: null,
|
|
901
|
-
msDuration > 0 ? { id: EBMLId.BlockDuration, data: msDuration } : null,
|
|
902
|
-
] };
|
|
903
|
-
this.ebmlWriter.writeEBML(blockGroup);
|
|
904
|
-
}
|
|
905
|
-
this.duration = Math.max(this.duration, msTimestamp + msDuration);
|
|
906
|
-
trackData.lastWrittenMsTimestamp = msTimestamp;
|
|
907
|
-
if (!this.trackDatasInCurrentCluster.has(trackData)) {
|
|
908
|
-
this.trackDatasInCurrentCluster.set(trackData, {
|
|
909
|
-
firstMsTimestamp: msTimestamp,
|
|
910
|
-
});
|
|
911
|
-
}
|
|
912
|
-
this.currentClusterMaxMsTimestamp = Math.max(this.currentClusterMaxMsTimestamp, msTimestamp);
|
|
913
|
-
}
|
|
914
|
-
/** Creates a new Cluster element to contain media chunks. */
|
|
915
|
-
createNewCluster(msTimestamp) {
|
|
916
|
-
if (this.currentCluster) {
|
|
917
|
-
this.finalizeCurrentCluster();
|
|
918
|
-
}
|
|
919
|
-
if (this.format._options.onCluster) {
|
|
920
|
-
this.writer.startTrackingWrites();
|
|
921
|
-
}
|
|
922
|
-
this.currentCluster = {
|
|
923
|
-
id: EBMLId.Cluster,
|
|
924
|
-
size: this.format._options.appendOnly ? -1 : CLUSTER_SIZE_BYTES,
|
|
925
|
-
data: [
|
|
926
|
-
{ id: EBMLId.Timestamp, data: msTimestamp },
|
|
927
|
-
],
|
|
928
|
-
};
|
|
929
|
-
this.ebmlWriter.writeEBML(this.currentCluster);
|
|
930
|
-
this.currentClusterStartMsTimestamp = msTimestamp;
|
|
931
|
-
this.currentClusterMaxMsTimestamp = msTimestamp;
|
|
932
|
-
this.trackDatasInCurrentCluster.clear();
|
|
933
|
-
}
|
|
934
|
-
finalizeCurrentCluster() {
|
|
935
|
-
assert(this.currentCluster);
|
|
936
|
-
if (!this.format._options.appendOnly) {
|
|
937
|
-
const clusterSize = this.writer.getPos() - this.ebmlWriter.dataOffsets.get(this.currentCluster);
|
|
938
|
-
const endPos = this.writer.getPos();
|
|
939
|
-
// Write the size now that we know it
|
|
940
|
-
this.writer.seek(this.ebmlWriter.offsets.get(this.currentCluster) + 4);
|
|
941
|
-
this.ebmlWriter.writeVarInt(clusterSize, CLUSTER_SIZE_BYTES);
|
|
942
|
-
this.writer.seek(endPos);
|
|
943
|
-
}
|
|
944
|
-
if (this.format._options.onCluster) {
|
|
945
|
-
assert(this.currentClusterStartMsTimestamp !== null);
|
|
946
|
-
const { data, start } = this.writer.stopTrackingWrites();
|
|
947
|
-
this.format._options.onCluster(data, start, this.currentClusterStartMsTimestamp / 1000);
|
|
948
|
-
}
|
|
949
|
-
const clusterOffsetFromSegment = this.ebmlWriter.offsets.get(this.currentCluster) - this.segmentDataOffset;
|
|
950
|
-
// Group tracks by their first timestamp and create a CuePoint for each unique timestamp
|
|
951
|
-
const groupedByTimestamp = new Map();
|
|
952
|
-
for (const [trackData, { firstMsTimestamp }] of this.trackDatasInCurrentCluster) {
|
|
953
|
-
if (!groupedByTimestamp.has(firstMsTimestamp)) {
|
|
954
|
-
groupedByTimestamp.set(firstMsTimestamp, []);
|
|
955
|
-
}
|
|
956
|
-
groupedByTimestamp.get(firstMsTimestamp).push(trackData);
|
|
957
|
-
}
|
|
958
|
-
const groupedAndSortedByTimestamp = [...groupedByTimestamp.entries()].sort((a, b) => a[0] - b[0]);
|
|
959
|
-
// Add CuePoints to the Cues element for better seeking
|
|
960
|
-
for (const [msTimestamp, trackDatas] of groupedAndSortedByTimestamp) {
|
|
961
|
-
assert(this.cues);
|
|
962
|
-
this.cues.data.push({ id: EBMLId.CuePoint, data: [
|
|
963
|
-
{ id: EBMLId.CueTime, data: msTimestamp },
|
|
964
|
-
// Create CueTrackPositions for each track that starts at this timestamp
|
|
965
|
-
...trackDatas.map((trackData) => {
|
|
966
|
-
return { id: EBMLId.CueTrackPositions, data: [
|
|
967
|
-
{ id: EBMLId.CueTrack, data: trackData.track.id },
|
|
968
|
-
{ id: EBMLId.CueClusterPosition, data: clusterOffsetFromSegment },
|
|
969
|
-
] };
|
|
970
|
-
}),
|
|
971
|
-
] });
|
|
972
|
-
}
|
|
973
|
-
}
|
|
974
|
-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
975
|
-
async onTrackClose() {
|
|
976
|
-
const release = await this.mutex.acquire();
|
|
977
|
-
if (this.allTracksAreKnown()) {
|
|
978
|
-
this.allTracksKnown.resolve();
|
|
979
|
-
}
|
|
980
|
-
// Since a track is now closed, we may be able to write out chunks that were previously waiting
|
|
981
|
-
await this.interleaveChunks();
|
|
982
|
-
release();
|
|
983
|
-
}
|
|
984
|
-
/** Finalizes the file, making it ready for use. Must be called after all media chunks have been added. */
|
|
985
|
-
async finalize() {
|
|
986
|
-
const release = await this.mutex.acquire();
|
|
987
|
-
this.allTracksKnown.resolve();
|
|
988
|
-
if (!this.segment) {
|
|
989
|
-
this.createSegment();
|
|
990
|
-
}
|
|
991
|
-
// Flush any remaining queued chunks to the file
|
|
992
|
-
await this.interleaveChunks(true);
|
|
993
|
-
if (this.currentCluster) {
|
|
994
|
-
this.finalizeCurrentCluster();
|
|
995
|
-
}
|
|
996
|
-
assert(this.cues);
|
|
997
|
-
this.ebmlWriter.writeEBML(this.cues);
|
|
998
|
-
if (!this.format._options.appendOnly) {
|
|
999
|
-
const endPos = this.writer.getPos();
|
|
1000
|
-
// Write the Segment size
|
|
1001
|
-
const segmentSize = this.writer.getPos() - this.segmentDataOffset;
|
|
1002
|
-
this.writer.seek(this.ebmlWriter.offsets.get(this.segment) + 4);
|
|
1003
|
-
this.ebmlWriter.writeVarInt(segmentSize, SEGMENT_SIZE_BYTES);
|
|
1004
|
-
// Write the duration of the media to the Segment
|
|
1005
|
-
this.segmentDuration.data = new EBMLFloat64(this.duration);
|
|
1006
|
-
this.writer.seek(this.ebmlWriter.offsets.get(this.segmentDuration));
|
|
1007
|
-
this.ebmlWriter.writeEBML(this.segmentDuration);
|
|
1008
|
-
// Fill in SeekHead position data and write it again
|
|
1009
|
-
assert(this.seekHead);
|
|
1010
|
-
this.writer.seek(this.ebmlWriter.offsets.get(this.seekHead));
|
|
1011
|
-
this.maybeCreateSeekHead(true);
|
|
1012
|
-
this.ebmlWriter.writeEBML(this.seekHead);
|
|
1013
|
-
this.writer.seek(endPos);
|
|
1014
|
-
}
|
|
1015
|
-
release();
|
|
1016
|
-
}
|
|
1017
|
-
}
|