From 7c65c2e7e91ca32d53c80bbc2d88deb3fec2fccb Mon Sep 17 00:00:00 2001 From: Vanilagy <1696106+Vanilagy@users.noreply.github.com> Date: Mon, 6 Jan 2025 19:45:47 +0100 Subject: [PATCH] Slightly modify input formats --- src/index.ts | 6 ++-- src/input-format.ts | 70 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 60 insertions(+), 16 deletions(-) diff --git a/src/index.ts b/src/index.ts index db91b06..dc90203 100644 --- a/src/index.ts +++ b/src/index.ts @@ -50,13 +50,13 @@ export { Source, BufferSource, BlobSource, UrlSource } from './source'; export { InputFormat, IsobmffInputFormat, + Mp4InputFormat, + QuickTimeInputFormat as MovInputFormat, MatroskaInputFormat, ALL_FORMATS, - ISOBMFF, MP4, - MOV, + QTFF, MATROSKA, - MKV, WEBM, } from './input-format'; export { Input, InputOptions } from './input'; diff --git a/src/input-format.ts b/src/input-format.ts index 8cc9bce..c673d7c 100644 --- a/src/input-format.ts +++ b/src/input-format.ts @@ -10,22 +10,28 @@ export abstract class InputFormat { /** @internal */ abstract _createDemuxer(input: Input): Demuxer; + + abstract getName(): string; + abstract getMimeType(): string; } /** @public */ -export class IsobmffInputFormat extends InputFormat { - /** @internal */ - override async _canReadInput(input: Input) { +export abstract class IsobmffInputFormat extends InputFormat { + protected async _getMajorBrand(input: Input) { const sourceSize = await input._mainReader.source._getSize(); - if (sourceSize < 8) { - return false; + if (sourceSize < 12) { + return null; } const isobmffReader = new IsobmffReader(input._mainReader); isobmffReader.pos = 4; const fourCc = isobmffReader.readAscii(4); - return fourCc === 'ftyp'; + if (fourCc !== 'ftyp') { + return null; + } + + return isobmffReader.readAscii(4); } /** @internal */ @@ -34,6 +40,40 @@ export class IsobmffInputFormat extends InputFormat { } } +/** @public */ +export class Mp4InputFormat extends IsobmffInputFormat { + /** @internal */ + override async _canReadInput(input: Input) { + const majorBrand = await this._getMajorBrand(input); + return majorBrand !== 'qt '; + } + + getName() { + return 'MP4'; + } + + getMimeType() { + return 'video/mp4'; + } +} + +/** @public */ +export class QuickTimeInputFormat extends IsobmffInputFormat { + /** @internal */ + override async _canReadInput(input: Input) { + const majorBrand = await this._getMajorBrand(input); + return majorBrand === 'qt '; + } + + getName() { + return 'QuickTime File Format'; + } + + getMimeType() { + return 'video/quicktime'; + } +} + /** @public */ export class MatroskaInputFormat extends InputFormat { /** @internal */ @@ -45,20 +85,24 @@ export class MatroskaInputFormat extends InputFormat { override _createDemuxer(): never { throw new Error('Not implemented'); } + + getName() { + return 'Matroska'; + } + + getMimeType() { + return 'video/x-matroska'; + } } /** @public */ -export const ISOBMFF = new IsobmffInputFormat(); +export const MP4 = new Mp4InputFormat(); /** @public */ -export const MP4 = ISOBMFF; -/** @public */ -export const MOV = ISOBMFF; +export const QTFF = new QuickTimeInputFormat(); /** @public */ export const MATROSKA = new MatroskaInputFormat(); /** @public */ -export const MKV = MATROSKA; -/** @public */ export const WEBM = MATROSKA; /** @public */ -export const ALL_FORMATS: InputFormat[] = [ISOBMFF, MKV]; +export const ALL_FORMATS: InputFormat[] = [MP4, QTFF, MATROSKA];