This is a micro library for parsing and editing mp4 files. It's only barely functional, but it does what I needed - which is adding a few iTunes tags to user-uploaded mp4 files. All of this has been only tested on audio files. It can parse an MPEG-4 binary buffer into a traversable 'Atom' structure defined by the spec - edit that structure - and rebuild it into an ArrayBuffer which can be downloaded from a browser or written to a file.
Using npm:
npm install mp4edit
// In a browser environment, listening to a file-picker:
const fileTags;
const mp4;
const reader = new FileReader();
reader.readAsArrayBuffer(uploadedFile);
reader.onload = function(data){
mp4 = new MP4(data.target.result);
fileTags = mp4.getCommonTags(); // Gets cover, album, artist, title and track number.
// fileTags.cover can be bound to an <img src='{{cover}}'>
});
// ----- At some later point -------
mp4.giveTags(newTags); // In the form of an object with optionally cover, album, artist, title or track keys.
const blob = new Blob([mp4.build().buffer], {type: 'audio/mp4'});
const url = URL.createObjectURL(blob);
// The client opening the URL will download it. Or appending an <a> element and calling 'click' on it
URL.revokeObjectURL(url);
Given an ArrayBuffer (or other) containing mp4 binary data, will return a root Atom, containing the rest of the structure nested within.
Given a root Atom, will create a jDataView with the binary data. This create the Atom headers, which have four bytes in them denoting their length.
Given a JS object with the predefined tags (shown below) this will update the internal tree structure with the tags, and offset the stco atom (see here).
Key | Value | type |
---|---|---|
title | Title of song (or video) | String |
artist | Artist name | String |
album | Album title | String |
genre | Song genre | String |
cover | cover art | ArrayBuffer of jpeg |
The units that parsed mp4 files are reduced to, in adherence to the spec. Each Atom can have data, or subatoms as children. Not both - though this rule is broken by several implementations and several atoms.
Constructs an atom with a given four letter name. If given boolean true instead of a name, Atom will act as the mp4 root.
Returns true or false if atom has a subatom named
Returns entire byte length of an atom - same as will be in the header value for the atom. Includes the 8 bytes of header and padding for odd Atoms like meta.
Returns the index of an atom child. If no child is found with that name, -1 is returned.
Returns the first child of Atom that has the name . If no child is found, returns false.
Searches for a child with name . If none is found, will create one and return it. String child can include nested names - such as 'moov.udta.trak'. The method will create neccesary children to accomplish that, and always return an Atom.
Returns a pretty-printed string to help understand the heirarchy of an atom and all of its children.
A jDataView of the internal binary data. All jDataView methods can be used, such as .getString().
Since, as mentioned earlier, some Atoms break the standard a little bit, padding has been added to help deal with that. Primarily, the moov.udta.meta Atom (containing metadata) has a padding of four, which is unique to it, and would be unable to be worked with without that consideration. MP4.parse will correctly identify a moov.udta.meta Atom and not throw an error. Other Atoms that may break spec will not be accounted for.
An array of the children, which are Atoms
The parent Atom, unless this Atom is root - in which case it's not really an atom anyway.
The four-letter atom name. Usually set by the constructor and kept the same.