Skip to content

🎥 Make videos programmatically with React

nice!

Jonny Burger9e4c302f03bb
diff --git a/packages/media-parser/src/boxes/iso-base-media/process-box.ts b/packages/media-parser/src/boxes/iso-base-media/process-box.ts
index 53084faff9..ac963a6df4 100644
--- a/packages/media-parser/src/boxes/iso-base-media/process-box.ts
+++ b/packages/media-parser/src/boxes/iso-base-media/process-box.ts
@@ -62,6 +62,7 @@ const getChildren = async ({
boxType === 'mdia' ||
boxType === 'minf' ||
boxType === 'stbl' ||
+ boxType === 'udta' ||
boxType === 'moof' ||
boxType === 'dims' ||
boxType === 'meta' ||
diff --git a/packages/media-parser/src/metadata/metadata-from-iso.ts b/packages/media-parser/src/metadata/metadata-from-iso.ts
index 3252f70709..a123675a2c 100644
--- a/packages/media-parser/src/metadata/metadata-from-iso.ts
+++ b/packages/media-parser/src/metadata/metadata-from-iso.ts
@@ -1,3 +1,4 @@
+import type {IlstBox} from '../boxes/iso-base-media/meta/ilst';
import {
getMoovBox,
getTkhdBox,
@@ -7,6 +8,106 @@ import type {IsoBaseMediaStructure, RegularBox} from '../parse-result';
import {truthy} from '../truthy';
import type {MetadataEntry} from './get-metadata';
+/**
+ *
+ * @param ilstBox ©ART - Artist
+ â–ª Hex: A9 41 52 54
+ 2. ©alb - Album
+ â–ª Hex: A9 61 6C 62
+ 3. ©cmt - Comment
+ â–ª Hex: A9 63 6D 74
+ 4. ©day - Release Date
+ â–ª Hex: A9 64 61 79
+ 5. ©gen - Genre
+ â–ª Hex: A9 67 65 6E
+ 6. ©nam - Title
+ â–ª Hex: A9 6E 61 6D
+ 7. ©too - Encoder
+ â–ª Hex: A9 74 6F 6F
+ 8. ©wrt - Writer
+ â–ª Hex: A9 77 72 74
+ 9. ©cpy - Copyright
+ â–ª Hex: A9 63 70 79
+ 10. ©dir - Director
+ â–ª Hex: A9 64 69 72
+ 11. ©prd - Producer
+ â–ª Hex: A9 70 72 64
+ 12. ©des - Description
+ â–ª Hex: A9 64 65 73
+ */
+const mapToKey = (index: number) => {
+ if (index === 0xa9415254) {
+ return 'artist';
+ }
+
+ if (index === 0xa9616c62) {
+ return 'album';
+ }
+
+ if (index === 0xa9636d74) {
+ return 'comment';
+ }
+
+ if (index === 0xa9646179) {
+ return 'releaseDate';
+ }
+
+ if (index === 0xa967656e) {
+ return 'genre';
+ }
+
+ if (index === 0xa96e616d) {
+ return 'title';
+ }
+
+ if (index === 0xa9746f6f) {
+ return 'encoder';
+ }
+
+ if (index === 0xa9777274) {
+ return 'writer';
+ }
+
+ if (index === 0xa9637079) {
+ return 'copyright';
+ }
+
+ if (index === 0xa9646972) {
+ return 'director';
+ }
+
+ if (index === 0xa9707264) {
+ return 'producer';
+ }
+
+ if (index === 0xa9646573) {
+ return 'description';
+ }
+
+ return null;
+};
+
+const parseIlstBoxWithoutKeys = (ilstBox: IlstBox): MetadataEntry[] => {
+ return ilstBox.entries
+ .map((entry): MetadataEntry | null => {
+ const key = mapToKey(entry.index);
+ if (!key) {
+ return null;
+ }
+
+ if (entry.value.type === 'unknown') {
+ return null;
+ }
+
+ return {
+ trackId: null,
+ key,
+ value: entry.value.value,
+ };
+ })
+ .filter(truthy);
+};
+
export const parseIsoMetaBox = (
meta: RegularBox,
trackId: number | null,
@@ -14,11 +115,11 @@ export const parseIsoMetaBox = (
const ilstBox = meta.children.find((b) => b.type === 'ilst-box');
const keysBox = meta.children.find((b) => b.type === 'keys-box');
if (!ilstBox || !keysBox) {
- return [];
- }
+ if (ilstBox) {
+ return parseIlstBoxWithoutKeys(ilstBox as IlstBox);
+ }
- if (ilstBox.entries.length !== keysBox.entries.length) {
- throw new Error('Number of entries in ilst and keys box do not match');
+ return [];
}
const entries: MetadataEntry[] = [];
@@ -27,9 +128,14 @@ export const parseIsoMetaBox = (
const ilstEntry = ilstBox.entries[i];
const keysEntry = keysBox.entries[i];
if (ilstEntry.value.type !== 'unknown') {
+ const value =
+ typeof ilstEntry.value.value === 'string' &&
+ ilstEntry.value.value.endsWith('\u0000')
+ ? ilstEntry.value.value.slice(0, -1)
+ : ilstEntry.value.value;
entries.push({
key: keysEntry.value,
- value: ilstEntry.value.value,
+ value,
trackId,
});
}
@@ -50,6 +156,13 @@ export const getMetadataFromIsoBase = (
const meta = moov.children.find(
(b) => b.type === 'regular-box' && b.boxType === 'meta',
) as RegularBox | null;
+ const udta = moov.children.find(
+ (b) => b.type === 'regular-box' && b.boxType === 'udta',
+ ) as RegularBox | null;
+ const metaInUdta = udta?.children.find((b) => {
+ return b.type === 'regular-box' && b.boxType === 'meta';
+ }) as RegularBox | null;
+
const metaInTracks = traks
.map((t) => {
const metaBox = t.children.find(
@@ -67,9 +180,10 @@ export const getMetadataFromIsoBase = (
return null;
})
.filter(truthy);
- if (!meta) {
- return metaInTracks.flat(1);
- }
- return [...parseIsoMetaBox(meta, null), ...metaInTracks.flat(1)];
+ return [
+ ...(meta ? parseIsoMetaBox(meta, null) : []),
+ ...(metaInUdta ? parseIsoMetaBox(metaInUdta, null) : []),
+ ...metaInTracks.flat(1),
+ ];
};
diff --git a/packages/media-parser/src/metadata/metadata-from-matroska.ts b/packages/media-parser/src/metadata/metadata-from-matroska.ts
index 70bd0a489b..5c626090fc 100644
--- a/packages/media-parser/src/metadata/metadata-from-matroska.ts
+++ b/packages/media-parser/src/metadata/metadata-from-matroska.ts
@@ -4,6 +4,10 @@ import {getTrackWithUid} from '../boxes/webm/traversal';
import type {MatroskaStructure} from '../parse-result';
import type {MetadataEntry} from './get-metadata';
+const removeEndZeroes = (value: string): string => {
+ return value.endsWith('\u0000') ? removeEndZeroes(value.slice(0, -1)) : value;
+};
+
const parseSimpleTagIntoEbml = (
children: PossibleEbml[],
trackId: number | null,
@@ -17,9 +21,7 @@ const parseSimpleTagIntoEbml = (
return {
trackId,
key: tagName.value.toLowerCase(),
- value: tagString.value.endsWith('\u0000')
- ? tagString.value.slice(0, -1)
- : tagString.value,
+ value: removeEndZeroes(tagString.value),
};
};
diff --git a/packages/media-parser/src/test/matroska.test.ts b/packages/media-parser/src/test/matroska.test.ts
index 9d9c9d821e..43c21c9053 100644
--- a/packages/media-parser/src/test/matroska.test.ts
+++ b/packages/media-parser/src/test/matroska.test.ts
@@ -296,7 +296,7 @@ test('Should get duration of AV1 video', async () => {
{
minVintWidth: 1,
type: 'Targets',
- value: '0x',
+ value: [],
},
{
minVintWidth: 1,
@@ -323,7 +323,13 @@ test('Should get duration of AV1 video', async () => {
{
minVintWidth: 1,
type: 'Targets',
- value: '0x63c588ab2171012bb9020a',
+ value: [
+ {
+ minVintWidth: 1,
+ type: 'TagTrackUID',
+ value: '0xab2171012bb9020a',
+ },
+ ],
},
{
minVintWidth: 1,
diff --git a/packages/media-parser/src/test/read-metadata.test.ts b/packages/media-parser/src/test/read-metadata.test.ts
index e096ec548b..02d91ae52b 100644
--- a/packages/media-parser/src/test/read-metadata.test.ts
+++ b/packages/media-parser/src/test/read-metadata.test.ts
@@ -140,3 +140,111 @@ test('Metadata from Matroska', async () => {
},
]);
});
+
+test('webm Big buck bunny metadata', async () => {
+ const {metadata} = await parseMedia({
+ src: exampleVideos.av1bbb,
+ fields: {
+ metadata: true,
+ },
+ reader: nodeReader,
+ });
+ expect(metadata).toEqual([
+ {
+ key: 'major_brand',
+ trackId: null,
+ value: 'isom',
+ },
+ {
+ key: 'minor_version',
+ trackId: null,
+ value: '512',
+ },
+ {
+ key: 'compatible_brands',
+ trackId: null,
+ value: 'isomav01iso2mp41',
+ },
+ {
+ key: 'artist',
+ trackId: null,
+ value: 'Blender Foundation 2008, Janus Bager Kristensen 2013',
+ },
+ {
+ key: 'composer',
+ trackId: null,
+ value: 'Sacha Goedegebure',
+ },
+ {
+ key: 'genre',
+ trackId: null,
+ value: 'Animation',
+ },
+ {
+ key: 'comment',
+ trackId: null,
+ value:
+ 'Creative Commons Attribution 3.0 - http://bbb3d.renderfarming.net',
+ },
+ {
+ key: 'encoder',
+ trackId: null,
+ value: 'Lavf58.63.100',
+ },
+ {
+ key: 'handler_name',
+ trackId: 1,
+ value: 'GPAC ISO Video Handler',
+ },
+ {
+ key: 'duration',
+ trackId: 1,
+ value: '00:00:10.000000000',
+ },
+ ]);
+});
+
+test('mp4 Big buck bunny metadata', async () => {
+ const {metadata, structure} = await parseMedia({
+ src: exampleVideos.av1mp4,
+ fields: {
+ metadata: true,
+ structure: true,
+ },
+ reader: nodeReader,
+ });
+
+ expect(metadata).toEqual([
+ {
+ key: 'title',
+ trackId: null,
+ value: 'Big Buck Bunny, Sunflower version',
+ },
+ {
+ key: 'artist',
+ trackId: null,
+ value: 'Blender Foundation 2008, Janus Bager Kristensen 2013',
+ },
+ {
+ key: 'writer',
+ trackId: null,
+ value: 'Sacha Goedegebure',
+ },
+ {
+ key: 'encoder',
+ trackId: null,
+ value: 'Lavf60.16.100',
+ },
+ {
+ key: 'comment',
+ trackId: null,
+ value:
+ 'Creative Commons Attribution 3.0 - http://bbb3d.renderfarming.net',
+ },
+ {
+ key: 'genre',
+ trackId: null,
+ value: 'Animation',
+ },
+ ]);
+});