🎥 Make videos programmatically with React
dts and delta
Jonny Burger950f61b2bf59
diff --git a/packages/browser-renderer/src/index.ts b/packages/browser-renderer/src/index.tsindex e774b34a0f..97bf301056 100644--- a/packages/browser-renderer/src/index.ts+++ b/packages/browser-renderer/src/index.ts@@ -1,2 +1,2 @@-export {reencodeVideo as reEncodeVideo} from './reencode-video';+export {reencodeVideo} from './reencode-video';export {parseVideo} from './video-parser';diff --git a/packages/example/src/EncoderDemo.tsx b/packages/example/src/EncoderDemo.tsxindex 203cd6600d..296d9017b8 100644--- a/packages/example/src/EncoderDemo.tsx+++ b/packages/example/src/EncoderDemo.tsx@@ -1,4 +1,4 @@-import {parseVideo, reEncodeVideo} from '@remotion/browser-renderer';+import {parseVideo, reencodeVideo} from '@remotion/browser-renderer';import React from 'react';export const EncoderDemo: React.FC = () => {@@ -10,7 +10,7 @@ export const EncoderDemo: React.FC = () => {/><inputtype="file"- onChange={(e) => reEncodeVideo(e.target.files?.[0] as File)}+ onChange={(e) => reencodeVideo(e.target.files?.[0] as File)}/></>);diff --git a/packages/media-parser/src/get-sample-positions.ts b/packages/media-parser/src/get-sample-positions.tsindex 07f7a92aad..16ecc23f14 100644--- a/packages/media-parser/src/get-sample-positions.ts+++ b/packages/media-parser/src/get-sample-positions.ts@@ -2,11 +2,14 @@ import type {StcoBox} from './boxes/iso-base-media/stsd/stco';import type {StscBox} from './boxes/iso-base-media/stsd/stsc';import type {StssBox} from './boxes/iso-base-media/stsd/stss';import type {StszBox} from './boxes/iso-base-media/stsd/stsz';+import type {SttsBox} from './boxes/iso-base-media/stts/stts';export type SamplePosition = {offset: number;size: number;isKeyframe: boolean;+ dts: number;+ duration: number;};export const getSamplePositions = ({@@ -14,12 +17,23 @@ export const getSamplePositions = ({stszBox,stscBox,stssBox,+ sttsBox,}: {stcoBox: StcoBox;stszBox: StszBox;stscBox: StscBox;stssBox: StssBox | null;+ sttsBox: SttsBox;}) => {+ const deltas: number[] = [];+ for (const distribution of sttsBox.sampleDistribution) {+ for (let i = 0; i < distribution.sampleCount; i++) {+ deltas.push(distribution.sampleDelta);+ }+ }++ let dts = 0;+const chunks = stcoBox.entries;const samples: SamplePosition[] = [];@@ -45,11 +59,16 @@ export const getSamplePositions = ({? stssBox.sampleNumber.includes(samples.length + 1): true;+ const delta = deltas[samples.length];+samples.push({offset: chunks[i] + offsetInThisChunk,size,isKeyframe,+ dts,+ duration: delta,});+ dts += delta;offsetInThisChunk += size;}}diff --git a/packages/media-parser/src/get-tracks.ts b/packages/media-parser/src/get-tracks.tsindex 95bbc3ddf0..55f56fbda7 100644--- a/packages/media-parser/src/get-tracks.ts+++ b/packages/media-parser/src/get-tracks.ts@@ -15,6 +15,7 @@ import {getStscBox,getStssBox,getStszBox,+ getSttsBox,getTkhdBox,getTraks,getVideoDescriptors,@@ -80,6 +81,7 @@ export const getTracks = (const stcoBox = getStcoBox(track);const stscBox = getStscBox(track);const stssBox = getStssBox(track);+ const sttsBox = getSttsBox(track);const tkhdBox = getTkhdBox(track);const videoDescriptors = getVideoDescriptors(track);const timescaleAndDuration = getTimescaleAndDuration(track);@@ -100,6 +102,10 @@ export const getTracks = (throw new Error('Expected stsc box in trak box');}+ if (!sttsBox) {+ throw new Error('Expected stts box in trak box');+ }+if (!timescaleAndDuration) {throw new Error('Expected timescale and duration in trak box');}@@ -109,6 +115,7 @@ export const getTracks = (stscBox,stszBox,stssBox,+ sttsBox,});if (trakBoxContainsAudio(track)) {diff --git a/packages/media-parser/src/test/stream-samples.test.ts b/packages/media-parser/src/test/stream-samples.test.tsindex 3c6448f9de..2fa3de7714 100644--- a/packages/media-parser/src/test/stream-samples.test.ts+++ b/packages/media-parser/src/test/stream-samples.test.ts@@ -27,51 +27,71 @@ test('Stream samples', async () => {offset: 1637,size: 4834,isKeyframe: true,+ dts: 0,+ duration: 512,},{offset: 6471,size: 437,isKeyframe: false,+ dts: 512,+ duration: 512,},{offset: 7868,size: 698,isKeyframe: false,+ dts: 1024,+ duration: 512,},{offset: 10486,size: 595,isKeyframe: false,+ dts: 1536,+ duration: 512,},{offset: 12041,size: 548,isKeyframe: false,+ dts: 2048,+ duration: 512,},{offset: 14509,size: 611,isKeyframe: false,+ dts: 2560,+ duration: 512,},{offset: 16080,size: 701,isKeyframe: false,+ dts: 3072,+ duration: 512,},{offset: 17741,size: 535,isKeyframe: false,+ dts: 3584,+ duration: 512,},{offset: 20196,size: 776,isKeyframe: false,+ dts: 4096,+ duration: 512,},{offset: 21932,size: 586,isKeyframe: false,+ dts: 4608,+ duration: 512,},],description,@@ -88,76 +108,106 @@ test('Stream samples', async () => {offset: 6908,size: 960,isKeyframe: true,+ dts: 0,+ duration: 1152,},{offset: 8566,size: 960,isKeyframe: true,+ dts: 1152,+ duration: 1152,},{offset: 9526,size: 960,isKeyframe: true,+ dts: 2304,+ duration: 1152,},{offset: 11081,size: 960,isKeyframe: true,+ dts: 3456,+ duration: 1152,},{offset: 12589,size: 960,isKeyframe: true,+ dts: 4608,+ duration: 1152,},{offset: 13549,size: 960,isKeyframe: true,+ dts: 5760,+ duration: 1152,},{offset: 15120,size: 960,isKeyframe: true,+ dts: 6912,+ duration: 1152,},{offset: 16781,size: 960,isKeyframe: true,+ dts: 8064,+ duration: 1152,},{offset: 18276,size: 960,isKeyframe: true,+ dts: 9216,+ duration: 1152,},{offset: 19236,size: 960,isKeyframe: true,+ dts: 10368,+ duration: 1152,},{offset: 20972,size: 960,isKeyframe: true,+ dts: 11520,+ duration: 1152,},{offset: 22518,size: 960,isKeyframe: true,+ dts: 12672,+ duration: 1152,},{offset: 23478,size: 960,isKeyframe: true,+ dts: 13824,+ duration: 1152,},{offset: 24438,size: 960,isKeyframe: true,+ dts: 14976,+ duration: 1152,},{offset: 25398,size: 960,isKeyframe: true,+ dts: 16128,+ duration: 1152,},],description: null,