🎥 Make videos programmatically with React
make parsing stsd more efficient
Jonny Burgerad555320fa61
diff --git a/packages/media-parser/src/get-sample-positions-from-mp4.ts b/packages/media-parser/src/get-sample-positions-from-mp4.tsindex bdd9897ec8..6c5bc589c7 100644--- a/packages/media-parser/src/get-sample-positions-from-mp4.ts+++ b/packages/media-parser/src/get-sample-positions-from-mp4.ts@@ -42,11 +42,17 @@ export const getGroupedSamplesPositionsFromMp4 = ({let timestamp = 0;+ const stscKeys = Array.from(stscBox.entries.keys());for (let i = 0; i < stcoBox.entries.length; i++) {const entry = stcoBox.entries[i];const chunk = i + 1;- const stscEntry = stscBox.entries.findLast((e) => e.firstChunk <= chunk);- if (!stscEntry) {+ const stscEntry = stscKeys.findLast((e) => e <= chunk);+ if (stscEntry === undefined) {+ throw new Error('should not be');+ }++ const samplesPerChunk = stscBox.entries.get(stscEntry);+ if (samplesPerChunk === undefined) {throw new Error('should not be');}@@ -55,13 +61,13 @@ export const getGroupedSamplesPositionsFromMp4 = ({cts: timestamp,dts: timestamp,offset: Number(entry),- size: stszBox.sampleSize * stscEntry.samplesPerChunk,- duration: stscEntry.samplesPerChunk,+ size: stszBox.sampleSize * samplesPerChunk,+ duration: samplesPerChunk,isKeyframe: true,bigEndian,chunkSize: stszBox.sampleSize,});- timestamp += stscEntry.samplesPerChunk;+ timestamp += samplesPerChunk;}return samples;diff --git a/packages/media-parser/src/get-sample-positions.ts b/packages/media-parser/src/get-sample-positions.tsindex 0df78cc4bb..7c88daed71 100644--- a/packages/media-parser/src/get-sample-positions.ts+++ b/packages/media-parser/src/get-sample-positions.ts@@ -56,11 +56,9 @@ export const getSamplePositions = ({let samplesPerChunk = 1;for (let i = 0; i < chunks.length; i++) {- const hasEntry = stscBox.entries.find(- (entry) => entry.firstChunk === i + 1,- );- if (hasEntry) {- samplesPerChunk = hasEntry.samplesPerChunk;+ const hasEntry = stscBox.entries.get(i + 1);+ if (hasEntry !== undefined) {+ samplesPerChunk = hasEntry;}let offsetInThisChunk = 0;@@ -72,7 +70,7 @@ export const getSamplePositions = ({: stszBox.entries[samples.length];const isKeyframe = stssBox- ? stssBox.sampleNumber.includes(samples.length + 1)+ ? stssBox.sampleNumber.has(samples.length + 1): true;const delta = sttsDeltas[samples.length];diff --git a/packages/media-parser/src/test/parse-stsc.test.ts b/packages/media-parser/src/test/parse-stsc.test.tsindex ba494980f2..87ce39285c 100644--- a/packages/media-parser/src/test/parse-stsc.test.ts+++ b/packages/media-parser/src/test/parse-stsc.test.ts@@ -26,16 +26,10 @@ test('Parse stsc box', () => {type: 'stsc-box',version: 0,entryCount: 2,- entries: [- {- firstChunk: 1,- samplesPerChunk: 2,- },- {- firstChunk: 2,- samplesPerChunk: 1,- },- ],+ entries: new Map([+ [1, 2],+ [2, 1],+ ]),});});@@ -66,39 +60,15 @@ test('Parse stsc box 2', () => {type: 'stsc-box',version: 0,entryCount: 8,- entries: [- {- firstChunk: 1,- samplesPerChunk: 1,- },- {- firstChunk: 2,- samplesPerChunk: 2,- },- {- firstChunk: 3,- samplesPerChunk: 1,- },- {- firstChunk: 4,- samplesPerChunk: 2,- },- {- firstChunk: 5,- samplesPerChunk: 1,- },- {- firstChunk: 7,- samplesPerChunk: 2,- },- {- firstChunk: 8,- samplesPerChunk: 1,- },- {- firstChunk: 9,- samplesPerChunk: 4,- },- ],+ entries: new Map([+ [1, 1],+ [2, 2],+ [3, 1],+ [4, 2],+ [5, 1],+ [7, 2],+ [8, 1],+ [9, 4],+ ]),});});