Skip to content

🎥 Make videos programmatically with React

literally sick

Jonny Burgerce83bcd231d8
diff --git a/packages/browser-renderer/src/video-parser.ts b/packages/browser-renderer/src/video-parser.ts
index ab16e1ef16..9593e7bf94 100644
--- a/packages/browser-renderer/src/video-parser.ts
+++ b/packages/browser-renderer/src/video-parser.ts
@@ -47,6 +47,15 @@ export const parseVideo = async (file: File) => {
durationInSeconds: true,
},
onVideoSample: (video) => {
+ const chunk = new EncodedVideoChunk({
+ type: video.type,
+ timestamp: video.timestamp,
+ duration: video.duration,
+ data: video.bytes,
+ });
+
+ decoder.decode(chunk);
+
console.log('video sample', video);
},
});
diff --git a/packages/example/src/EncoderDemo.tsx b/packages/example/src/EncoderDemo.tsx
index 7f1b92f985..203cd6600d 100644
--- a/packages/example/src/EncoderDemo.tsx
+++ b/packages/example/src/EncoderDemo.tsx
@@ -1,11 +1,17 @@
-import {parseVideo} from '@remotion/browser-renderer';
+import {parseVideo, reEncodeVideo} from '@remotion/browser-renderer';
import React from 'react';
export const EncoderDemo: React.FC = () => {
return (
- <input
- type="file"
- onChange={(e) => parseVideo(e.target.files?.[0] as File)}
- />
+ <>
+ <input
+ type="file"
+ onChange={(e) => parseVideo(e.target.files?.[0] as File)}
+ />
+ <input
+ type="file"
+ onChange={(e) => reEncodeVideo(e.target.files?.[0] as File)}
+ />
+ </>
);
};
diff --git a/packages/media-parser/src/boxes/iso-base-media/mdat/mdat.ts b/packages/media-parser/src/boxes/iso-base-media/mdat/mdat.ts
index 580b71a037..b813412a39 100644
--- a/packages/media-parser/src/boxes/iso-base-media/mdat/mdat.ts
+++ b/packages/media-parser/src/boxes/iso-base-media/mdat/mdat.ts
@@ -11,16 +11,17 @@ export interface MdatBox {
export type AudioSample = {
bytes: Uint8Array;
- start: number;
+ timestamp: number;
offset: number;
trackId: number;
};
export type VideoSample = {
bytes: Uint8Array;
- start: number;
- offset: number;
+ timestamp: number;
+ duration: number;
trackId: number;
+ type: 'key' | 'delta';
};
export type OnAudioSample = (sample: AudioSample) => void;
@@ -79,18 +80,20 @@ export const parseMdat = ({
if (sampleWithIndex.type === 'audio' && options.onAudioSample) {
options.onAudioSample({
bytes,
- start: sampleWithIndex.samplePosition.offset,
+ timestamp: sampleWithIndex.samplePosition.offset,
offset: data.counter.getOffset(),
trackId: sampleWithIndex.trackId,
});
}
if (sampleWithIndex.type === 'video' && options.onVideoSample) {
+ const timestamp = sampleWithIndex.samplePosition.offset;
options.onVideoSample({
bytes,
- start: sampleWithIndex.samplePosition.offset,
- offset: data.counter.getOffset(),
+ timestamp,
+ duration: data.counter.getOffset() - timestamp,
trackId: sampleWithIndex.trackId,
+ type: sampleWithIndex.samplePosition.isKeyframe ? 'key' : 'delta',
});
}
diff --git a/packages/media-parser/src/boxes/iso-base-media/process-box.ts b/packages/media-parser/src/boxes/iso-base-media/process-box.ts
index 7d7088703f..edee36ea03 100644
--- a/packages/media-parser/src/boxes/iso-base-media/process-box.ts
+++ b/packages/media-parser/src/boxes/iso-base-media/process-box.ts
@@ -19,6 +19,7 @@ import {parseMebx} from './stsd/mebx';
import {parseStco} from './stsd/stco';
import {parseStsc} from './stsd/stsc';
import {parseStsd} from './stsd/stsd';
+import {parseStss} from './stsd/stss';
import {parseStsz} from './stsd/stsz';
import {parseStts} from './stts/stts';
import {parseTkhd} from './tkhd';
@@ -216,6 +217,21 @@ const processBox = ({
};
}
+ if (boxType === 'stss') {
+ const box = parseStss({
+ iterator,
+ offset: fileOffset,
+ boxSize,
+ });
+
+ return {
+ type: 'complete',
+ box,
+ size: boxSize,
+ skipTo: null,
+ };
+ }
+
if (boxType === 'stsc') {
const box = parseStsc({
iterator,
diff --git a/packages/media-parser/src/boxes/iso-base-media/stsd/stss.ts b/packages/media-parser/src/boxes/iso-base-media/stsd/stss.ts
new file mode 100644
index 0000000000..be85eaaedc
--- /dev/null
+++ b/packages/media-parser/src/boxes/iso-base-media/stsd/stss.ts
@@ -0,0 +1,47 @@
+import type {BufferIterator} from '../../../buffer-iterator';
+import type {BaseBox} from '../base-type';
+
+export interface StssBox extends BaseBox {
+ type: 'stss-box';
+ version: number;
+ flags: number[];
+ sampleNumber: number[];
+}
+
+export const parseStss = ({
+ iterator,
+ offset,
+ boxSize,
+}: {
+ iterator: BufferIterator;
+ offset: number;
+ boxSize: number;
+}): StssBox => {
+ const version = iterator.getUint8();
+ if (version !== 0) {
+ throw new Error(`Unsupported STSS version ${version}`);
+ }
+
+ const flags = iterator.getSlice(3);
+ const sampleCount = iterator.getUint32();
+
+ const sampleNumber: number[] = [];
+ for (let i = 0; i < sampleCount; i++) {
+ sampleNumber.push(iterator.getUint32());
+ }
+
+ const bytesRemainingInBox = boxSize - (iterator.counter.getOffset() - offset);
+
+ if (bytesRemainingInBox > 0) {
+ throw new Error(`Unexpected bytes remaining in box stss`);
+ }
+
+ return {
+ type: 'stss-box',
+ version,
+ flags: [...flags],
+ sampleNumber,
+ boxSize,
+ offset,
+ };
+};
diff --git a/packages/media-parser/src/get-sample-positions.ts b/packages/media-parser/src/get-sample-positions.ts
index b92866da98..07f7a92aad 100644
--- a/packages/media-parser/src/get-sample-positions.ts
+++ b/packages/media-parser/src/get-sample-positions.ts
@@ -1,20 +1,24 @@
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';
export type SamplePosition = {
offset: number;
size: number;
+ isKeyframe: boolean;
};
export const getSamplePositions = ({
stcoBox,
stszBox,
stscBox,
+ stssBox,
}: {
stcoBox: StcoBox;
stszBox: StszBox;
stscBox: StscBox;
+ stssBox: StssBox | null;
}) => {
const chunks = stcoBox.entries;
const samples: SamplePosition[] = [];
@@ -37,9 +41,14 @@ export const getSamplePositions = ({
? stszBox.sampleSize
: stszBox.entries[samples.length];
+ const isKeyframe = stssBox
+ ? stssBox.sampleNumber.includes(samples.length + 1)
+ : true;
+
samples.push({
offset: chunks[i] + offsetInThisChunk,
size,
+ isKeyframe,
});
offsetInThisChunk += size;
}
diff --git a/packages/media-parser/src/get-tracks.ts b/packages/media-parser/src/get-tracks.ts
index 33453e34f9..0c50edf03e 100644
--- a/packages/media-parser/src/get-tracks.ts
+++ b/packages/media-parser/src/get-tracks.ts
@@ -9,6 +9,7 @@ import {
getMvhdBox,
getStcoBox,
getStscBox,
+ getStssBox,
getStszBox,
getTkhdBox,
getTraks,
@@ -73,6 +74,7 @@ export const getTracks = (
const stszBox = getStszBox(track);
const stcoBox = getStcoBox(track);
const stscBox = getStscBox(track);
+ const stssBox = getStssBox(track);
const tkhdBox = getTkhdBox(track);
const videoDescriptors = getVideoDescriptors(track);
@@ -96,6 +98,7 @@ export const getTracks = (
stcoBox,
stscBox,
stszBox,
+ stssBox,
});
if (trakBoxContainsAudio(track)) {
diff --git a/packages/media-parser/src/parse-result.ts b/packages/media-parser/src/parse-result.ts
index 7299d7b376..b5498d3204 100644
--- a/packages/media-parser/src/parse-result.ts
+++ b/packages/media-parser/src/parse-result.ts
@@ -11,6 +11,7 @@ import type {MebxBox} from './boxes/iso-base-media/stsd/mebx';
import type {StcoBox} from './boxes/iso-base-media/stsd/stco';
import type {StscBox} from './boxes/iso-base-media/stsd/stsc';
import type {StsdBox} from './boxes/iso-base-media/stsd/stsd';
+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';
import type {TkhdBox} from './boxes/iso-base-media/tkhd';
@@ -45,7 +46,8 @@ export type IsoBaseMediaBox =
| StscBox
| AvccBox
| HvccBox
- | VoidBox;
+ | VoidBox
+ | StssBox;
export type AnySegment = MatroskaSegment | IsoBaseMediaBox;
diff --git a/packages/media-parser/src/test/stream-samples.test.ts b/packages/media-parser/src/test/stream-samples.test.ts
index f9b6669fed..c20a5ac42a 100644
--- a/packages/media-parser/src/test/stream-samples.test.ts
+++ b/packages/media-parser/src/test/stream-samples.test.ts
@@ -26,42 +26,52 @@ test('Stream samples', async () => {
{
offset: 1637,
size: 4834,
+ isKeyframe: true,
},
{
offset: 6471,
size: 437,
+ isKeyframe: false,
},
{
offset: 7868,
size: 698,
+ isKeyframe: false,
},
{
offset: 10486,
size: 595,
+ isKeyframe: false,
},
{
offset: 12041,
size: 548,
+ isKeyframe: false,
},
{
offset: 14509,
size: 611,
+ isKeyframe: false,
},
{
offset: 16080,
size: 701,
+ isKeyframe: false,
},
{
offset: 17741,
size: 535,
+ isKeyframe: false,
},
{
offset: 20196,
size: 776,
+ isKeyframe: false,
},
{
offset: 21932,
size: 586,
+ isKeyframe: false,
},
],
description,
@@ -76,62 +86,77 @@ test('Stream samples', async () => {
{
offset: 6908,
size: 960,
+ isKeyframe: true,
},
{
offset: 8566,
size: 960,
+ isKeyframe: true,
},
{
offset: 9526,
size: 960,
+ isKeyframe: true,
},
{
offset: 11081,
size: 960,
+ isKeyframe: true,
},
{
offset: 12589,
size: 960,
+ isKeyframe: true,
},
{
offset: 13549,
size: 960,
+ isKeyframe: true,
},
{
offset: 15120,
size: 960,
+ isKeyframe: true,
},
{
offset: 16781,
size: 960,
+ isKeyframe: true,
},
{
offset: 18276,
size: 960,
+ isKeyframe: true,
},
{
offset: 19236,
size: 960,
+ isKeyframe: true,
},
{
offset: 20972,
size: 960,
+ isKeyframe: true,
},
{
offset: 22518,
size: 960,
+ isKeyframe: true,
},
{
offset: 23478,
size: 960,
+ isKeyframe: true,
},
{
offset: 24438,
size: 960,
+ isKeyframe: true,
},
{
offset: 25398,
size: 960,
+ isKeyframe: true,
},
],
description: null,
diff --git a/packages/media-parser/src/traversal.ts b/packages/media-parser/src/traversal.ts
index 9d8aaeab14..a9353b89d3 100644
--- a/packages/media-parser/src/traversal.ts
+++ b/packages/media-parser/src/traversal.ts
@@ -4,6 +4,7 @@ import type {MvhdBox} from './boxes/iso-base-media/mvhd';
import type {StcoBox} from './boxes/iso-base-media/stsd/stco';
import type {StscBox} from './boxes/iso-base-media/stsd/stsc';
import type {StsdBox} from './boxes/iso-base-media/stsd/stsd';
+import type {StssBox} from './boxes/iso-base-media/stsd/stss';
import type {StszBox} from './boxes/iso-base-media/stsd/stsz';
import type {TkhdBox} from './boxes/iso-base-media/tkhd';
import type {TrakBox} from './boxes/iso-base-media/trak/trak';
@@ -164,3 +165,17 @@ export const getStscBox = (trakBox: TrakBox): StscBox | null => {
return stcoBox;
};
+
+export const getStssBox = (trakBox: TrakBox): StssBox | null => {
+ const stblBox = getStblBox(trakBox);
+
+ if (!stblBox || stblBox.type !== 'regular-box') {
+ return null;
+ }
+
+ const stssBox = stblBox.children.find(
+ (b) => b.type === 'stss-box',
+ ) as StssBox | null;
+
+ return stssBox;
+};