Add own encoded sample classes and port the code to them

This commit is contained in:
Vanilagy
2025-01-04 00:14:34 +01:00
parent f6d12390cd
commit e83356662e
10 changed files with 694 additions and 434 deletions
+38 -8
View File
@@ -842,7 +842,7 @@ export const canEncode = (codec: MediaCodec) => {
return canEncodeSubtitles(codec as SubtitleCodec);
}
throw new Error('Unhandled codec.');
throw new TypeError(`Unknown codec '${codec}'.`);
};
/** @public */
@@ -851,6 +851,19 @@ export const canEncodeVideo = async (codec: VideoCodec, { width = 1280, height =
height?: number;
bitrate?: 1e6;
} = {}) => {
if (!VIDEO_CODECS.includes(codec)) {
return false;
}
if (!Number.isInteger(width) || width <= 0) {
throw new TypeError('width must be a positive integer.');
}
if (!Number.isInteger(height) || height <= 0) {
throw new TypeError('height must be a positive integer.');
}
if (!Number.isInteger(bitrate) || bitrate <= 0) {
throw new TypeError('bitrate must be a positive integer.');
}
if (typeof VideoEncoder === 'undefined') {
return false;
}
@@ -872,14 +885,27 @@ export const canEncodeAudio = async (codec: AudioCodec, { numberOfChannels = 2,
sampleRate?: number;
bitrate?: number;
} = {}) => {
if (typeof AudioEncoder === 'undefined') {
if (!AUDIO_CODECS.includes(codec)) {
return false;
}
if (!Number.isInteger(numberOfChannels) || numberOfChannels <= 0) {
throw new TypeError('numberOfChannels must be a positive integer.');
}
if (!Number.isInteger(sampleRate) || sampleRate <= 0) {
throw new TypeError('sampleRate must be a positive integer.');
}
if (!Number.isInteger(bitrate) || bitrate <= 0) {
throw new TypeError('bitrate must be a positive integer.');
}
if ((PCM_CODECS as readonly string[]).includes(codec)) {
return false; // TODO write encoder
}
if (typeof AudioEncoder === 'undefined') {
return false;
}
const support = await AudioEncoder.isConfigSupported({
codec: buildAudioCodecString(codec, numberOfChannels, sampleRate),
numberOfChannels,
@@ -891,6 +917,16 @@ export const canEncodeAudio = async (codec: AudioCodec, { numberOfChannels = 2,
return support.supported === true;
};
/** @public */
export const canEncodeSubtitles = async (codec: SubtitleCodec) => {
if (!SUBTITLE_CODECS.includes(codec)) {
return false;
}
return true;
};
/** @public */
export const getEncodableCodecs = async (): Promise<MediaCodec[]> => {
const [videoCodecs, audioCodecs, subtitleCodecs] = await Promise.all([
@@ -902,12 +938,6 @@ export const getEncodableCodecs = async (): Promise<MediaCodec[]> => {
return [...videoCodecs, ...audioCodecs, ...subtitleCodecs];
};
/** @public */
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export const canEncodeSubtitles = async (codec: SubtitleCodec) => {
return true;
};
/** @public */
export const getEncodableVideoCodecs = async (): Promise<VideoCodec[]> => {
const bools = await Promise.all(VIDEO_CODECS.map(canEncode));