🎥 Make videos programmatically with React
make a video parser in JS
Jonny Burgerc2cc945074af
diff --git a/packages/video-parser/package.json b/packages/video-parser/package.jsonnew file mode 100644index 0000000000..060dd9a301--- /dev/null+++ b/packages/video-parser/package.json@@ -0,0 +1,60 @@+{+ "name": "@remotion/cli",+ "version": "4.0.104",+ "description": "CLI for Remotion",+ "main": "dist/index.js",+ "sideEffects": false,+ "bin": {+ "remotion": "remotion-cli.js"+ },+ "scripts": {+ "formatting": "prettier src --check",+ "lint": "eslint src --ext ts,tsx",+ "test": "vitest --run",+ "build": "tsc -d",+ "watch": "tsc -w"+ },+ "repository": {+ "url": "https://github.com/remotion-dev/remotion"+ },+ "bugs": {+ "url": "https://github.com/remotion-dev/remotion/issues"+ },+ "exports": {+ ".": "./dist/index.js",+ "./config": "./dist/config/index.js",+ "./package.json": "./package.json"+ },+ "typesVersions": {+ ">=1.0": {+ "config": [+ "dist/config/index.d.ts"+ ]+ }+ },+ "author": "Jonny Burger <[email protected]>",+ "license": "SEE LICENSE IN LICENSE.md",+ "dependencies": {},+ "devDependencies": {+ "@jonny/eslint-config": "3.0.276",+ "@types/bun": "^1.0.4",+ "@types/node": "18.14.6",+ "eslint": "8.42.0",+ "eslint-plugin-10x": "1.5.2",+ "eslint-plugin-react": "7.32.2",+ "eslint-plugin-react-hooks": "4.4.0",+ "prettier": "3.1.1",+ "prettier-plugin-organize-imports": "3.2.4",+ "vitest": "0.31.1"+ },+ "keywords": [+ "remotion",+ "ffmpeg",+ "video",+ "react",+ "player"+ ],+ "publishConfig": {+ "access": "public"+ }+}diff --git a/packages/video-parser/src/index.ts b/packages/video-parser/src/index.tsnew file mode 100644index 0000000000..5dd29b0b6b--- /dev/null+++ b/packages/video-parser/src/index.ts@@ -0,0 +1 @@+export { parseVideo } from "./parse-video";diff --git a/packages/video-parser/src/parse-video.tsx b/packages/video-parser/src/parse-video.tsxnew file mode 100644index 0000000000..1bab570287--- /dev/null+++ b/packages/video-parser/src/parse-video.tsx@@ -0,0 +1,84 @@+import { createReadStream } from "fs";++const fourByteToNumber = (data: Buffer, from: number) => {+ return (+ (data[from + 0] << 24) |+ (data[from + 1] << 16) |+ (data[from + 2] << 8) |+ data[from + 3]+ );+};++const isoBaseMediaMp4Pattern = Buffer.from("ftyp");++const parseFtyp = (data: Buffer) => {+ const majorBrand = data.subarray(8, 12).toString("utf-8");++ const minorVersion = fourByteToNumber(data, 12);++ const rest = data.subarray(16);+ let types = rest.length / 4;+ let compatibleBrands: string[] = [];+ for (let i = 0; i < types; i++) {+ const fourBytes = rest.subarray(i * 4, i * 4 + 4);+ compatibleBrands.push(fourBytes.toString("utf-8"));+ }++ return {+ type: "ftyp-box",+ majorBrand,+ minorVersion,+ compatibleBrands,+ };+};++const getExtraDataFromBox = (box: Buffer, type: string) => {+ if (type === "ftyp") {+ return parseFtyp(box);+ }+};++const processBoxAndSubtract = (data: Buffer) => {+ const boxSize = fourByteToNumber(data, 0);+ const boxType = data.subarray(4, 8).toString("utf-8");++ const next = data.subarray(boxSize);++ return {+ next,+ boxType,+ boxSize,+ extraData: getExtraDataFromBox(data.subarray(0, boxSize), boxType),+ };+};++const matchesPattern = (pattern: Buffer) => {+ return (data: Buffer) => {+ return pattern.every((value, index) => data[index] === value);+ };+};++export const parseVideo = async (src: string) => {+ const stream = createReadStream(src, { end: 4 * 1024 - 1 });+ const data = await new Promise<Buffer>((resolve, reject) => {+ stream.on("data", (chunk) => {+ resolve(chunk as Buffer);+ });++ stream.on("error", (err) => {+ reject(err);+ });+ });++ if (matchesPattern(isoBaseMediaMp4Pattern)(data.subarray(4, 8))) {+ let boxes = [];+ let remaining = data;+ while (remaining.length > 0) {+ const { next, boxType, boxSize, extraData } =+ processBoxAndSubtract(remaining);+ remaining = next;+ boxes.push({ boxType, boxSize, extraData });+ }+ return boxes;+ }+};diff --git a/packages/video-parser/src/test/example-videos.ts b/packages/video-parser/src/test/example-videos.tsnew file mode 100644index 0000000000..d28e61d296--- /dev/null+++ b/packages/video-parser/src/test/example-videos.ts@@ -0,0 +1,27 @@+import path from "node:path";++const examplePackage = path.join(__dirname, "..", "..", "..", "example");+const docsPackage = path.join(__dirname, "..", "..", "..", "docs");++export const exampleVideos = {+ bigBuckBunny: path.join(examplePackage, "public/bigbuckbunny.mp4"),+ transparentWebm: path.join(docsPackage, "/static/img/transparent-video.webm"),+ framerWithoutFileExtension: path.join(+ examplePackage,+ "public",+ "framermp4withoutfileextension"+ ),+ corrupted: path.join(examplePackage, "public", "corrupted.mp4"),+ customDar: path.join(examplePackage, "public", "custom-dar.mp4"),+ screenrecording: path.join(examplePackage, "public", "quick.mov"),+ nofps: path.join(examplePackage, "public", "nofps.webm"),+ variablefps: path.join(examplePackage, "public", "variablefps.webm"),+ zerotimestamp: path.join(examplePackage, "public", "zero-timestamp.mp4"),+ webcam: path.join(examplePackage, "public", "webcam.webm"),+ iphonevideo: path.join(examplePackage, "public", "iphonevideo.mov"),+ av1: path.join(examplePackage, "public", "av1.webm"),+ framer24fps: path.join(examplePackage, "public", "framer-24fps.mp4"),+ music: path.join(examplePackage, "public", "music.mp3"),+ notavideo: path.join(examplePackage, "public", "giphy.gif"),+ notafile: path.join(examplePackage, "public", "doesnotexist"),+};diff --git a/packages/video-parser/src/test/parse-video.test.ts b/packages/video-parser/src/test/parse-video.test.tsnew file mode 100644index 0000000000..4cdb02cf38--- /dev/null+++ b/packages/video-parser/src/test/parse-video.test.ts@@ -0,0 +1,29 @@+import { test, expect } from "bun:test";+import { parseVideo } from "../parse-video";+import { exampleVideos } from "./example-videos";++test("Parse a video", async () => {+ const data = await parseVideo(exampleVideos.bigBuckBunny);+ expect(data).toEqual([+ {+ boxSize: 32,+ boxType: "ftyp",+ extraData: {+ type: "ftyp-box",+ majorBrand: "isom",+ minorVersion: 512,+ compatibleBrands: ["isom", "iso2", "avc1", "mp41"],+ },+ },+ {+ boxSize: 8,+ boxType: "free",+ extraData: undefined,+ },+ {+ boxSize: 14282275,+ boxType: "mdat",+ extraData: undefined,+ },+ ]);+});diff --git a/packages/video-parser/tsconfig.json b/packages/video-parser/tsconfig.jsonnew file mode 100644index 0000000000..66a04633f9--- /dev/null+++ b/packages/video-parser/tsconfig.json@@ -0,0 +1,9 @@+{+ "extends": "../tsconfig.settings.json",+ "compilerOptions": {+ "rootDir": "src",+ "outDir": "./dist",+ "skipLibCheck": true+ },+ "references": []+}diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 2dd169728e..b2eba259e6 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -378,7 +378,7 @@ importers:version: link:../bundler'@remotion/cli':specifier: workspace:*- version: link:../cli+ version: link:../video-parser'@remotion/renderer':specifier: workspace:*version: link:../renderer@@ -601,7 +601,7 @@ importers:version: link:../bundler'@remotion/cli':specifier: workspace:*- version: link:../cli+ version: link:../video-parser'@remotion/cloudrun':specifier: workspace:*version: link:../cloudrun@@ -829,7 +829,7 @@ importers:version: link:../bundler'@remotion/cli':specifier: workspace:*- version: link:../cli+ version: link:../video-parser'@remotion/cloudrun':specifier: workspace:*version: link:../cloudrun@@ -986,7 +986,7 @@ importers:dependencies:'@remotion/cli':specifier: workspace:*- version: link:../cli+ version: link:../video-parserreact:specifier: ^18.2.0version: 18.2.0@@ -1091,7 +1091,7 @@ importers:version: link:../bundler'@remotion/cli':specifier: workspace:*- version: link:../cli+ version: link:../video-parser'@remotion/cloudrun':specifier: workspace:*version: link:../cloudrun@@ -1193,7 +1193,7 @@ importers:version: link:../bundler'@remotion/cli':specifier: workspace:*- version: link:../cli+ version: link:../video-parser'@remotion/renderer':specifier: workspace:*version: link:../renderer@@ -2161,6 +2161,39 @@ importers:specifier: 0.31.1version: 0.31.1([email protected])+ packages/video-parser:+ devDependencies:+ '@jonny/eslint-config':+ specifier: 3.0.276+ version: 3.0.276([email protected])([email protected])+ '@types/bun':+ specifier: ^1.0.4+ version: 1.0.4+ '@types/node':+ specifier: 18.14.6+ version: 18.14.6+ eslint:+ specifier: 8.42.0+ version: 8.42.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.1.1+ version: 3.1.1+ prettier-plugin-organize-imports:+ specifier: 3.2.4+ version: 3.2.4([email protected])([email protected])+ vitest:+ specifier: 0.31.1+ version: 0.31.1([email protected])+packages/zod-types:dependencies:remotion:@@ -3842,7 +3875,7 @@ packages:debug: 4.3.4([email protected])lodash.debounce: 4.0.8resolve: 1.22.2- semver: 6.3.0+ semver: 6.3.1transitivePeerDependencies:- supports-colordev: false@@ -9809,6 +9842,12 @@ packages:bun-types: 1.0.24dev: true+ /@types/[email protected]:+ resolution: {integrity: sha512-2DO7sqwtpko3d3XP2kLpJsOkV12sSRt8cFR955JVB60m1DiXE56T+gJq+DcCczQ5khxgCDQKkyBRlgg5VH33Dw==}+ dependencies:+ bun-types: 1.0.25+ dev: true+/@types/[email protected]:resolution: {integrity: sha512-B3xVo+dlKM6nnKTcmm5ZtY/OL8bOAOd2Olee9M1zft65ox50OzjEHW91sDiU9j6cvW8Ejg1/Qkf4xd2kugApUA==}dependencies:@@ -11056,14 +11095,6 @@ packages:acorn: ^6.0.0 || ^7.0.0 || ^8.0.0dependencies:acorn: 8.11.3- dev: false-- resolution: {integrity: sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==}- peerDependencies:- acorn: ^6.0.0 || ^7.0.0 || ^8.0.0- dependencies:- acorn: 8.8.2resolution: {integrity: sha512-k+iyHEuPgSw6SbuDpGQM+06HQUa04DZ3o+F6CSzXMvvI5KMvnaEqXe+YVe555R9nn6GPt404fos4wcgpw12SDA==}@@ -11550,7 +11581,7 @@ packages:'@babel/compat-data': 7.19.1'@babel/core': 7.18.6'@babel/helper-define-polyfill-provider': 0.3.3(@babel/[email protected])- semver: 6.3.0+ semver: 6.3.1transitivePeerDependencies:- supports-colordev: false@@ -11867,6 +11898,14 @@ packages:undici-types: 5.28.2dev: true+ resolution: {integrity: sha512-9lxeUR/OJsvlZH4GOWteiAdx7ikrSxCUX7Rr0JJux+DrR3LejouVLxIZnTeQ3UPAZovvSgKivWeHPJ2wlo7/Kg==}+ dependencies:+ '@types/node': 18.14.6+ '@types/ws': 8.5.3+ undici-types: 5.28.2+ dev: true+resolution: {integrity: sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==}engines: {node: '>=10.16.0'}@@ -13910,7 +13949,7 @@ packages:object.values: 1.1.6prop-types: 15.8.1resolve: 2.0.0-next.4- semver: 6.3.0+ semver: 6.3.1string.prototype.matchall: 4.0.8@@ -14009,8 +14048,8 @@ packages:resolution: {integrity: sha512-7OASN1Wma5fum5SrNhFMAMJxOUAbhyfQ8dQ//PJaJbNw0URTPWqIghHWt1MmAANKhHZIYOHruW4Kw4ruUWOdGw==}engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0}dependencies:- acorn: 8.8.2- acorn-jsx: 5.3.2([email protected])+ acorn: 8.11.3+ acorn-jsx: 5.3.2([email protected])eslint-visitor-keys: 3.4.1@@ -18019,7 +18058,7 @@ packages:got: 9.6.0registry-auth-token: 4.2.1registry-url: 5.1.0- semver: 6.3.0+ semver: 6.3.1dev: false@@ -20394,7 +20433,7 @@ packages:resolution: {integrity: sha512-GX0Ix/CJcHyB8c4ykpHGIAvLyOwOobtM/8d+TQkAd81/bEjgPHrfba41Vpesr7jX/t8Uh+R3EX9eAS5be+jQYg==}engines: {node: '>=8'}dependencies:- semver: 6.3.0+ semver: 6.3.1dev: false@@ -20409,7 +20448,6 @@ packages:resolution: {integrity: sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==}hasBin: true- dev: trueresolution: {integrity: sha512-+GB6zVA9LWh6zovYQLALHwv5rb2PHGlJi3lfiqIHxR0uuwCgefcOJc59v9fv1w8GbStwxuuqqAjI9NMAOOgq1A==}