🎥 Make videos programmatically with React
get mfro atom
Jonny Burger3edbe2659f6f
diff --git a/packages/media-parser/src/containers/iso-base-media/get-mfro-atom.ts b/packages/media-parser/src/containers/iso-base-media/get-mfro-atom.tsnew file mode 100644index 0000000000..f33c5ad19e--- /dev/null+++ b/packages/media-parser/src/containers/iso-base-media/get-mfro-atom.ts@@ -0,0 +1,49 @@+import type {MediaParserController} from '../../controller/media-parser-controller';+import {getArrayBufferIterator} from '../../iterator/buffer-iterator';+import type {ParseMediaSrc} from '../../options';+import type {ReaderInterface} from '../../readers/reader';++export const getMfroAtom = async ({+ src,+ contentLength,+ readerInterface,+ controller,+}: {+ src: ParseMediaSrc;+ contentLength: number;+ readerInterface: ReaderInterface;+ controller: MediaParserController;+}) => {+ const result = await readerInterface.read({+ controller,+ range: [contentLength - 16, contentLength - 1],+ src,+ });+ const {value} = await result.reader.reader.read();+ if (!value) {+ return null;+ }++ result.reader.abort();+ const iterator = getArrayBufferIterator(value, value.length);+ const size = iterator.getUint32();+ if (size !== 16) {+ return null;+ }++ const atom = iterator.getByteString(4, false);+ if (atom !== 'mfro') {+ return null;+ }++ const version = iterator.getUint8();+ if (version !== 0) {+ return null;+ }++ // flags+ iterator.discard(3);+ const parentSize = iterator.getUint32();++ return parentSize;+};diff --git a/packages/media-parser/src/containers/iso-base-media/get-seeking-from-mp4.ts b/packages/media-parser/src/containers/iso-base-media/get-seeking-from-mp4.tsindex dece881300..b7f05cdd92 100644--- a/packages/media-parser/src/containers/iso-base-media/get-seeking-from-mp4.ts+++ b/packages/media-parser/src/containers/iso-base-media/get-seeking-from-mp4.ts@@ -1,6 +1,9 @@+import type {MediaParserController} from '../../controller/media-parser-controller';import {getTracksFromMoovBox} from '../../get-tracks';import type {LogLevel} from '../../log';import {Log} from '../../log';+import type {ParseMediaSrc} from '../../options';+import type {ReaderInterface} from '../../readers/reader';import type {IsoBaseMediaSeekingInfo} from '../../seeking-info';import {getCurrentVideoSection,@@ -9,22 +12,31 @@ import {import type {SeekResolution} from '../../work-on-seek-request';import {collectSamplePositionsFromMoofBoxes} from './collect-sample-positions-from-moof-boxes';import {findKeyframeBeforeTime} from './find-keyframe-before-time';+import {getMfroAtom} from './get-mfro-atom';import {getSamplePositionBounds} from './get-sample-position-bounds';import {getSamplePositionsFromTrack} from './get-sample-positions-from-track';import type {TrakBox} from './trak/trak';import {getTkhdBox} from './traversal';-export const getSeekingByteFromIsoBaseMedia = ({+export const getSeekingByteFromIsoBaseMedia = async ({info,time,logLevel,currentPosition,+ src,+ contentLength,+ controller,+ readerInterface,}: {info: IsoBaseMediaSeekingInfo;time: number;logLevel: LogLevel;currentPosition: number;-}): SeekResolution => {+ src: ParseMediaSrc;+ contentLength: number;+ controller: MediaParserController;+ readerInterface: ReaderInterface;+}): Promise<SeekResolution> => {const tracks = getTracksFromMoovBox(info.moovBox);const allTracks = [...tracks.videoTracks,@@ -41,6 +53,14 @@ export const getSeekingByteFromIsoBaseMedia = ({const {timescale} = firstVideoTrack;if (info.moofBoxes.length > 0) {+ const mfraAtom = await getMfroAtom({+ contentLength,+ controller,+ readerInterface,+ src,+ });++ console.log(mfraAtom);const tkhdBox = getTkhdBox(firstVideoTrack.trakBox as TrakBox);if (!tkhdBox) {throw new Error('Expected tkhd box in trak box');diff --git a/packages/media-parser/src/get-seeking-info.ts b/packages/media-parser/src/get-seeking-info.tsindex c02f138c2c..37e17e28a3 100644--- a/packages/media-parser/src/get-seeking-info.ts+++ b/packages/media-parser/src/get-seeking-info.ts@@ -1,6 +1,9 @@import {getSeekingByteFromIsoBaseMedia} from './containers/iso-base-media/get-seeking-from-mp4';import {getSeekingInfoFromMp4} from './containers/iso-base-media/get-seeking-info-from-mp4';+import type {MediaParserController} from './controller/media-parser-controller';import type {LogLevel} from './log';+import type {ParseMediaSrc} from './options';+import type {ReaderInterface} from './readers/reader';import type {SeekingInfo} from './seeking-info';import type {ParserState} from './state/parser-state';import type {SeekResolution} from './work-on-seek-request';@@ -24,18 +27,30 @@ export const getSeekingByte = ({time,logLevel,currentPosition,+ src,+ contentLength,+ controller,+ readerInterface,}: {info: SeekingInfo;time: number;logLevel: LogLevel;currentPosition: number;-}): SeekResolution => {+ src: ParseMediaSrc;+ contentLength: number;+ controller: MediaParserController;+ readerInterface: ReaderInterface;+}): Promise<SeekResolution> => {if (info.type === 'iso-base-media-seeking-info') {return getSeekingByteFromIsoBaseMedia({info,time,logLevel,currentPosition,+ src,+ contentLength,+ controller,+ readerInterface,});}diff --git a/packages/media-parser/src/work-on-seek-request.ts b/packages/media-parser/src/work-on-seek-request.tsindex b0835c4e06..4d6dc555ba 100644--- a/packages/media-parser/src/work-on-seek-request.ts+++ b/packages/media-parser/src/work-on-seek-request.ts@@ -4,13 +4,13 @@ import {Log} from './log';import {performSeek} from './perform-seek';import type {ParserState} from './state/parser-state';-const turnSeekIntoByte = ({+const turnSeekIntoByte = async ({seek,state,}: {seek: Seek;state: ParserState;-}): SeekResolution => {+}): Promise<SeekResolution> => {const videoSections = state.videoSection.getVideoSections();if (videoSections.length === 0) {Log.trace(state.logLevel, 'No video sections defined, cannot seek yet');@@ -28,11 +28,15 @@ const turnSeekIntoByte = ({};}- const seekingByte = getSeekingByte({+ const seekingByte = await getSeekingByte({info: seekingInfo,time: seek.time,logLevel: state.logLevel,currentPosition: state.iterator.counter.getOffset(),+ src: state.src,+ contentLength: state.contentLength,+ controller: state.controller,+ readerInterface: state.readerInterface,});return seekingByte;@@ -57,7 +61,7 @@ export const workOnSeekRequest = async (state: ParserState) => {}Log.trace(state.logLevel, `Has seek request: ${JSON.stringify(seek)}`);- const resolution = turnSeekIntoByte({seek, state});+ const resolution = await turnSeekIntoByte({seek, state});Log.trace(state.logLevel, `Seek action: ${JSON.stringify(resolution)}`);if (resolution.type === 'intermediary-seek') {