🎥 Make videos programmatically with React
nice
Jonny Burgerd9292cb1374b
diff --git a/packages/media-parser/src/errors.ts b/packages/media-parser/src/errors.tsindex 49019dda64..19da69d879 100644--- a/packages/media-parser/src/errors.ts+++ b/packages/media-parser/src/errors.ts@@ -10,14 +10,21 @@ export class IsAGifError extends Error {}type ImageType = 'png' | 'jpeg' | 'bmp' | 'webp';+type Dimensions = {width: number; height: number};export class IsAnImageError extends Error {public imageType: ImageType;+ public dimensions: Dimensions | null;- constructor(message: string, imageType: ImageType) {+ constructor(+ message: string,+ imageType: ImageType,+ dimensions: Dimensions | null,+ ) {super(message);this.name = 'IsAnImageError';this.imageType = imageType;+ this.dimensions = dimensions;if (Error.captureStackTrace) {Error.captureStackTrace(this, IsAnImageError);diff --git a/packages/media-parser/src/file-types.ts b/packages/media-parser/src/file-types.tsdeleted file mode 100644index c62f18e3c0..0000000000--- a/packages/media-parser/src/file-types.ts+++ /dev/null@@ -1,106 +0,0 @@-import {webmPattern} from './boxes/webm/make-header';--const matchesPattern = (pattern: Uint8Array) => {- return (data: Uint8Array) => {- return pattern.every((value, index) => data[index] === value);- };-};--const isRiff = (data: Uint8Array) => {- const riffPattern = new Uint8Array([0x52, 0x49, 0x46, 0x46]);- return matchesPattern(riffPattern)(data.subarray(0, 4));-};--const isWebm = (data: Uint8Array) => {- return matchesPattern(webmPattern)(data.subarray(0, 4));-};--const isIsoBaseMedia = (data: Uint8Array) => {- const isoBaseMediaMp4Pattern = new TextEncoder().encode('ftyp');-- return matchesPattern(isoBaseMediaMp4Pattern)(data.subarray(4, 8));-};--const isTransportStream = (data: Uint8Array) => {- return data[0] === 0x47 && data[188] === 0x47;-};--const isMp3 = (data: Uint8Array) => {- const mpegPattern = new Uint8Array([0xff, 0xf3, 0xe4, 0x64]);-- return matchesPattern(mpegPattern)(data.subarray(0, 4));-};--const isGif = (data: Uint8Array) => {- const gifPattern = new Uint8Array([0x47, 0x49, 0x46, 0x38]);-- return matchesPattern(gifPattern)(data.subarray(0, 4));-};--const isPng = (data: Uint8Array) => {- const pngPattern = new Uint8Array([0x89, 0x50, 0x4e, 0x47]);-- return matchesPattern(pngPattern)(data.subarray(0, 4));-};--const isBmp = (data: Uint8Array) => {- const bmpPattern = new Uint8Array([0x42, 0x4d]);-- return matchesPattern(bmpPattern)(data.subarray(0, 2));-};--const isJpeg = (data: Uint8Array) => {- const jpegPattern = new Uint8Array([0xff, 0xd8]);-- return matchesPattern(jpegPattern)(data.subarray(0, 2));-};--const isWebp = (data: Uint8Array) => {- const webpPattern = new Uint8Array([0x52, 0x49, 0x46, 0x46]);-- return matchesPattern(webpPattern)(data.subarray(0, 4));-};--export const detectFileType = (data: Uint8Array) => {- if (isWebp(data)) {- return 'webp';- }-- if (isRiff(data)) {- return 'riff';- }-- if (isWebm(data)) {- return 'webm';- }-- if (isIsoBaseMedia(data)) {- return 'iso-base-media';- }-- if (isTransportStream(data)) {- return 'transport-stream';- }-- if (isMp3(data)) {- return 'mp3';- }-- if (isGif(data)) {- return 'gif';- }-- if (isPng(data)) {- return 'png';- }-- if (isBmp(data)) {- return 'bmp';- }-- if (isJpeg(data)) {- return 'jpeg';- }-- return 'unknown';-};diff --git a/packages/media-parser/src/file-types/bmp.ts b/packages/media-parser/src/file-types/bmp.tsnew file mode 100644index 0000000000..391699d6e9--- /dev/null+++ b/packages/media-parser/src/file-types/bmp.ts@@ -0,0 +1,33 @@+import type {Dimensions} from '../get-dimensions';+import {matchesPattern} from './detect-file-type';++function getBmpDimensions(+ bmpData: Uint8Array,+): {width: number; height: number} | null {+ if (bmpData.length < 26) {+ return null;+ }++ const view = new DataView(bmpData.buffer, bmpData.byteOffset);++ return {+ width: view.getUint32(18, true),+ height: Math.abs(view.getInt32(22, true)),+ };+}++export const isBmp = (data: Uint8Array): BmpType | null => {+ const bmpPattern = new Uint8Array([0x42, 0x4d]);++ if (matchesPattern(bmpPattern)(data.subarray(0, 2))) {+ const bmp = getBmpDimensions(data);+ return {dimensions: bmp, type: 'bmp'};+ }++ return null;+};++export type BmpType = {+ type: 'bmp';+ dimensions: Dimensions | null;+};diff --git a/packages/media-parser/src/file-types/detect-file-type.ts b/packages/media-parser/src/file-types/detect-file-type.tsnew file mode 100644index 0000000000..77788c1daf--- /dev/null+++ b/packages/media-parser/src/file-types/detect-file-type.ts@@ -0,0 +1,101 @@+import {webmPattern} from '../boxes/webm/make-header';+import type {BmpType} from './bmp';+import type {JpegType} from './jpeg';++export const matchesPattern = (pattern: Uint8Array) => {+ return (data: Uint8Array) => {+ return pattern.every((value, index) => data[index] === value);+ };+};++export const isRiff = (data: Uint8Array) => {+ const riffPattern = new Uint8Array([0x52, 0x49, 0x46, 0x46]);+ return matchesPattern(riffPattern)(data.subarray(0, 4));+};++export const isWebm = (data: Uint8Array) => {+ return matchesPattern(webmPattern)(data.subarray(0, 4));+};++export const isIsoBaseMedia = (data: Uint8Array) => {+ const isoBaseMediaMp4Pattern = new TextEncoder().encode('ftyp');++ return matchesPattern(isoBaseMediaMp4Pattern)(data.subarray(4, 8));+};++export const isTransportStream = (data: Uint8Array) => {+ return data[0] === 0x47 && data[188] === 0x47;+};++export const isMp3 = (data: Uint8Array) => {+ const mpegPattern = new Uint8Array([0xff, 0xf3, 0xe4, 0x64]);++ return matchesPattern(mpegPattern)(data.subarray(0, 4));+};++export const isGif = (data: Uint8Array) => {+ const gifPattern = new Uint8Array([0x47, 0x49, 0x46, 0x38]);++ return matchesPattern(gifPattern)(data.subarray(0, 4));+};++export const isPng = (data: Uint8Array) => {+ const pngPattern = new Uint8Array([0x89, 0x50, 0x4e, 0x47]);++ return matchesPattern(pngPattern)(data.subarray(0, 4));+};++export const isWebp = (data: Uint8Array) => {+ const webpPattern = new Uint8Array([0x52, 0x49, 0x46, 0x46]);++ return matchesPattern(webpPattern)(data.subarray(0, 4));+};++export type WebpType = {+ type: 'webp';+};++export type RiffType = {+ type: 'riff';+};++export type WebmType = {+ type: 'webm';+};++export type IsoBaseMediaType = {+ type: 'iso-base-media';+};++export type TransportStreamType = {+ type: 'transport-stream';+};++export type Mp3Type = {+ type: 'mp3';+};++export type GifType = {+ type: 'gif';+};++export type PngType = {+ type: 'png';+};++export type UnknownType = {+ type: 'unknown';+};++export type FileType =+ | JpegType+ | WebpType+ | RiffType+ | WebmType+ | IsoBaseMediaType+ | TransportStreamType+ | Mp3Type+ | GifType+ | PngType+ | BmpType+ | UnknownType;diff --git a/packages/media-parser/src/file-types/index.ts b/packages/media-parser/src/file-types/index.tsnew file mode 100644index 0000000000..2b8571e925--- /dev/null+++ b/packages/media-parser/src/file-types/index.ts@@ -0,0 +1,59 @@+import {isBmp} from './bmp';+import type {FileType} from './detect-file-type';+import {+ isGif,+ isIsoBaseMedia,+ isMp3,+ isPng,+ isRiff,+ isTransportStream,+ isWebm,+ isWebp,+} from './detect-file-type';+import {isJpeg} from './jpeg';++export const detectFileType = (data: Uint8Array): FileType => {+ if (isWebp(data)) {+ return {type: 'webp'};+ }++ if (isRiff(data)) {+ return {type: 'riff'};+ }++ if (isWebm(data)) {+ return {type: 'webm'};+ }++ if (isIsoBaseMedia(data)) {+ return {type: 'iso-base-media'};+ }++ if (isTransportStream(data)) {+ return {type: 'transport-stream'};+ }++ if (isMp3(data)) {+ return {type: 'mp3'};+ }++ if (isGif(data)) {+ return {type: 'gif'};+ }++ if (isPng(data)) {+ return {type: 'png'};+ }++ const bmp = isBmp(data);+ if (bmp) {+ return bmp;+ }++ const jpeg = isJpeg(data);+ if (jpeg) {+ return jpeg;+ }++ return {type: 'unknown'};+};diff --git a/packages/media-parser/src/file-types/jpeg.ts b/packages/media-parser/src/file-types/jpeg.tsnew file mode 100644index 0000000000..aeb3a65455--- /dev/null+++ b/packages/media-parser/src/file-types/jpeg.ts@@ -0,0 +1,58 @@+import {matchesPattern} from './detect-file-type';++export function getJpegDimensions(+ data: Uint8Array,+): {width: number; height: number} | null {+ let offset = 0;++ // Helper function to read a 16-bit big-endian integer+ function readUint16BE(o: number): number {+ return (data[o] << 8) | data[o + 1];+ }++ // Skip the Start of Image (SOI) marker+ if (readUint16BE(offset) !== 0xffd8) {+ return null; // Not a valid JPEG file+ }++ offset += 2;++ while (offset < data.length) {+ if (data[offset] === 0xff) {+ const marker = data[offset + 1];+ if (marker === 0xc0 || marker === 0xc2) {+ // SOF0 or SOF2+ const height = readUint16BE(offset + 5);+ const width = readUint16BE(offset + 7);+ return {width, height};+ }++ const length = readUint16BE(offset + 2);+ offset += length + 2; // Move to the next marker+ } else {+ offset++;+ }+ }++ return null; // Return null if dimensions are not found+}++export const isJpeg = (data: Uint8Array): JpegType | null => {+ const jpegPattern = new Uint8Array([0xff, 0xd8]);++ const jpeg = matchesPattern(jpegPattern)(data.subarray(0, 2));+ if (!jpeg) {+ return null;+ }++ const dim = getJpegDimensions(data);+ return {dimensions: dim, type: 'jpeg'};+};++export type JpegType = {+ type: 'jpeg';+ dimensions: {+ width: number;+ height: number;+ } | null;+};diff --git a/packages/media-parser/src/parse-video.ts b/packages/media-parser/src/parse-video.tsindex 5fe2880d3e..416aa5506f 100644--- a/packages/media-parser/src/parse-video.ts+++ b/packages/media-parser/src/parse-video.ts@@ -47,12 +47,12 @@ export const parseVideo = ({const fileType = iterator.detectFileType();- if (fileType === 'riff') {+ if (fileType.type === 'riff') {Log.verbose(logLevel, 'Detected RIFF container');return Promise.resolve(parseRiff({iterator, options, fields}));}- if (fileType === 'iso-base-media') {+ if (fileType.type === 'iso-base-media') {Log.verbose(logLevel, 'Detected ISO Base Media container');return parseIsoBaseMediaBoxes({iterator,@@ -67,12 +67,12 @@ export const parseVideo = ({});}- if (fileType === 'webm') {+ if (fileType.type === 'webm') {Log.verbose(logLevel, 'Detected Matroska container');return parseWebm({counter: iterator, parserContext: options, fields});}- if (fileType === 'transport-stream') {+ if (fileType.type === 'transport-stream') {return parseTransportStream({iterator,parserContext: options,@@ -86,26 +86,36 @@ export const parseVideo = ({});}- if (fileType === 'mp3') {+ if (fileType.type === 'mp3') {return Promise.reject(new Error('MP3 files are not yet supported'));}- if (fileType === 'gif') {+ if (fileType.type === 'gif') {return Promise.reject(new IsAGifError('GIF files are not yet supported'));}if (- fileType === 'bmp' ||- fileType === 'jpeg' ||- fileType === 'png' ||- fileType === 'webp'+ fileType.type === 'bmp' ||+ fileType.type === 'jpeg' ||+ fileType.type === 'png' ||+ fileType.type === 'webp') {+ if (fileType.type === 'jpeg' || fileType.type === 'bmp') {+ return Promise.reject(+ new IsAnImageError(+ 'Image files are not supported',+ fileType.type,+ fileType.dimensions,+ ),+ );+ }+return Promise.reject(- new IsAnImageError('Image files are not supported', fileType),+ new IsAnImageError('Image files are not supported', fileType.type, null),);}- if (fileType === 'unknown') {+ if (fileType.type === 'unknown') {return Promise.reject(new Error('Unknown file format'));}diff --git a/packages/media-parser/src/test/foreign-types.test.ts b/packages/media-parser/src/test/foreign-types.test.tsindex 6f9b602cf5..bdaa02e461 100644--- a/packages/media-parser/src/test/foreign-types.test.ts+++ b/packages/media-parser/src/test/foreign-types.test.ts@@ -37,6 +37,7 @@ test('Should throw IsAnImageError for a jpeg', async () => {} catch (e) {if (e instanceof IsAnImageError) {expect(e.imageType).toBe('jpeg');+ expect(e.dimensions).toEqual({height: 400, width: 400});return;}}@@ -53,6 +54,7 @@ test('Should throw IsAnImageError for a bmp', async () => {} catch (e) {if (e instanceof IsAnImageError) {expect(e.imageType).toBe('bmp');+ expect(e.dimensions).toEqual({width: 600, height: 300});return;}}