🎥 Make videos programmatically with React
set up streaming
Jonny Burger5ff2ade1e133
diff --git a/packages/video-parser/src/boxes/iso-base-media/process-box.ts b/packages/video-parser/src/boxes/iso-base-media/process-box.tsindex 22b8d51531..c37b181990 100644--- a/packages/video-parser/src/boxes/iso-base-media/process-box.ts+++ b/packages/video-parser/src/boxes/iso-base-media/process-box.ts@@ -23,12 +23,19 @@ const processBoxAndSubtract = ({const boxType = new TextDecoder().decode(boxTypeBuffer);- const sub = data.slice(0, boxSize);+ const boxBuffer = data.slice(0, boxSize);const next = data.slice(boxSize);+ if (boxBuffer.byteLength !== boxSize) {+ return {+ type: 'incomplete',+ };+ }+if (boxType === 'ftyp') {return {- box: parseFtyp(sub, fileOffset),+ type: 'complete',+ box: parseFtyp(boxBuffer, fileOffset),next,size: boxSize,};@@ -36,7 +43,8 @@ const processBoxAndSubtract = ({if (boxType === 'mvhd') {return {- box: parseMvhd(sub, fileOffset),+ type: 'complete',+ box: parseMvhd(boxBuffer, fileOffset),next,size: boxSize,};@@ -44,7 +52,8 @@ const processBoxAndSubtract = ({if (boxType === 'tkhd') {return {- box: parseTkhd(sub, fileOffset),+ type: 'complete',+ box: parseTkhd(boxBuffer, fileOffset),next,size: boxSize,};@@ -52,7 +61,8 @@ const processBoxAndSubtract = ({if (boxType === 'stsd') {return {- box: parseStsd(sub, fileOffset),+ type: 'complete',+ box: parseStsd(boxBuffer, fileOffset),next,size: boxSize,};@@ -60,15 +70,19 @@ const processBoxAndSubtract = ({if (boxType === 'mebx') {return {- box: parseMebx(sub, fileOffset),+ type: 'complete',+ box: parseMebx(boxBuffer, fileOffset),next,size: boxSize,};}if (boxType === 'moov') {+ const box = parseMoov(boxBuffer, fileOffset);+return {- box: parseMoov(sub, fileOffset),+ type: 'complete',+ box,next,size: boxSize,};@@ -76,13 +90,14 @@ const processBoxAndSubtract = ({if (boxType === 'trak') {return {- box: parseTrak(sub, fileOffset),+ type: 'complete',+ box: parseTrak(boxBuffer, fileOffset),next,size: boxSize,};}- const childArray = sub.slice(8, boxSize);+ const childArray = boxBuffer.slice(8, boxSize);const children =boxType === 'mdia' ||@@ -94,6 +109,7 @@ const processBoxAndSubtract = ({: [];return {+ type: 'complete',box: {type: 'regular-box',boxType,@@ -115,14 +131,17 @@ export const parseBoxes = (let bytesConsumed = fileOffset;while (remaining.byteLength > 0) {- const {next, box, size} = processBoxAndSubtract({+ const result = processBoxAndSubtract({data: remaining,fileOffset: bytesConsumed,});+ if (result.type === 'incomplete') {+ return boxes;+ }- remaining = next;- boxes.push(box);- bytesConsumed = box.offset + size;+ remaining = result.next;+ boxes.push(result.box);+ bytesConsumed = result.box.offset + result.size;}return boxes;diff --git a/packages/video-parser/src/boxes/webm/parse-webm-header.ts b/packages/video-parser/src/boxes/webm/parse-webm-header.tsindex 766d8ebe03..a897d083bc 100644--- a/packages/video-parser/src/boxes/webm/parse-webm-header.ts+++ b/packages/video-parser/src/boxes/webm/parse-webm-header.ts@@ -1,9 +1,8 @@-import {getArrayBufferIterator} from '../../read-and-increment-offset';+import type {BufferIterator} from '../../read-and-increment-offset';import {expectSegment} from './segments';// Parsing according to https://darkcoding.net/software/reading-mediarecorders-webm-opus-output/-export const parseWebm = (data: Uint8Array) => {- const counter = getArrayBufferIterator(data);+export const parseWebm = (counter: BufferIterator) => {counter.discard(4);const length = counter.getEBML();diff --git a/packages/video-parser/src/parse-video.ts b/packages/video-parser/src/parse-video.tsindex dd2b3fa684..093a211e82 100644--- a/packages/video-parser/src/parse-video.ts+++ b/packages/video-parser/src/parse-video.ts@@ -11,6 +11,7 @@ import type {TkhdBox} from './boxes/iso-base-media/tkhd';import type {TrakBox} from './boxes/iso-base-media/trak/trak';import {parseWebm} from './boxes/webm/parse-webm-header';import type {MatroskaSegment} from './boxes/webm/segments';+import {getArrayBufferIterator} from './read-and-increment-offset';interface RegularBox extends BaseBox {boxType: string;@@ -31,11 +32,16 @@ export type IsoBaseMediaBox =| MoovBox| TrakBox;-export type BoxAndNext = {- box: IsoBaseMediaBox;- next: Uint8Array;- size: number;-};+export type BoxAndNext =+ | {+ type: 'complete';+ box: IsoBaseMediaBox;+ next: Uint8Array;+ size: number;+ }+ | {+ type: 'incomplete';+ };const isoBaseMediaMp4Pattern = Buffer.from('ftyp');const webmPattern = Buffer.from([0x1a, 0x45, 0xdf, 0xa3]);@@ -77,8 +83,27 @@ export const parseVideo = async (}if (matchesPattern(webmPattern)(data.subarray(0, 4))) {- return [parseWebm(new Uint8Array(data))];+ return [parseWebm(getArrayBufferIterator(new Uint8Array(data)))];}return [];};++export const streamVideo = async (url: string) => {+ const res = await fetch(url);+ if (!res.body) {+ throw new Error('No body');+ }++ const reader = res.body.getReader();++ // eslint-disable-next-line no-constant-condition+ while (true) {+ const {done, value} = await reader.read();+ if (done) {+ break;+ }++ console.log(value?.length);+ }+};diff --git a/packages/video-parser/src/test/parse-video.test.ts b/packages/video-parser/src/test/parse-video.test.tsindex 51541fb469..4a8dc43898 100644--- a/packages/video-parser/src/test/parse-video.test.ts+++ b/packages/video-parser/src/test/parse-video.test.ts@@ -22,13 +22,6 @@ if (process.platform !== 'win32') {type: 'regular-box',children: [],},- {- boxSize: 14282275,- boxType: 'mdat',- offset: 40,- type: 'regular-box',- children: [],- },]);});@@ -50,13 +43,6 @@ if (process.platform !== 'win32') {offset: 20,children: [],},- {- boxSize: 39048800,- boxType: 'mdat',- offset: 28,- type: 'regular-box',- children: [],- },]);});@@ -81,13 +67,6 @@ if (process.platform !== 'win32') {type: 'regular-box',children: [],},- {- offset: 40,- boxSize: 73010,- boxType: 'mdat',- children: [],- type: 'regular-box',- },]);});diff --git a/packages/video-parser/src/test/stream.test.ts b/packages/video-parser/src/test/stream.test.tsnew file mode 100644index 0000000000..729c5c5976--- /dev/null+++ b/packages/video-parser/src/test/stream.test.ts@@ -0,0 +1,8 @@+import {test} from 'bun:test';+import {streamVideo} from '../parse-video';++test('Should stream', async () => {+ await streamVideo(+ 'https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4',+ );+});