Skip to content

🎥 Make videos programmatically with React

add a test for it and prevent infinite loop on cropped mp4s

Jonny Burger0a7f3733b6f9
diff --git a/packages/example-videos/src/remote-videos.ts b/packages/example-videos/src/remote-videos.ts
index acd46cf2f7..85bf318612 100644
--- a/packages/example-videos/src/remote-videos.ts
+++ b/packages/example-videos/src/remote-videos.ts
@@ -16,6 +16,8 @@ export const remoteExampleVideos = {
'https://remotion-assets.s3.eu-central-1.amazonaws.com/example-videos/webm-no-codecprivate.webm',
tsKeyframes:
'https://remotion-assets.s3.eu-central-1.amazonaws.com/example-videos/ts-keyframes.ts',
+ largeStsd:
+ 'https://remotion-assets.s3.eu-central-1.amazonaws.com/example-videos/large-stsd.mp4',
};
export const privateExampleVideos = {
diff --git a/packages/media-parser/src/containers/iso-base-media/mdat/mdat.ts b/packages/media-parser/src/containers/iso-base-media/mdat/mdat.ts
index b936f5ecbc..2e6d734734 100644
--- a/packages/media-parser/src/containers/iso-base-media/mdat/mdat.ts
+++ b/packages/media-parser/src/containers/iso-base-media/mdat/mdat.ts
@@ -86,6 +86,16 @@ export const parseMdatSection = async (
return makeSkip(endOfMdat);
}
+ // Corrupt file: Sample is beyond the end of the file. Don't process it.
+ if (
+ samplesWithIndex.samplePosition.offset +
+ samplesWithIndex.samplePosition.size >
+ state.contentLength
+ ) {
+ return makeSkip(endOfMdat);
+ }
+
+ // Need to fetch more data
if (iterator.bytesRemaining() < samplesWithIndex.samplePosition.size) {
return null;
}
diff --git a/packages/media-parser/src/test/very-big-mp4.test.ts b/packages/media-parser/src/test/very-big-mp4.test.ts
new file mode 100644
index 0000000000..a164590a38
--- /dev/null
+++ b/packages/media-parser/src/test/very-big-mp4.test.ts
@@ -0,0 +1,30 @@
+import {getRemoteExampleVideo} from '@remotion/example-videos';
+import {beforeAll, expect, test} from 'bun:test';
+import {nodeReader} from '../node';
+import {parseMedia} from '../parse-media';
+
+beforeAll(async () => {
+ await getRemoteExampleVideo('largeStsd');
+});
+
+test('should process the stsd of a 2 hour long video', async () => {
+ let videoSamples = 0;
+
+ const {durationInSeconds} = await parseMedia({
+ // Video is cropped to only the first few frames
+ src: await getRemoteExampleVideo('largeStsd'),
+ reader: nodeReader,
+ fields: {
+ durationInSeconds: true,
+ },
+ acknowledgeRemotionLicense: true,
+ onVideoTrack: () => {
+ return () => {
+ videoSamples++;
+ };
+ },
+ });
+
+ expect(videoSamples).toBe(6);
+ expect(durationInSeconds).toBe(22947.121);
+});