🎥 Make videos programmatically with React
`@remotion/streaming` package
Jonny Burgerade4f9fdc6ae
diff --git a/packages/streaming/.eslintrc b/packages/streaming/.eslintrcnew file mode 100644index 0000000000..19d2a5fd62--- /dev/null+++ b/packages/streaming/.eslintrc@@ -0,0 +1,3 @@+{+ "extends": "@jonny",+}diff --git a/packages/streaming/.npmignore b/packages/streaming/.npmignorenew file mode 100644index 0000000000..a575358512--- /dev/null+++ b/packages/streaming/.npmignore@@ -0,0 +1,5 @@+src+tsconfig.json+dist/test+.eslintrc.js+.prettierrc.jsdiff --git a/packages/streaming/.prettierrc.js b/packages/streaming/.prettierrc.jsnew file mode 100644index 0000000000..7129f6a51c--- /dev/null+++ b/packages/streaming/.prettierrc.js@@ -0,0 +1,14 @@+module.exports = {+ singleQuote: true,+ bracketSpacing: false,+ useTabs: true,+ overrides: [+ {+ files: ["*.yml"],+ options: {+ singleQuote: false,+ },+ },+ ],+ plugins: [require.resolve("prettier-plugin-organize-imports")],+};diff --git a/packages/streaming/package.json b/packages/streaming/package.jsonnew file mode 100644index 0000000000..24b27e926c--- /dev/null+++ b/packages/streaming/package.json@@ -0,0 +1,36 @@+{+ "name": "@remotion/streaming",+ "version": "4.0.166",+ "description": "Internal streaming helpers",+ "main": "dist",+ "sideEffects": false,+ "scripts": {+ "lint": "eslint src --ext ts,tsx",+ "formatting": "prettier src --check"+ },+ "author": "Jonny Burger <[email protected]>",+ "contributors": [],+ "license": "MIT",+ "repository": {+ "url": "https://github.com/remotion-dev/remotion"+ },+ "bugs": {+ "url": "https://github.com/remotion-dev/remotion/issues"+ },+ "publishConfig": {+ "access": "public"+ },+ "devDependencies": {+ "@jonny/eslint-config": "3.0.281",+ "eslint": "8.56.0",+ "eslint-plugin-10x": "1.5.2",+ "eslint-plugin-react": "7.32.2",+ "eslint-plugin-react-hooks": "4.4.0",+ "prettier": "3.2.5",+ "prettier-plugin-organize-imports": "3.2.4"+ },+ "exports": {+ ".": "./dist/index.js",+ "./package.json": "./package.json"+ }+}diff --git a/packages/streaming/src/index.ts b/packages/streaming/src/index.tsnew file mode 100644index 0000000000..bfc6dc8c54--- /dev/null+++ b/packages/streaming/src/index.ts@@ -0,0 +1,2 @@+export {makeStreamPayloadMessage} from './make-stream-payload-message';+export {makeStreamer} from './make-streamer';diff --git a/packages/streaming/src/make-stream-payload-message.ts b/packages/streaming/src/make-stream-payload-message.tsnew file mode 100644index 0000000000..4645d5e3d7--- /dev/null+++ b/packages/streaming/src/make-stream-payload-message.ts@@ -0,0 +1,49 @@+export const magicWordStr = 'remotion_buffer:';++export const makeStreamPayloadMessage = ({+ status,+ body,+ nonce,+}: {+ nonce: string;+ status: 0 | 1;+ body: Uint8Array;+}): Uint8Array => {+ const nonceArr = new TextEncoder().encode(nonce);+ const magicWordArr = new TextEncoder().encode(magicWordStr);+ const separatorArr = new TextEncoder().encode(':');+ const bodyLengthArr = new TextEncoder().encode(body.length.toString());+ const statusArr = new TextEncoder().encode(String(status));++ // Calculate total length of new Uint8Array+ const totalLength =+ nonceArr.length ++ magicWordArr.length ++ separatorArr.length * 3 ++ bodyLengthArr.length ++ statusArr.length ++ body.length;++ // Create a new Uint8Array to hold all combined parts+ const concat = new Uint8Array(totalLength);++ let offset = 0;++ // Function to append data to concat+ const appendToConcat = (data: Uint8Array) => {+ concat.set(data, offset);+ offset += data.length;+ };++ // Building the final Uint8Array+ appendToConcat(magicWordArr);+ appendToConcat(nonceArr);+ appendToConcat(separatorArr);+ appendToConcat(bodyLengthArr);+ appendToConcat(separatorArr);+ appendToConcat(statusArr);+ appendToConcat(separatorArr);+ appendToConcat(body);++ return concat;+};diff --git a/packages/streaming/src/make-streamer.ts b/packages/streaming/src/make-streamer.tsnew file mode 100644index 0000000000..7c36b72164--- /dev/null+++ b/packages/streaming/src/make-streamer.ts@@ -0,0 +1,197 @@+export const streamingKey = 'remotion_buffer:';++const magicWordStr = 'remotion_buffer:';++export const makeStreamer = (+ onMessage: (+ statusType: 'success' | 'error',+ nonce: string,+ data: Uint8Array,+ ) => void,+) => {+ const separator = new Uint8Array(magicWordStr.length);+ for (let i = 0; i < magicWordStr.length; i++) {+ separator[i] = magicWordStr.charCodeAt(i);+ }++ let unprocessedBuffers: Uint8Array[] = [];+ let outputBuffer = new Uint8Array(0);+ let missingData: null | {+ dataMissing: number;+ } = null;++ const processInput = () => {+ let separatorIndex = outputBuffer.indexOf(separator[0]); // Start checking for the first byte of the separator+ if (+ separatorIndex === -1 ||+ outputBuffer+ .subarray(separatorIndex, separatorIndex + separator.length)+ .toString() !== separator.toString()+ ) {+ return;+ }++ separatorIndex += separator.length;++ let nonceString = '';+ let lengthString = '';+ let statusString = '';++ // eslint-disable-next-line no-constant-condition+ while (true) {+ if (separatorIndex > outputBuffer.length - 1) {+ return;+ }++ const nextDigit = outputBuffer[separatorIndex];+ separatorIndex++;++ if (nextDigit === 0x3a) {+ break;+ }++ nonceString += String.fromCharCode(nextDigit);+ }++ // eslint-disable-next-line no-constant-condition+ while (true) {+ if (separatorIndex > outputBuffer.length - 1) {+ return;+ }++ const nextDigit = outputBuffer[separatorIndex];+ separatorIndex++;++ if (nextDigit === 0x3a) {+ break;+ }++ lengthString += String.fromCharCode(nextDigit);+ }++ // eslint-disable-next-line no-constant-condition+ while (true) {+ if (separatorIndex > outputBuffer.length - 1) {+ return;+ }++ const nextDigit = outputBuffer[separatorIndex];+ if (nextDigit === 0x3a) {+ break;+ }++ separatorIndex++;++ statusString += String.fromCharCode(nextDigit);+ }++ const length = Number(lengthString);+ const status = Number(statusString);++ const dataLength = outputBuffer.length - separatorIndex - 1;+ if (dataLength < length) {+ missingData = {+ dataMissing: length - dataLength,+ };++ return;+ }++ const data = outputBuffer.subarray(+ separatorIndex + 1,+ separatorIndex + 1 + Number(lengthString),+ );+ onMessage(status === 1 ? 'error' : 'success', nonceString, data);+ missingData = null;++ outputBuffer = outputBuffer.subarray(+ separatorIndex + Number(lengthString) + 1,+ );++ processInput();+ };++ const onData = (data: Uint8Array) => {+ unprocessedBuffers.push(data);+ const separatorIndex = data.indexOf(separator[0]);+ if (separatorIndex === -1) {+ if (missingData) {+ missingData.dataMissing -= data.length;+ }++ if (!missingData || missingData.dataMissing > 0) {+ return;+ }+ }++ unprocessedBuffers.unshift(outputBuffer);+ outputBuffer = new Uint8Array(+ unprocessedBuffers.reduce((acc, val) => acc + val.length, 0),+ );+ let offset = 0;+ for (const buf of unprocessedBuffers) {+ outputBuffer.set(buf, offset);+ offset += buf.length;+ }++ unprocessedBuffers = [];+ processInput();+ };++ return {+ onData,+ getOutputBuffer: () => outputBuffer,+ clear: () => {+ unprocessedBuffers = [];+ outputBuffer = new Uint8Array(0);+ },+ };+};++export const makeStreamPayloadMessage = ({+ status,+ body,+ nonce,+}: {+ nonce: string;+ status: 0 | 1;+ body: Uint8Array;+}): Uint8Array => {+ const nonceArr = new TextEncoder().encode(nonce);+ const magicWordArr = new TextEncoder().encode(magicWordStr);+ const separatorArr = new TextEncoder().encode(':');+ const bodyLengthArr = new TextEncoder().encode(body.length.toString());+ const statusArr = new TextEncoder().encode(String(status));++ // Calculate total length of new Uint8Array+ const totalLength =+ nonceArr.length ++ magicWordArr.length ++ separatorArr.length * 3 ++ bodyLengthArr.length ++ statusArr.length ++ body.length;++ // Create a new Uint8Array to hold all combined parts+ const concat = new Uint8Array(totalLength);++ let offset = 0;++ // Function to append data to concat+ const appendToConcat = (data: Uint8Array) => {+ concat.set(data, offset);+ offset += data.length;+ };++ // Building the final Uint8Array+ appendToConcat(magicWordArr);+ appendToConcat(nonceArr);+ appendToConcat(separatorArr);+ appendToConcat(bodyLengthArr);+ appendToConcat(separatorArr);+ appendToConcat(statusArr);+ appendToConcat(separatorArr);+ appendToConcat(body);++ return concat;+};diff --git a/packages/streaming/tsconfig.json b/packages/streaming/tsconfig.jsonnew file mode 100644index 0000000000..414a4523af--- /dev/null+++ b/packages/streaming/tsconfig.json@@ -0,0 +1,11 @@+{+ "extends": "../tsconfig.settings.json",+ "compilerOptions": {+ "composite": true,+ "rootDir": "./src",+ "outDir": "dist",+ "skipLibCheck": true+ },+ "include": ["src"],+ "references": []+}diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex fe2609e363..efeff5c5a3 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -1921,6 +1921,30 @@ importers:specifier: 18.3.1version: 18.3.1([email protected])+ packages/streaming:+ devDependencies:+ '@jonny/eslint-config':+ specifier: 3.0.281+ version: 3.0.281([email protected])([email protected])+ eslint:+ specifier: 8.56.0+ version: 8.56.0+ eslint-plugin-10x:+ specifier: 1.5.2+ version: 1.5.2([email protected])+ eslint-plugin-react:+ specifier: 7.32.2+ version: 7.32.2([email protected])+ eslint-plugin-react-hooks:+ specifier: 4.4.0+ version: 4.4.0([email protected])+ prettier:+ specifier: 3.2.5+ version: 3.2.5+ prettier-plugin-organize-imports:+ specifier: 3.2.4+ version: 3.2.4([email protected])([email protected])+packages/studio:dependencies:'@remotion/media-utils':diff --git a/tsconfig.json b/tsconfig.jsonindex 0e6f3108f4..02238066b7 100644--- a/tsconfig.json+++ b/tsconfig.json@@ -138,6 +138,9 @@{"path": "./packages/enable-scss"},+ {+ "path": "./packages/streaming"+ },{"path": "./packages/zod-types"}