🎥 Make videos programmatically with React
fix fields
Jonny Burger38604b2b3397
diff --git a/packages/media-parser/src/boxes/riff/parse-movi.ts b/packages/media-parser/src/boxes/riff/parse-movi.tsindex b3d1a3b414..07b5e9805a 100644--- a/packages/media-parser/src/boxes/riff/parse-movi.ts+++ b/packages/media-parser/src/boxes/riff/parse-movi.ts@@ -66,25 +66,23 @@ export const handleChunk = async ({// We must also NOT pass a duration because if the the next sample is 0,// this sample would be longer. Chrome will pad it with silence.// If we'd pass a duration instead, it would shift the audio and we think that audio is not finished- if (data.length > 0) {- await state.callbacks.onVideoSample(- trackId,- convertAudioOrVideoSampleToWebCodecsTimestamps(- {- cts: timestamp,- dts: timestamp,- data,- duration: undefined,- timestamp,- trackId,- type: keyOrDelta,- offset,- timescale: samplesPerSecond,- },- 1,- ),- );- }+ await state.callbacks.onVideoSample(+ trackId,+ convertAudioOrVideoSampleToWebCodecsTimestamps(+ {+ cts: timestamp,+ dts: timestamp,+ data,+ duration: undefined,+ timestamp,+ trackId,+ type: keyOrDelta,+ offset,+ timescale: samplesPerSecond,+ },+ 1,+ ),+ );return;}@@ -107,26 +105,23 @@ export const handleChunk = async ({// We must also NOT pass a duration because if the the next sample is 0,// this sample would be longer. Chrome will pad it with silence.// If we'd pass a duration instead, it would shift the audio and we think that audio is not finished-- if (data.length > 0) {- await state.callbacks.onAudioSample(- trackId,- convertAudioOrVideoSampleToWebCodecsTimestamps(- {- cts: timestamp,- dts: timestamp,- data,- duration: undefined,- timestamp,- trackId,- type: 'key',- offset,- timescale: samplesPerSecond,- },- 1,- ),- );- }+ await state.callbacks.onAudioSample(+ trackId,+ convertAudioOrVideoSampleToWebCodecsTimestamps(+ {+ cts: timestamp,+ dts: timestamp,+ data,+ duration: undefined,+ timestamp,+ trackId,+ type: 'key',+ offset,+ timescale: samplesPerSecond,+ },+ 1,+ ),+ );}};diff --git a/packages/media-parser/src/get-duration.ts b/packages/media-parser/src/get-duration.tsindex 4d5001628e..8c70ce0033 100644--- a/packages/media-parser/src/get-duration.ts+++ b/packages/media-parser/src/get-duration.ts@@ -142,9 +142,24 @@ export const getDuration = (throw new Error('Has no duration ' + (structure satisfies never));};+// `duration` just grabs from metadata, and otherwise returns null+// Therefore just checking if we have tracksexport const hasDuration = (structure: Structure,parserState: ParserState,): boolean => {return hasTracks(structure, parserState);};++// `slowDuration` does through everything, and therefore is false+// Unless it it somewhere in the metadata and is non-null+export const hasSlowDuration = (+ structure: Structure,+ parserState: ParserState,+): boolean => {+ try {+ return getDuration(structure, parserState) !== null;+ } catch {+ return false;+ }+};diff --git a/packages/media-parser/src/has-all-info.ts b/packages/media-parser/src/has-all-info.tsindex 027316d5e7..ee3cd7a229 100644--- a/packages/media-parser/src/has-all-info.ts+++ b/packages/media-parser/src/has-all-info.ts@@ -1,7 +1,7 @@import {hasAudioCodec} from './get-audio-codec';import {hasContainer} from './get-container';import {hasDimensions} from './get-dimensions';-import {hasDuration} from './get-duration';+import {hasDuration, hasSlowDuration} from './get-duration';import {hasFps, hasFpsSuitedForSlowFps} from './get-fps';import {hasHdr} from './get-is-hdr';import {hasTracks} from './get-tracks';@@ -30,12 +30,14 @@ export const getAvailableInfo = ({return false;}- if (key === 'durationInSeconds' || key === 'slowDurationInSeconds') {- // If we have fastDuration, we also propagate it to slowDuration- // Otherwise, we need to go through the entire file to get the duration+ if (key === 'durationInSeconds') {return Boolean(structure && hasDuration(structure, state));}+ if (key === 'slowDurationInSeconds') {+ return Boolean(structure && hasSlowDuration(structure, state));+ }+if (key === 'dimensions' ||key === 'rotation' ||diff --git a/packages/media-parser/src/state/sample-callbacks.ts b/packages/media-parser/src/state/sample-callbacks.tsindex 78e962b5fb..7c1faf7569 100644--- a/packages/media-parser/src/state/sample-callbacks.ts+++ b/packages/media-parser/src/state/sample-callbacks.ts@@ -92,7 +92,9 @@ export const sampleCallback = ({samplesForTrack[trackId]++;const callback = videoSampleCallbacks[trackId];- if (callback) {++ // If we emit samples with data 0, Chrome will fail+ if (callback && videoSample.data.length > 0) {await callback(videoSample);}diff --git a/packages/media-parser/src/state/slow-duration-fps.ts b/packages/media-parser/src/state/slow-duration-fps.tsindex 2c5b6cb024..fbd1a1abe4 100644--- a/packages/media-parser/src/state/slow-duration-fps.ts+++ b/packages/media-parser/src/state/slow-duration-fps.ts@@ -7,7 +7,9 @@ export const slowDurationAndFpsState = () => {const getSlowDurationInSeconds = () => {if (smallestSample !== undefined && largestSample !== undefined) {- return largestSample - smallestSample;+ const startingTimestampDifference = largestSample - smallestSample;+ const timeBetweenSamples = startingTimestampDifference / (samples - 1);+ return timeBetweenSamples * samples;}throw new Error('No samples');@@ -33,7 +35,7 @@ export const slowDurationAndFpsState = () => {},getSlowDurationInSeconds,getFps: () => {- return (samples - 1) / getSlowDurationInSeconds();+ return samples / getSlowDurationInSeconds();},};};diff --git a/packages/media-parser/src/test/parse-avi-file.test.ts b/packages/media-parser/src/test/parse-avi-file.test.tsindex 573dd3f078..01820e8eb7 100644--- a/packages/media-parser/src/test/parse-avi-file.test.ts+++ b/packages/media-parser/src/test/parse-avi-file.test.ts@@ -66,7 +66,7 @@ test('AVI file', async () => {expect(size).toBe(742478);expect(audioTrackCount).toBe(1);expect(videoTrackCount).toBe(1);- expect(audioSamples).toBe(1433);+ expect(audioSamples).toBe(1435);expect(videoSamples).toBe(901);expect(videoCodec).toBe('h264');expect(tracks.audioTracks).toEqual([diff --git a/packages/media-parser/src/test/stream-local.test.ts b/packages/media-parser/src/test/stream-local.test.tsindex 39c67c0147..37112724fa 100644--- a/packages/media-parser/src/test/stream-local.test.ts+++ b/packages/media-parser/src/test/stream-local.test.ts@@ -35,7 +35,7 @@ test('Should stream WebM with no duration', async () => {height: 690,});expect(result.fps).toBe(null);- expect(result.slowFps).toBe(0.9968433294567204);+ expect(result.slowFps).toBe(0.9968433294567203);expect(result.videoCodec).toBe('vp8');expect(result.audioCodec).toBe(null);expect(result.rotation).toBe(0);diff --git a/packages/media-parser/src/test/transportstream.test.ts b/packages/media-parser/src/test/transportstream.test.tsindex 9ddf6861d8..b40729fbb3 100644--- a/packages/media-parser/src/test/transportstream.test.ts+++ b/packages/media-parser/src/test/transportstream.test.ts@@ -180,8 +180,8 @@ test('Transport stream', async () => {expect(audioSamples).toBe(234);expect(audioCodec).toBe('aac');expect(fps).toBe(null);- expect(slowFps).toBe(59.191919191919176);- expect(slowDurationInSeconds).toBe(1.8000000000000007);+ expect(slowFps).toBe(59.19191919191918);+ expect(slowDurationInSeconds).toBe(4.966894197952219);expect(isHdr).toBe(true);expect(videoCodec).toBe('h264');expect(videoSamples).toBe(294);