@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
package/src/encode.ts
DELETED
|
@@ -1,739 +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 {
|
|
10
|
-
AUDIO_CODECS,
|
|
11
|
-
AudioCodec,
|
|
12
|
-
buildAudioCodecString,
|
|
13
|
-
buildVideoCodecString,
|
|
14
|
-
getAudioEncoderConfigExtension,
|
|
15
|
-
getVideoEncoderConfigExtension,
|
|
16
|
-
inferCodecFromCodecString,
|
|
17
|
-
MediaCodec,
|
|
18
|
-
PCM_AUDIO_CODECS,
|
|
19
|
-
SUBTITLE_CODECS,
|
|
20
|
-
SubtitleCodec,
|
|
21
|
-
VIDEO_CODECS,
|
|
22
|
-
VideoCodec,
|
|
23
|
-
} from './codec';
|
|
24
|
-
import { customAudioEncoders, customVideoEncoders } from './custom-coder';
|
|
25
|
-
import { EncodedPacket } from './packet';
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Configuration object that controls video encoding. Can be used to set codec, quality, and more.
|
|
29
|
-
* @group Encoding
|
|
30
|
-
* @public
|
|
31
|
-
*/
|
|
32
|
-
export type VideoEncodingConfig = {
|
|
33
|
-
/** The video codec that should be used for encoding the video samples (frames). */
|
|
34
|
-
codec: VideoCodec;
|
|
35
|
-
/**
|
|
36
|
-
* The target bitrate for the encoded video, in bits per second. Alternatively, a subjective {@link Quality} can
|
|
37
|
-
* be provided.
|
|
38
|
-
*/
|
|
39
|
-
bitrate: number | Quality;
|
|
40
|
-
/**
|
|
41
|
-
* The interval, in seconds, of how often frames are encoded as a key frame. The default is 5 seconds. Frequent key
|
|
42
|
-
* frames improve seeking behavior but increase file size. When using multiple video tracks, you should give them
|
|
43
|
-
* all the same key frame interval.
|
|
44
|
-
*/
|
|
45
|
-
keyFrameInterval?: number;
|
|
46
|
-
/**
|
|
47
|
-
* Video frames may change size over time. This field controls the behavior in case this happens.
|
|
48
|
-
*
|
|
49
|
-
* - `'deny'` (default) will throw an error, requiring all frames to have the exact same dimensions.
|
|
50
|
-
* - `'passThrough'` will allow the change and directly pass the frame to the encoder.
|
|
51
|
-
* - `'fill'` will stretch the image to fill the entire original box, potentially altering aspect ratio.
|
|
52
|
-
* - `'contain'` will contain the entire image within the original box while preserving aspect ratio. This may lead
|
|
53
|
-
* to letterboxing.
|
|
54
|
-
* - `'cover'` will scale the image until the entire original box is filled, while preserving aspect ratio.
|
|
55
|
-
*
|
|
56
|
-
* The "original box" refers to the dimensions of the first encoded frame.
|
|
57
|
-
*/
|
|
58
|
-
sizeChangeBehavior?: 'deny' | 'passThrough' | 'fill' | 'contain' | 'cover';
|
|
59
|
-
|
|
60
|
-
/** Called for each successfully encoded packet. Both the packet and the encoding metadata are passed. */
|
|
61
|
-
onEncodedPacket?: (packet: EncodedPacket, meta: EncodedVideoChunkMetadata | undefined) => unknown;
|
|
62
|
-
/**
|
|
63
|
-
* Called when the internal [encoder config](https://www.w3.org/TR/webcodecs/#video-encoder-config), as used by the
|
|
64
|
-
* WebCodecs API, is created.
|
|
65
|
-
*/
|
|
66
|
-
onEncoderConfig?: (config: VideoEncoderConfig) => unknown;
|
|
67
|
-
} & VideoEncodingAdditionalOptions;
|
|
68
|
-
|
|
69
|
-
export const validateVideoEncodingConfig = (config: VideoEncodingConfig) => {
|
|
70
|
-
if (!config || typeof config !== 'object') {
|
|
71
|
-
throw new TypeError('Encoding config must be an object.');
|
|
72
|
-
}
|
|
73
|
-
if (!VIDEO_CODECS.includes(config.codec)) {
|
|
74
|
-
throw new TypeError(`Invalid video codec '${config.codec}'. Must be one of: ${VIDEO_CODECS.join(', ')}.`);
|
|
75
|
-
}
|
|
76
|
-
if (!(config.bitrate instanceof Quality) && (!Number.isInteger(config.bitrate) || config.bitrate <= 0)) {
|
|
77
|
-
throw new TypeError('config.bitrate must be a positive integer or a quality.');
|
|
78
|
-
}
|
|
79
|
-
if (
|
|
80
|
-
config.keyFrameInterval !== undefined
|
|
81
|
-
&& (!Number.isFinite(config.keyFrameInterval) || config.keyFrameInterval < 0)
|
|
82
|
-
) {
|
|
83
|
-
throw new TypeError('config.keyFrameInterval, when provided, must be a non-negative number.');
|
|
84
|
-
}
|
|
85
|
-
if (
|
|
86
|
-
config.sizeChangeBehavior !== undefined
|
|
87
|
-
&& !['deny', 'passThrough', 'fill', 'contain', 'cover'].includes(config.sizeChangeBehavior)
|
|
88
|
-
) {
|
|
89
|
-
throw new TypeError(
|
|
90
|
-
'config.sizeChangeBehavior, when provided, must be \'deny\', \'passThrough\', \'fill\', \'contain\''
|
|
91
|
-
+ ' or \'cover\'.',
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
if (config.onEncodedPacket !== undefined && typeof config.onEncodedPacket !== 'function') {
|
|
95
|
-
throw new TypeError('config.onEncodedChunk, when provided, must be a function.');
|
|
96
|
-
}
|
|
97
|
-
if (config.onEncoderConfig !== undefined && typeof config.onEncoderConfig !== 'function') {
|
|
98
|
-
throw new TypeError('config.onEncoderConfig, when provided, must be a function.');
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
validateVideoEncodingAdditionalOptions(config.codec, config);
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Additional options that control audio encoding.
|
|
106
|
-
* @group Encoding
|
|
107
|
-
* @public
|
|
108
|
-
*/
|
|
109
|
-
export type VideoEncodingAdditionalOptions = {
|
|
110
|
-
/**
|
|
111
|
-
* What to do with alpha data contained in the video samples.
|
|
112
|
-
*
|
|
113
|
-
* - `'discard'` (default): Only the samples' color data is kept; the video is opaque.
|
|
114
|
-
* - `'keep'`: The samples' alpha data is also encoded as side data. Make sure to pair this mode with a container
|
|
115
|
-
* format that supports transparency (such as WebM or Matroska).
|
|
116
|
-
*/
|
|
117
|
-
alpha?: 'discard' | 'keep';
|
|
118
|
-
/** Configures the bitrate mode; defaults to `'variable'`. */
|
|
119
|
-
bitrateMode?: 'constant' | 'variable';
|
|
120
|
-
/**
|
|
121
|
-
* The latency mode used by the encoder; controls the performance-quality tradeoff.
|
|
122
|
-
*
|
|
123
|
-
* - `'quality'` (default): The encoder prioritizes quality over latency, and no frames can be dropped.
|
|
124
|
-
* - `'realtime'`: The encoder prioritizes low latency over quality, and may drop frames if the encoder becomes
|
|
125
|
-
* overloaded to keep up with real-time requirements.
|
|
126
|
-
*/
|
|
127
|
-
latencyMode?: 'quality' | 'realtime';
|
|
128
|
-
/**
|
|
129
|
-
* The full codec string as specified in the WebCodecs Codec Registry. This string must match the codec
|
|
130
|
-
* specified in `codec`. When not set, a fitting codec string will be constructed automatically by the library.
|
|
131
|
-
*/
|
|
132
|
-
fullCodecString?: string;
|
|
133
|
-
/**
|
|
134
|
-
* A hint that configures the hardware acceleration method of this codec. This is best left on `'no-preference'`,
|
|
135
|
-
* the default.
|
|
136
|
-
*/
|
|
137
|
-
hardwareAcceleration?: 'no-preference' | 'prefer-hardware' | 'prefer-software';
|
|
138
|
-
/**
|
|
139
|
-
* An encoding scalability mode identifier as defined by
|
|
140
|
-
* [WebRTC-SVC](https://w3c.github.io/webrtc-svc/#scalabilitymodes*).
|
|
141
|
-
*/
|
|
142
|
-
scalabilityMode?: string;
|
|
143
|
-
/**
|
|
144
|
-
* An encoding video content hint as defined by
|
|
145
|
-
* [mst-content-hint](https://w3c.github.io/mst-content-hint/#video-content-hints).
|
|
146
|
-
*/
|
|
147
|
-
contentHint?: string;
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
export const validateVideoEncodingAdditionalOptions = (codec: VideoCodec, options: VideoEncodingAdditionalOptions) => {
|
|
151
|
-
if (!options || typeof options !== 'object') {
|
|
152
|
-
throw new TypeError('Encoding options must be an object.');
|
|
153
|
-
}
|
|
154
|
-
if (options.alpha !== undefined && !['discard', 'keep'].includes(options.alpha)) {
|
|
155
|
-
throw new TypeError('options.alpha, when provided, must be \'discard\' or \'keep\'.');
|
|
156
|
-
}
|
|
157
|
-
if (options.bitrateMode !== undefined && !['constant', 'variable'].includes(options.bitrateMode)) {
|
|
158
|
-
throw new TypeError('bitrateMode, when provided, must be \'constant\' or \'variable\'.');
|
|
159
|
-
}
|
|
160
|
-
if (options.latencyMode !== undefined && !['quality', 'realtime'].includes(options.latencyMode)) {
|
|
161
|
-
throw new TypeError('latencyMode, when provided, must be \'quality\' or \'realtime\'.');
|
|
162
|
-
}
|
|
163
|
-
if (options.fullCodecString !== undefined && typeof options.fullCodecString !== 'string') {
|
|
164
|
-
throw new TypeError('fullCodecString, when provided, must be a string.');
|
|
165
|
-
}
|
|
166
|
-
if (options.fullCodecString !== undefined && inferCodecFromCodecString(options.fullCodecString) !== codec) {
|
|
167
|
-
throw new TypeError(
|
|
168
|
-
`fullCodecString, when provided, must be a string that matches the specified codec (${codec}).`,
|
|
169
|
-
);
|
|
170
|
-
}
|
|
171
|
-
if (
|
|
172
|
-
options.hardwareAcceleration !== undefined
|
|
173
|
-
&& !['no-preference', 'prefer-hardware', 'prefer-software'].includes(options.hardwareAcceleration)
|
|
174
|
-
) {
|
|
175
|
-
throw new TypeError(
|
|
176
|
-
'hardwareAcceleration, when provided, must be \'no-preference\', \'prefer-hardware\' or'
|
|
177
|
-
+ ' \'prefer-software\'.',
|
|
178
|
-
);
|
|
179
|
-
}
|
|
180
|
-
if (options.scalabilityMode !== undefined && typeof options.scalabilityMode !== 'string') {
|
|
181
|
-
throw new TypeError('scalabilityMode, when provided, must be a string.');
|
|
182
|
-
}
|
|
183
|
-
if (options.contentHint !== undefined && typeof options.contentHint !== 'string') {
|
|
184
|
-
throw new TypeError('contentHint, when provided, must be a string.');
|
|
185
|
-
}
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
export const buildVideoEncoderConfig = (options: {
|
|
189
|
-
codec: VideoCodec;
|
|
190
|
-
width: number;
|
|
191
|
-
height: number;
|
|
192
|
-
bitrate: number | Quality;
|
|
193
|
-
framerate: number | undefined;
|
|
194
|
-
} & VideoEncodingAdditionalOptions): VideoEncoderConfig => {
|
|
195
|
-
const resolvedBitrate = options.bitrate instanceof Quality
|
|
196
|
-
? options.bitrate._toVideoBitrate(options.codec, options.width, options.height)
|
|
197
|
-
: options.bitrate;
|
|
198
|
-
|
|
199
|
-
return {
|
|
200
|
-
codec: options.fullCodecString ?? buildVideoCodecString(
|
|
201
|
-
options.codec,
|
|
202
|
-
options.width,
|
|
203
|
-
options.height,
|
|
204
|
-
resolvedBitrate,
|
|
205
|
-
),
|
|
206
|
-
width: options.width,
|
|
207
|
-
height: options.height,
|
|
208
|
-
bitrate: resolvedBitrate,
|
|
209
|
-
bitrateMode: options.bitrateMode,
|
|
210
|
-
alpha: options.alpha ?? 'discard',
|
|
211
|
-
framerate: options.framerate,
|
|
212
|
-
latencyMode: options.latencyMode,
|
|
213
|
-
hardwareAcceleration: options.hardwareAcceleration,
|
|
214
|
-
scalabilityMode: options.scalabilityMode,
|
|
215
|
-
contentHint: options.contentHint,
|
|
216
|
-
...getVideoEncoderConfigExtension(options.codec),
|
|
217
|
-
};
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Configuration object that controls audio encoding. Can be used to set codec, quality, and more.
|
|
222
|
-
* @group Encoding
|
|
223
|
-
* @public
|
|
224
|
-
*/
|
|
225
|
-
export type AudioEncodingConfig = {
|
|
226
|
-
/** The audio codec that should be used for encoding the audio samples. */
|
|
227
|
-
codec: AudioCodec;
|
|
228
|
-
/**
|
|
229
|
-
* The target bitrate for the encoded audio, in bits per second. Alternatively, a subjective {@link Quality} can
|
|
230
|
-
* be provided. Required for compressed audio codecs, unused for PCM codecs.
|
|
231
|
-
*/
|
|
232
|
-
bitrate?: number | Quality;
|
|
233
|
-
|
|
234
|
-
/** Called for each successfully encoded packet. Both the packet and the encoding metadata are passed. */
|
|
235
|
-
onEncodedPacket?: (packet: EncodedPacket, meta: EncodedAudioChunkMetadata | undefined) => unknown;
|
|
236
|
-
/**
|
|
237
|
-
* Called when the internal [encoder config](https://www.w3.org/TR/webcodecs/#audio-encoder-config), as used by the
|
|
238
|
-
* WebCodecs API, is created.
|
|
239
|
-
*/
|
|
240
|
-
onEncoderConfig?: (config: AudioEncoderConfig) => unknown;
|
|
241
|
-
} & AudioEncodingAdditionalOptions;
|
|
242
|
-
|
|
243
|
-
export const validateAudioEncodingConfig = (config: AudioEncodingConfig) => {
|
|
244
|
-
if (!config || typeof config !== 'object') {
|
|
245
|
-
throw new TypeError('Encoding config must be an object.');
|
|
246
|
-
}
|
|
247
|
-
if (!AUDIO_CODECS.includes(config.codec)) {
|
|
248
|
-
throw new TypeError(`Invalid audio codec '${config.codec}'. Must be one of: ${AUDIO_CODECS.join(', ')}.`);
|
|
249
|
-
}
|
|
250
|
-
if (
|
|
251
|
-
config.bitrate === undefined
|
|
252
|
-
&& (!(PCM_AUDIO_CODECS as readonly string[]).includes(config.codec) || config.codec === 'flac')
|
|
253
|
-
) {
|
|
254
|
-
throw new TypeError('config.bitrate must be provided for compressed audio codecs.');
|
|
255
|
-
}
|
|
256
|
-
if (
|
|
257
|
-
config.bitrate !== undefined
|
|
258
|
-
&& !(config.bitrate instanceof Quality)
|
|
259
|
-
&& (!Number.isInteger(config.bitrate) || config.bitrate <= 0)
|
|
260
|
-
) {
|
|
261
|
-
throw new TypeError('config.bitrate, when provided, must be a positive integer or a quality.');
|
|
262
|
-
}
|
|
263
|
-
if (config.onEncodedPacket !== undefined && typeof config.onEncodedPacket !== 'function') {
|
|
264
|
-
throw new TypeError('config.onEncodedChunk, when provided, must be a function.');
|
|
265
|
-
}
|
|
266
|
-
if (config.onEncoderConfig !== undefined && typeof config.onEncoderConfig !== 'function') {
|
|
267
|
-
throw new TypeError('config.onEncoderConfig, when provided, must be a function.');
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
validateAudioEncodingAdditionalOptions(config.codec, config);
|
|
271
|
-
};
|
|
272
|
-
|
|
273
|
-
/**
|
|
274
|
-
* Additional options that control audio encoding.
|
|
275
|
-
* @group Encoding
|
|
276
|
-
* @public
|
|
277
|
-
*/
|
|
278
|
-
export type AudioEncodingAdditionalOptions = {
|
|
279
|
-
/** Configures the bitrate mode. */
|
|
280
|
-
bitrateMode?: 'constant' | 'variable';
|
|
281
|
-
/**
|
|
282
|
-
* The full codec string as specified in the WebCodecs Codec Registry. This string must match the codec
|
|
283
|
-
* specified in `codec`. When not set, a fitting codec string will be constructed automatically by the library.
|
|
284
|
-
*/
|
|
285
|
-
fullCodecString?: string;
|
|
286
|
-
};
|
|
287
|
-
|
|
288
|
-
export const validateAudioEncodingAdditionalOptions = (codec: AudioCodec, options: AudioEncodingAdditionalOptions) => {
|
|
289
|
-
if (!options || typeof options !== 'object') {
|
|
290
|
-
throw new TypeError('Encoding options must be an object.');
|
|
291
|
-
}
|
|
292
|
-
if (options.bitrateMode !== undefined && !['constant', 'variable'].includes(options.bitrateMode)) {
|
|
293
|
-
throw new TypeError('bitrateMode, when provided, must be \'constant\' or \'variable\'.');
|
|
294
|
-
}
|
|
295
|
-
if (options.fullCodecString !== undefined && typeof options.fullCodecString !== 'string') {
|
|
296
|
-
throw new TypeError('fullCodecString, when provided, must be a string.');
|
|
297
|
-
}
|
|
298
|
-
if (options.fullCodecString !== undefined && inferCodecFromCodecString(options.fullCodecString) !== codec) {
|
|
299
|
-
throw new TypeError(
|
|
300
|
-
`fullCodecString, when provided, must be a string that matches the specified codec (${codec}).`,
|
|
301
|
-
);
|
|
302
|
-
}
|
|
303
|
-
};
|
|
304
|
-
|
|
305
|
-
export const buildAudioEncoderConfig = (options: {
|
|
306
|
-
codec: AudioCodec;
|
|
307
|
-
numberOfChannels: number;
|
|
308
|
-
sampleRate: number;
|
|
309
|
-
bitrate?: number | Quality;
|
|
310
|
-
} & AudioEncodingAdditionalOptions): AudioEncoderConfig => {
|
|
311
|
-
const resolvedBitrate = options.bitrate instanceof Quality
|
|
312
|
-
? options.bitrate._toAudioBitrate(options.codec)
|
|
313
|
-
: options.bitrate;
|
|
314
|
-
|
|
315
|
-
return {
|
|
316
|
-
codec: options.fullCodecString ?? buildAudioCodecString(
|
|
317
|
-
options.codec,
|
|
318
|
-
options.numberOfChannels,
|
|
319
|
-
options.sampleRate,
|
|
320
|
-
),
|
|
321
|
-
numberOfChannels: options.numberOfChannels,
|
|
322
|
-
sampleRate: options.sampleRate,
|
|
323
|
-
bitrate: resolvedBitrate,
|
|
324
|
-
bitrateMode: options.bitrateMode,
|
|
325
|
-
...getAudioEncoderConfigExtension(options.codec),
|
|
326
|
-
};
|
|
327
|
-
};
|
|
328
|
-
|
|
329
|
-
/**
|
|
330
|
-
* Represents a subjective media quality level.
|
|
331
|
-
* @group Encoding
|
|
332
|
-
* @public
|
|
333
|
-
*/
|
|
334
|
-
export class Quality {
|
|
335
|
-
/** @internal */
|
|
336
|
-
_factor: number;
|
|
337
|
-
|
|
338
|
-
/** @internal */
|
|
339
|
-
constructor(factor: number) {
|
|
340
|
-
this._factor = factor;
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
/** @internal */
|
|
344
|
-
_toVideoBitrate(codec: VideoCodec, width: number, height: number) {
|
|
345
|
-
const pixels = width * height;
|
|
346
|
-
|
|
347
|
-
const codecEfficiencyFactors = {
|
|
348
|
-
avc: 1.0, // H.264/AVC (baseline)
|
|
349
|
-
hevc: 0.6, // H.265/HEVC (~40% more efficient than AVC)
|
|
350
|
-
vp9: 0.6, // Similar to HEVC
|
|
351
|
-
av1: 0.4, // ~60% more efficient than AVC
|
|
352
|
-
vp8: 1.2, // Slightly less efficient than AVC
|
|
353
|
-
};
|
|
354
|
-
|
|
355
|
-
const referencePixels = 1920 * 1080;
|
|
356
|
-
const referenceBitrate = 3000000;
|
|
357
|
-
const scaleFactor = Math.pow(pixels / referencePixels, 0.95); // Slight non-linear scaling
|
|
358
|
-
const baseBitrate = referenceBitrate * scaleFactor;
|
|
359
|
-
|
|
360
|
-
const codecAdjustedBitrate = baseBitrate * codecEfficiencyFactors[codec];
|
|
361
|
-
const finalBitrate = codecAdjustedBitrate * this._factor;
|
|
362
|
-
|
|
363
|
-
return Math.ceil(finalBitrate / 1000) * 1000;
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
/** @internal */
|
|
367
|
-
_toAudioBitrate(codec: AudioCodec) {
|
|
368
|
-
if ((PCM_AUDIO_CODECS as readonly string[]).includes(codec) || codec === 'flac') {
|
|
369
|
-
return undefined;
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
const baseRates = {
|
|
373
|
-
aac: 128000, // 128kbps base for AAC
|
|
374
|
-
opus: 64000, // 64kbps base for Opus
|
|
375
|
-
mp3: 160000, // 160kbps base for MP3
|
|
376
|
-
vorbis: 64000, // 64kbps base for Vorbis
|
|
377
|
-
};
|
|
378
|
-
|
|
379
|
-
const baseBitrate = baseRates[codec as keyof typeof baseRates];
|
|
380
|
-
if (!baseBitrate) {
|
|
381
|
-
throw new Error(`Unhandled codec: ${codec}`);
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
let finalBitrate = baseBitrate * this._factor;
|
|
385
|
-
|
|
386
|
-
if (codec === 'aac') {
|
|
387
|
-
// AAC only works with specific bitrates, let's find the closest
|
|
388
|
-
const validRates = [96000, 128000, 160000, 192000];
|
|
389
|
-
finalBitrate = validRates.reduce((prev, curr) =>
|
|
390
|
-
Math.abs(curr - finalBitrate) < Math.abs(prev - finalBitrate) ? curr : prev,
|
|
391
|
-
);
|
|
392
|
-
} else if (codec === 'opus' || codec === 'vorbis') {
|
|
393
|
-
finalBitrate = Math.max(6000, finalBitrate);
|
|
394
|
-
} else if (codec === 'mp3') {
|
|
395
|
-
const validRates = [
|
|
396
|
-
8000, 16000, 24000, 32000, 40000, 48000, 64000, 80000,
|
|
397
|
-
96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000,
|
|
398
|
-
];
|
|
399
|
-
finalBitrate = validRates.reduce((prev, curr) =>
|
|
400
|
-
Math.abs(curr - finalBitrate) < Math.abs(prev - finalBitrate) ? curr : prev,
|
|
401
|
-
);
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
return Math.round(finalBitrate / 1000) * 1000;
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
/**
|
|
409
|
-
* Represents a very low media quality.
|
|
410
|
-
* @group Encoding
|
|
411
|
-
* @public
|
|
412
|
-
*/
|
|
413
|
-
export const QUALITY_VERY_LOW = /* #__PURE__ */ new Quality(0.3);
|
|
414
|
-
/**
|
|
415
|
-
* Represents a low media quality.
|
|
416
|
-
* @group Encoding
|
|
417
|
-
* @public
|
|
418
|
-
*/
|
|
419
|
-
export const QUALITY_LOW = /* #__PURE__ */ new Quality(0.6);
|
|
420
|
-
/**
|
|
421
|
-
* Represents a medium media quality.
|
|
422
|
-
* @group Encoding
|
|
423
|
-
* @public
|
|
424
|
-
*/
|
|
425
|
-
export const QUALITY_MEDIUM = /* #__PURE__ */ new Quality(1);
|
|
426
|
-
/**
|
|
427
|
-
* Represents a high media quality.
|
|
428
|
-
* @group Encoding
|
|
429
|
-
* @public
|
|
430
|
-
*/
|
|
431
|
-
export const QUALITY_HIGH = /* #__PURE__ */ new Quality(2);
|
|
432
|
-
/**
|
|
433
|
-
* Represents a very high media quality.
|
|
434
|
-
* @group Encoding
|
|
435
|
-
* @public
|
|
436
|
-
*/
|
|
437
|
-
export const QUALITY_VERY_HIGH = /* #__PURE__ */ new Quality(4);
|
|
438
|
-
|
|
439
|
-
/**
|
|
440
|
-
* Checks if the browser is able to encode the given codec.
|
|
441
|
-
* @group Encoding
|
|
442
|
-
* @public
|
|
443
|
-
*/
|
|
444
|
-
export const canEncode = (codec: MediaCodec) => {
|
|
445
|
-
if ((VIDEO_CODECS as readonly string[]).includes(codec)) {
|
|
446
|
-
return canEncodeVideo(codec as VideoCodec);
|
|
447
|
-
} else if ((AUDIO_CODECS as readonly string[]).includes(codec)) {
|
|
448
|
-
return canEncodeAudio(codec as AudioCodec);
|
|
449
|
-
} else if ((SUBTITLE_CODECS as readonly string[]).includes(codec)) {
|
|
450
|
-
return canEncodeSubtitles(codec as SubtitleCodec);
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
throw new TypeError(`Unknown codec '${codec}'.`);
|
|
454
|
-
};
|
|
455
|
-
|
|
456
|
-
/**
|
|
457
|
-
* Checks if the browser is able to encode the given video codec with the given parameters.
|
|
458
|
-
* @group Encoding
|
|
459
|
-
* @public
|
|
460
|
-
*/
|
|
461
|
-
export const canEncodeVideo = async (
|
|
462
|
-
codec: VideoCodec,
|
|
463
|
-
options: {
|
|
464
|
-
width?: number;
|
|
465
|
-
height?: number;
|
|
466
|
-
bitrate?: number | Quality;
|
|
467
|
-
} & VideoEncodingAdditionalOptions = {},
|
|
468
|
-
) => {
|
|
469
|
-
const {
|
|
470
|
-
width = 1280,
|
|
471
|
-
height = 720,
|
|
472
|
-
bitrate = 1e6,
|
|
473
|
-
...restOptions
|
|
474
|
-
} = options;
|
|
475
|
-
|
|
476
|
-
if (!VIDEO_CODECS.includes(codec)) {
|
|
477
|
-
return false;
|
|
478
|
-
}
|
|
479
|
-
if (!Number.isInteger(width) || width <= 0) {
|
|
480
|
-
throw new TypeError('width must be a positive integer.');
|
|
481
|
-
}
|
|
482
|
-
if (!Number.isInteger(height) || height <= 0) {
|
|
483
|
-
throw new TypeError('height must be a positive integer.');
|
|
484
|
-
}
|
|
485
|
-
if (!(bitrate instanceof Quality) && (!Number.isInteger(bitrate) || bitrate <= 0)) {
|
|
486
|
-
throw new TypeError('bitrate must be a positive integer or a quality.');
|
|
487
|
-
}
|
|
488
|
-
validateVideoEncodingAdditionalOptions(codec, restOptions);
|
|
489
|
-
|
|
490
|
-
let encoderConfig: VideoEncoderConfig | null = null;
|
|
491
|
-
|
|
492
|
-
if (customVideoEncoders.length > 0) {
|
|
493
|
-
encoderConfig ??= buildVideoEncoderConfig({
|
|
494
|
-
codec,
|
|
495
|
-
width,
|
|
496
|
-
height,
|
|
497
|
-
bitrate,
|
|
498
|
-
framerate: undefined,
|
|
499
|
-
...restOptions,
|
|
500
|
-
});
|
|
501
|
-
|
|
502
|
-
if (customVideoEncoders.some(x => x.supports(codec, encoderConfig!))) {
|
|
503
|
-
// There's a custom encoder
|
|
504
|
-
return true;
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
if (typeof VideoEncoder === 'undefined') {
|
|
509
|
-
return false;
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
const hasOddDimension = width % 2 === 1 || height % 2 === 1;
|
|
513
|
-
if (
|
|
514
|
-
hasOddDimension
|
|
515
|
-
&& (codec === 'avc' || codec === 'hevc')
|
|
516
|
-
) {
|
|
517
|
-
// Disallow odd dimensions for certain codecs
|
|
518
|
-
return false;
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
encoderConfig ??= buildVideoEncoderConfig({
|
|
522
|
-
codec,
|
|
523
|
-
width,
|
|
524
|
-
height,
|
|
525
|
-
bitrate,
|
|
526
|
-
framerate: undefined,
|
|
527
|
-
...restOptions,
|
|
528
|
-
alpha: 'discard', // Since we handle alpha ourselves
|
|
529
|
-
});
|
|
530
|
-
|
|
531
|
-
const support = await VideoEncoder.isConfigSupported(encoderConfig);
|
|
532
|
-
return support.supported === true;
|
|
533
|
-
};
|
|
534
|
-
|
|
535
|
-
/**
|
|
536
|
-
* Checks if the browser is able to encode the given audio codec with the given parameters.
|
|
537
|
-
* @group Encoding
|
|
538
|
-
* @public
|
|
539
|
-
*/
|
|
540
|
-
export const canEncodeAudio = async (
|
|
541
|
-
codec: AudioCodec,
|
|
542
|
-
options: {
|
|
543
|
-
numberOfChannels?: number;
|
|
544
|
-
sampleRate?: number;
|
|
545
|
-
bitrate?: number | Quality;
|
|
546
|
-
} & AudioEncodingAdditionalOptions = {},
|
|
547
|
-
) => {
|
|
548
|
-
const {
|
|
549
|
-
numberOfChannels = 2,
|
|
550
|
-
sampleRate = 48000,
|
|
551
|
-
bitrate = 128e3,
|
|
552
|
-
...restOptions
|
|
553
|
-
} = options;
|
|
554
|
-
|
|
555
|
-
if (!AUDIO_CODECS.includes(codec)) {
|
|
556
|
-
return false;
|
|
557
|
-
}
|
|
558
|
-
if (!Number.isInteger(numberOfChannels) || numberOfChannels <= 0) {
|
|
559
|
-
throw new TypeError('numberOfChannels must be a positive integer.');
|
|
560
|
-
}
|
|
561
|
-
if (!Number.isInteger(sampleRate) || sampleRate <= 0) {
|
|
562
|
-
throw new TypeError('sampleRate must be a positive integer.');
|
|
563
|
-
}
|
|
564
|
-
if (!(bitrate instanceof Quality) && (!Number.isInteger(bitrate) || bitrate <= 0)) {
|
|
565
|
-
throw new TypeError('bitrate must be a positive integer.');
|
|
566
|
-
}
|
|
567
|
-
validateAudioEncodingAdditionalOptions(codec, restOptions);
|
|
568
|
-
|
|
569
|
-
let encoderConfig: AudioEncoderConfig | null = null;
|
|
570
|
-
|
|
571
|
-
if (customAudioEncoders.length > 0) {
|
|
572
|
-
encoderConfig ??= buildAudioEncoderConfig({
|
|
573
|
-
codec,
|
|
574
|
-
numberOfChannels,
|
|
575
|
-
sampleRate,
|
|
576
|
-
bitrate,
|
|
577
|
-
...restOptions,
|
|
578
|
-
});
|
|
579
|
-
|
|
580
|
-
if (customAudioEncoders.some(x => x.supports(codec, encoderConfig!))) {
|
|
581
|
-
// There's a custom encoder
|
|
582
|
-
return true;
|
|
583
|
-
}
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
if ((PCM_AUDIO_CODECS as readonly string[]).includes(codec)) {
|
|
587
|
-
return true; // Because we encode these ourselves
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
if (typeof AudioEncoder === 'undefined') {
|
|
591
|
-
return false;
|
|
592
|
-
}
|
|
593
|
-
|
|
594
|
-
encoderConfig ??= buildAudioEncoderConfig({
|
|
595
|
-
codec,
|
|
596
|
-
numberOfChannels,
|
|
597
|
-
sampleRate,
|
|
598
|
-
bitrate,
|
|
599
|
-
...restOptions,
|
|
600
|
-
});
|
|
601
|
-
|
|
602
|
-
const support = await AudioEncoder.isConfigSupported(encoderConfig);
|
|
603
|
-
return support.supported === true;
|
|
604
|
-
};
|
|
605
|
-
|
|
606
|
-
/**
|
|
607
|
-
* Checks if the browser is able to encode the given subtitle codec.
|
|
608
|
-
* @group Encoding
|
|
609
|
-
* @public
|
|
610
|
-
*/
|
|
611
|
-
export const canEncodeSubtitles = async (codec: SubtitleCodec) => {
|
|
612
|
-
if (!SUBTITLE_CODECS.includes(codec)) {
|
|
613
|
-
return false;
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
return true;
|
|
617
|
-
};
|
|
618
|
-
|
|
619
|
-
/**
|
|
620
|
-
* Returns the list of all media codecs that can be encoded by the browser.
|
|
621
|
-
* @group Encoding
|
|
622
|
-
* @public
|
|
623
|
-
*/
|
|
624
|
-
export const getEncodableCodecs = async (): Promise<MediaCodec[]> => {
|
|
625
|
-
const [videoCodecs, audioCodecs, subtitleCodecs] = await Promise.all([
|
|
626
|
-
getEncodableVideoCodecs(),
|
|
627
|
-
getEncodableAudioCodecs(),
|
|
628
|
-
getEncodableSubtitleCodecs(),
|
|
629
|
-
]);
|
|
630
|
-
|
|
631
|
-
return [...videoCodecs, ...audioCodecs, ...subtitleCodecs];
|
|
632
|
-
};
|
|
633
|
-
|
|
634
|
-
/**
|
|
635
|
-
* Returns the list of all video codecs that can be encoded by the browser.
|
|
636
|
-
* @group Encoding
|
|
637
|
-
* @public
|
|
638
|
-
*/
|
|
639
|
-
export const getEncodableVideoCodecs = async (
|
|
640
|
-
checkedCodecs: VideoCodec[] = VIDEO_CODECS as unknown as VideoCodec[],
|
|
641
|
-
options?: {
|
|
642
|
-
width?: number;
|
|
643
|
-
height?: number;
|
|
644
|
-
bitrate?: number | Quality;
|
|
645
|
-
},
|
|
646
|
-
): Promise<VideoCodec[]> => {
|
|
647
|
-
const bools = await Promise.all(checkedCodecs.map(codec => canEncodeVideo(codec, options)));
|
|
648
|
-
return checkedCodecs.filter((_, i) => bools[i]);
|
|
649
|
-
};
|
|
650
|
-
|
|
651
|
-
/**
|
|
652
|
-
* Returns the list of all audio codecs that can be encoded by the browser.
|
|
653
|
-
* @group Encoding
|
|
654
|
-
* @public
|
|
655
|
-
*/
|
|
656
|
-
export const getEncodableAudioCodecs = async (
|
|
657
|
-
checkedCodecs: AudioCodec[] = AUDIO_CODECS as unknown as AudioCodec[],
|
|
658
|
-
options?: {
|
|
659
|
-
numberOfChannels?: number;
|
|
660
|
-
sampleRate?: number;
|
|
661
|
-
bitrate?: number | Quality;
|
|
662
|
-
},
|
|
663
|
-
): Promise<AudioCodec[]> => {
|
|
664
|
-
const bools = await Promise.all(checkedCodecs.map(codec => canEncodeAudio(codec, options)));
|
|
665
|
-
return checkedCodecs.filter((_, i) => bools[i]);
|
|
666
|
-
};
|
|
667
|
-
|
|
668
|
-
/**
|
|
669
|
-
* Returns the list of all subtitle codecs that can be encoded by the browser.
|
|
670
|
-
* @group Encoding
|
|
671
|
-
* @public
|
|
672
|
-
*/
|
|
673
|
-
export const getEncodableSubtitleCodecs = async (
|
|
674
|
-
checkedCodecs: SubtitleCodec[] = SUBTITLE_CODECS as unknown as SubtitleCodec[],
|
|
675
|
-
): Promise<SubtitleCodec[]> => {
|
|
676
|
-
const bools = await Promise.all(checkedCodecs.map(canEncodeSubtitles));
|
|
677
|
-
return checkedCodecs.filter((_, i) => bools[i]);
|
|
678
|
-
};
|
|
679
|
-
|
|
680
|
-
/**
|
|
681
|
-
* Returns the first video codec from the given list that can be encoded by the browser.
|
|
682
|
-
* @group Encoding
|
|
683
|
-
* @public
|
|
684
|
-
*/
|
|
685
|
-
export const getFirstEncodableVideoCodec = async (
|
|
686
|
-
checkedCodecs: VideoCodec[],
|
|
687
|
-
options?: {
|
|
688
|
-
width?: number;
|
|
689
|
-
height?: number;
|
|
690
|
-
bitrate?: number | Quality;
|
|
691
|
-
},
|
|
692
|
-
): Promise<VideoCodec | null> => {
|
|
693
|
-
for (const codec of checkedCodecs) {
|
|
694
|
-
if (await canEncodeVideo(codec, options)) {
|
|
695
|
-
return codec;
|
|
696
|
-
}
|
|
697
|
-
}
|
|
698
|
-
|
|
699
|
-
return null;
|
|
700
|
-
};
|
|
701
|
-
|
|
702
|
-
/**
|
|
703
|
-
* Returns the first audio codec from the given list that can be encoded by the browser.
|
|
704
|
-
* @group Encoding
|
|
705
|
-
* @public
|
|
706
|
-
*/
|
|
707
|
-
export const getFirstEncodableAudioCodec = async (
|
|
708
|
-
checkedCodecs: AudioCodec[],
|
|
709
|
-
options?: {
|
|
710
|
-
numberOfChannels?: number;
|
|
711
|
-
sampleRate?: number;
|
|
712
|
-
bitrate?: number | Quality;
|
|
713
|
-
},
|
|
714
|
-
): Promise<AudioCodec | null> => {
|
|
715
|
-
for (const codec of checkedCodecs) {
|
|
716
|
-
if (await canEncodeAudio(codec, options)) {
|
|
717
|
-
return codec;
|
|
718
|
-
}
|
|
719
|
-
}
|
|
720
|
-
|
|
721
|
-
return null;
|
|
722
|
-
};
|
|
723
|
-
|
|
724
|
-
/**
|
|
725
|
-
* Returns the first subtitle codec from the given list that can be encoded by the browser.
|
|
726
|
-
* @group Encoding
|
|
727
|
-
* @public
|
|
728
|
-
*/
|
|
729
|
-
export const getFirstEncodableSubtitleCodec = async (
|
|
730
|
-
checkedCodecs: SubtitleCodec[],
|
|
731
|
-
): Promise<SubtitleCodec | null> => {
|
|
732
|
-
for (const codec of checkedCodecs) {
|
|
733
|
-
if (await canEncodeSubtitles(codec)) {
|
|
734
|
-
return codec;
|
|
735
|
-
}
|
|
736
|
-
}
|
|
737
|
-
|
|
738
|
-
return null;
|
|
739
|
-
};
|