🎥 Make videos programmatically with React
parse wav
Jonny Burger24ded5de410e
diff --git a/packages/docs/docs/media-parser/foreign-file-types.mdx b/packages/docs/docs/media-parser/foreign-file-types.mdxindex f1602f5bfe..fba1d9474c 100644--- a/packages/docs/docs/media-parser/foreign-file-types.mdx+++ b/packages/docs/docs/media-parser/foreign-file-types.mdx@@ -29,11 +29,13 @@ try {});} catch (e) {if (e instanceof IsAnImageError) {- console.log('The file is an image of format', e.imageType, e.dimensions);+ console.log('The file is an image of format:', e.imageType, 'dimensions:' e.dimensions);} else if (e instanceof IsAGifError) {console.log('The file is a GIF');} else if (e instanceof IsAPdfError) {console.log('The file is a PDF');+ } else if (e instanceof IsAnUnsupportedAudioTypeError) {+ console.log('The file is an audio file of format:', e.audioType);} else if (e instanceof IsAnUnsupportedFileTypeError) {console.log('The file is of an unsupported type');}@@ -59,6 +61,11 @@ An error if the file is an audio file that `@remotion/media-parser` does not yet- `fileName`: The name of the file - from file name or `Content-Disposition` header.- `sizeInBytes`: The size of the file in bytes.- `mimeType`: The MIME type of the file.+- `audioType`: The type of the audio file - one of `mp3`, `wav` and `aac`.++:::note+`@remotion/media-parser` aims to support these audio formats in the future, in which case they will not throw an error but return audio tracks.+:::### `IsAGifError`diff --git a/packages/example-videos/src/index.ts b/packages/example-videos/src/index.tsindex dad3749878..4f0ff32bdc 100644--- a/packages/example-videos/src/index.ts+++ b/packages/example-videos/src/index.ts@@ -57,6 +57,7 @@ export const exampleVideos = {gif: path.join(examplePackage, 'public', 'giphy.gif'),png: path.join(docsPackage, 'static', 'img', 'logo.png'),jpeg: path.join(docsPackage, 'static', 'img', 'freelancers', 'ray.jpeg'),+ aac: path.join(__dirname, '..', 'videos', 'sample3.aac'),bmp: path.join(lambdaPackage,'src',diff --git a/packages/example-videos/videos/sample3.aac b/packages/example-videos/videos/sample3.aacnew file mode 100644index 0000000000..6ac801196dBinary files /dev/null and b/packages/example-videos/videos/sample3.aac differdiff --git a/packages/media-parser/src/file-types/detect-file-type.ts b/packages/media-parser/src/file-types/detect-file-type.tsindex 0a5b394be4..2a09f27cf4 100644--- a/packages/media-parser/src/file-types/detect-file-type.ts+++ b/packages/media-parser/src/file-types/detect-file-type.ts@@ -62,6 +62,12 @@ export const isGif = (data: Uint8Array) => {return matchesPattern(gifPattern)(data.subarray(0, 4));};+export const isAac = (data: Uint8Array) => {+ const aacPattern = new Uint8Array([0xff, 0xf1]);++ return matchesPattern(aacPattern)(data.subarray(0, 2));+};+export type RiffType = {type: 'riff';};@@ -82,6 +88,14 @@ export type Mp3Type = {type: 'mp3';};+export type AacType = {+ type: 'aac';+};++export type WavType = {+ type: 'wav';+};+export type GifType = {type: 'gif';};@@ -95,11 +109,14 @@ export type FileType =| WebpType| RiffType| WebmType+ | WavType| PdfType+ | AacType| IsoBaseMediaType| TransportStreamType| Mp3Type| GifType| PngType| BmpType+ | AacType| UnknownType;diff --git a/packages/media-parser/src/file-types/index.ts b/packages/media-parser/src/file-types/index.tsindex 62d5e56bfe..cf22ec5afc 100644--- a/packages/media-parser/src/file-types/index.ts+++ b/packages/media-parser/src/file-types/index.ts@@ -1,6 +1,7 @@import {isBmp} from './bmp';import type {FileType} from './detect-file-type';import {+ isAac,isGif,isIsoBaseMedia,isMp3,@@ -15,10 +16,18 @@ import {isPng} from './png';import {isWebp} from './webp';export const detectFileType = (data: Uint8Array): FileType => {- if (isRiffAvi(data) || isRiffWave(data)) {+ if (isRiffWave(data)) {+ return {type: 'wav'};+ }++ if (isRiffAvi(data)) {return {type: 'riff'};}+ if (isAac(data)) {+ return {type: 'aac'};+ }+const webp = isWebp(data);if (webp) {return webp;diff --git a/packages/media-parser/src/parse-video.ts b/packages/media-parser/src/parse-video.tsindex 510b2355fb..39a4cfd86d 100644--- a/packages/media-parser/src/parse-video.ts+++ b/packages/media-parser/src/parse-video.ts@@ -101,7 +101,7 @@ export const parseVideo = ({if (fileType.type === 'mp3') {return Promise.reject(new IsAnUnsupportedAudioTypeError({- message: 'MP3 files are not supported',+ message: 'MP3 files are not yet supported',mimeType,sizeInBytes: contentLength,fileName: name,@@ -110,6 +110,30 @@ export const parseVideo = ({);}+ if (fileType.type === 'wav') {+ return Promise.reject(+ new IsAnUnsupportedAudioTypeError({+ message: 'WAV files are not yet supported',+ mimeType,+ sizeInBytes: contentLength,+ fileName: name,+ audioType: 'wav',+ }),+ );+ }++ if (fileType.type === 'aac') {+ return Promise.reject(+ new IsAnUnsupportedAudioTypeError({+ message: 'AAC files are not yet supported',+ mimeType,+ sizeInBytes: contentLength,+ fileName: name,+ audioType: 'aac',+ }),+ );+ }+if (fileType.type === 'gif') {return Promise.reject(new IsAGifError({diff --git a/packages/media-parser/src/test/foreign-types.test.ts b/packages/media-parser/src/test/foreign-types.test.tsindex 8650f3f6d8..8cc875fbe1 100644--- a/packages/media-parser/src/test/foreign-types.test.ts+++ b/packages/media-parser/src/test/foreign-types.test.ts@@ -28,6 +28,42 @@ test('Should throw IsAnUnsupportedAudio for a mp3', async () => {}});+test('Should throw IsAnUnsupportedAudio for a wav', async () => {+ try {+ await parseMedia({+ src: exampleVideos.chirp,+ reader: nodeReader,+ });+ throw new Error('Expected an error');+ } catch (e) {+ if (e instanceof IsAnUnsupportedAudioTypeError) {+ expect(e.sizeInBytes).toEqual(2646150);+ expect(e.audioType).toEqual('wav');+ return;+ }++ throw e;+ }+});++test('Should throw IsAnUnsupportedAudio for an aac', async () => {+ try {+ await parseMedia({+ src: exampleVideos.aac,+ reader: nodeReader,+ });+ throw new Error('Expected an error');+ } catch (e) {+ if (e instanceof IsAnUnsupportedAudioTypeError) {+ expect(e.sizeInBytes).toEqual(1758426);+ expect(e.audioType).toEqual('aac');+ return;+ }++ throw e;+ }+});+test('Should throw IsAGifError for a gif', () => {const prom = parseMedia({src: exampleVideos.gif,@@ -138,7 +174,7 @@ test('Should throw IsAnUnsupportedFileTypeError on unknown type', async () => {} catch (e) {if (e instanceof IsAnUnsupportedFileTypeError) {expect(e.mimeType).toEqual(null);- expect(e.sizeInBytes).toEqual(4251);+ expect(e.sizeInBytes).toBeGreaterThan(1000);return;}diff --git a/packages/media-parser/src/test/parse-wav-file.test.ts b/packages/media-parser/src/test/parse-wav-file.test.tsdeleted file mode 100644index b2c561dabe..0000000000--- a/packages/media-parser/src/test/parse-wav-file.test.ts+++ /dev/null@@ -1,65 +0,0 @@-import {exampleVideos} from '@remotion/example-videos';-import {expect, test} from 'bun:test';-import {parseMedia} from '../parse-media';-import {nodeReader} from '../readers/from-node';--test('WAV file full parse', async () => {- const {structure} = await parseMedia({- src: exampleVideos.chirp,- reader: nodeReader,- fields: {- structure: true,- },- });- expect(structure).toEqual({- type: 'riff',- boxes: [- {- fileSize: 2646142,- fileType: 'WAVE',- type: 'riff-header',- },- {- bitsPerSample: 16,- blockAlign: 2,- byteRate: 88200,- formatTag: 1,- numberOfChannels: 1,- sampleRate: 44100,- type: 'wave-format-box',- },- {- id: 'data',- size: 2646000,- type: 'riff-box',- },- {- children: [- {software: 'Lavf60.16.100 (libsndfile-1.0.31)', type: 'isft-box'},- ],- listType: 'INFO',- type: 'list-box',- },- {- id: 'id3',- size: 44,- type: 'riff-box',- },- ],- });-});--test('WAV file should skip over file if only wanting header', async () => {- const {internalStats} = await parseMedia({- src: exampleVideos.chirp,- reader: nodeReader,- fields: {- internalStats: true,- },- });-- expect(internalStats).toEqual({- finalCursorOffset: 12,- skippedBytes: 2646138,- });-});