From 9c501808b17b7798e658287e0e67621ff89da3a8 Mon Sep 17 00:00:00 2001 From: Vanilagy <1696106+Vanilagy@users.noreply.github.com> Date: Sun, 12 Jan 2025 16:52:28 +0100 Subject: [PATCH] Add WebMInputFormat, add OutputFormat.getFileExtension() --- dev/index.html | 2 +- src/index.ts | 3 ++- src/input-format.ts | 31 ++++++++++++++++++++++++++----- src/output-format.ts | 21 +++++++++++++++++++++ 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/dev/index.html b/dev/index.html index 92ad378..ad720d0 100644 --- a/dev/index.html +++ b/dev/index.html @@ -174,5 +174,5 @@ Testing... <00:17.350>One... <00:18.125>Two... await output.finalize(); console.log(target); - download(new Blob([target.buffer]), 'test.webm'); + download(new Blob([target.buffer]), 'test' + format.getFileExtension()); \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 8f4c15a..0813fe7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -52,8 +52,9 @@ export { InputFormat, IsobmffInputFormat, Mp4InputFormat, - QuickTimeInputFormat as MovInputFormat, + QuickTimeInputFormat, MatroskaInputFormat, + WebMInputFormat, WaveInputFormat, ALL_FORMATS, MP4, diff --git a/src/input-format.ts b/src/input-format.ts index d730a7d..5125ae2 100644 --- a/src/input-format.ts +++ b/src/input-format.ts @@ -81,7 +81,7 @@ export class QuickTimeInputFormat extends IsobmffInputFormat { /** @public */ export class MatroskaInputFormat extends InputFormat { /** @internal */ - async _canReadInput(input: Input) { + protected async isSupportedEBMLOfDocType(input: Input, desiredDocType: string) { const sourceSize = await input._mainReader.source._getSize(); if (sourceSize < 8) { return false; @@ -89,7 +89,7 @@ export class MatroskaInputFormat extends InputFormat { const ebmlReader = new EBMLReader(input._mainReader); const varIntSize = ebmlReader.readVarIntSize(); - if (varIntSize < 1 || varIntSize > 6) { + if (varIntSize < 1 || varIntSize > 8) { return false; } @@ -124,7 +124,7 @@ export class MatroskaInputFormat extends InputFormat { }; break; case EBMLId.DocType: { const docType = ebmlReader.readString(size); - if (docType !== 'matroska' && docType !== 'webm') { + if (docType !== desiredDocType) { return false; } }; break; @@ -142,6 +142,11 @@ export class MatroskaInputFormat extends InputFormat { return true; } + /** @internal */ + _canReadInput(input: Input) { + return this.isSupportedEBMLOfDocType(input, 'matroska'); + } + /** @internal */ _createDemuxer(input: Input) { return new MatroskaDemuxer(input); @@ -156,6 +161,22 @@ export class MatroskaInputFormat extends InputFormat { } } +/** @public */ +export class WebMInputFormat extends MatroskaInputFormat { + /** @internal */ + override _canReadInput(input: Input) { + return this.isSupportedEBMLOfDocType(input, 'webm'); + } + + override getName() { + return 'WebM'; + } + + override getMimeType() { + return 'video/webm'; + } +} + /** @public */ export class WaveInputFormat extends InputFormat { async _canReadInput(input: Input) { @@ -196,9 +217,9 @@ export const QTFF = new QuickTimeInputFormat(); /** @public */ export const MATROSKA = new MatroskaInputFormat(); /** @public */ -export const WEBM = MATROSKA; +export const WEBM = new WebMInputFormat(); /** @public */ export const WAVE = new WaveInputFormat(); /** @public */ -export const ALL_FORMATS: InputFormat[] = [MP4, QTFF, MATROSKA, WAVE]; +export const ALL_FORMATS: InputFormat[] = [MP4, QTFF, MATROSKA, WEBM, WAVE]; diff --git a/src/output-format.ts b/src/output-format.ts index 63e3ab7..bbed1f4 100644 --- a/src/output-format.ts +++ b/src/output-format.ts @@ -12,6 +12,7 @@ export abstract class OutputFormat { /** @internal */ abstract _getName(): string; + abstract getFileExtension(): string; abstract getSupportedCodecs(): MediaCodec[]; getSupportedVideoCodecs() { @@ -69,6 +70,10 @@ export class Mp4OutputFormat extends IsobmffOutputFormat { return 'MP4'; } + getFileExtension() { + return '.mp4'; + } + getSupportedCodecs() { return Mp4OutputFormat.getSupportedCodecs(); } @@ -98,6 +103,10 @@ export class MovOutputFormat extends IsobmffOutputFormat { return 'MOV'; } + getFileExtension() { + return '.mov'; + } + getSupportedCodecs() { return MovOutputFormat.getSupportedCodecs(); } @@ -152,6 +161,10 @@ export class MkvOutputFormat extends OutputFormat { return 'Matroska'; } + getFileExtension() { + return '.mkv'; + } + getSupportedCodecs() { return MkvOutputFormat.getSupportedCodecs(); } @@ -181,6 +194,10 @@ export class WebMOutputFormat extends MkvOutputFormat { return 'WebM'; } + override getFileExtension() { + return '.webm'; + } + static override getSupportedCodecs(): MediaCodec[] { return [ 'vp8', 'vp9', 'av1', @@ -211,6 +228,10 @@ export class WaveOutputFormat extends OutputFormat { return 'WAVE'; } + getFileExtension() { + return '.wav'; + } + getSupportedCodecs() { return WaveOutputFormat.getSupportedCodecs(); }