MP4Box.js
JavaScript library to process MP4 files in the browser, with support for progressive parsing. Inspired by the MP4Box tool from the GPAC project. It can be used to:
- get information about an MP4 file,
- segment an MP4 file for use with the Media Source Extension API,
- extract samples from an MP4 to create TextTracks.
- more to come.
A demo is available here and the QUnit tests are here
API
Getting Information###
Similar to MP4Box -info file.mp4
, MP4Box.js can provide general information about the file (duration, number and types of tracks ...). Create an MP4Box object, set the onReady
callback and provide data in the form of ArrayBuffer objects. MP4Box.js supports progressive parsing. You can provide small buffers at a time, the callback will be called when the 'moov' box is parsed.
var mp4box = ;mp4box {};mp4box {};mp4box;mp4box;mp4box;...mp4box;
onMoovStart()####
The onMoovStart
callback is called when the 'moov' box is starting to be parsed, i.e. when the metadata about the file is parsed. Depending on the download speed, it may take a while to download the whole 'moov' box. The end of parsing is signaled by the onReady
callback.
mp4box { console;}
onReady(info)####
The onReady
callback is called when the the 'moov' box has been parsed, i.e. when the metadata about the file is parsed.
mp4box { console;}
The info
argument is an object with the following structure.
- brands: Array of 4CC codes corresponding to the file brands,
- created: Date object, indicating the creation date of the file as given in the file header,
- modified: Date object, indicating the last modification date of the file as given in the file header,
- timescale: Number, corresponding to the timescale as given in the file header,
- duration: Number, providing the duration of the movie (unfragmented part) in timescale units,
- isProgressive: boolean, indicating if the file can be played progressively,
- isFragmented: boolean, indicating if the file is already fragmented,
- fragment_duration: Number, giving the duration of the fragmented part of the file, in timescale units,
- hasIOD: boolean, indicating if the the file contains an MPEG-4 Initial Object Descriptor
- tracks: Array of track information objects
Track information object:
- id: Number, giving track identifier,
- created: Date object, indicating the creation date of the file as given in the track header,
- modified: Date object, indicating the last modification date of the file as given in the track header,
- alternate_group: Number, identifier of the alternate group the track belongs to,
- timescale: Number, indicating the track timescale, as given in the track header,
- duration: Number, providing the duration of the (unfragmented part of) track, in timescale units,
- bitrate: Number, providing the bitrate of the track in bits per second,
- nb_samples: Number, giving the number of track samples (ie. frames),
- codec: String, giving the MIME codecs parameter for this track (e.g. "avc1.42c00d" or "mp4a.40.2"), to be used to create SourceBuffer objects with Media Source Extensions,
- language: String, giving the 3-letter language code,
- track_width: Number, width of the track as indicated in the track header,
- track_height: Number, height of the track as indicated in the track header,
- layer: Number, layer information as indicated in the track header,
- video: Object, information specific for video tracks
- audio: Object, information specific for audio tracks
Video-specific information object:
- width: Number, width of the video track as indicated in the media header,
- height: Number, height of the video track as indicated in the media header,
Audio-specific information object:
- sample_rate: Number, sample rate as indicated in the media header,
- channel_count: Number, number of channels as indicated in the media header,
- sample_size: Number, size in bits of an uncompressed audio sample as indicated in the media header,
onError(e)####
Indicates that an error has occured during the processing. e
is a String.
mp4box { console;}
appendBuffer(data)####
Provides an ArrayBuffer to parse from. The ArrayBuffer must have a fileStart
(Number) property indicating the 0-based position of first byte of the ArrayBuffer in the original file. Returns the offset (in the original file) that is expected to be the fileStart
value of the next buffer.
var ab = ;abfileStart = 0;mp4box;
flush()####
Indicates that no more data will be received and that all remaining samples should be flushed in the segmentation or extraction process.
Segmentation###
var mp4box = ;mp4box { ... mp4box {} mp4box; var initSegs = mp4box; ...};
setSegmentOptions(track_id, user, options)####
Indicates that the track with the given track_id
should be segmented, with the given options. When segments are ready, the callback onSegment is called with the user
parameter. The options
argument is an object with the following properties:
- nbSamples: Number, representing the number of frames per segment, i.e. the time between 2 callbacks to onSegment. If not enough data is received to form a segment, received samples are kept. If not provided, the default is 1000.
- rapAlignement: boolean, indicating if segments should start with a RAP. If not provided, the default is true.
mp4box;
unsetSegmentOptions(track_id)####
Indicates that the track with the given track_id
should not be segmented.
mp4box;
onSegment(id, user, buffer)####
Callback called when a segment is ready, according to the options passed in setSegmentOptions. user
is the caller of the segmentation, for this track, and buffer
is an ArrayBuffer containing the Movie Fragments for this segment.
mp4box { console;}
initializeSegmentation()####
Indicates that the application is ready to receive segments. Returns an array of objects containing the following properties:
- id: Number, the track id
- user: Object, the caller of the segmentation for this track, as given in setSegmentOptions
- buffer: ArrayBuffer, the initialization segment for this track.
Extraction###
It is possible to extract the samples of a track, in a similar manner to the segmentation process.
var mp4box = ;mp4box { ... /* create a texttrack */ var texttrack = v; mp4box {} mp4box; ...};
setExtractionOptions(track_id, user, options)####
Indicates that the track with the given track_id
for which samples should be extracted, with the given options. When samples are ready, the callback onSamples is called with the user
parameter. The options
argument is an object with the following properties:
- nbSamples: Number, representing the number of samples per callback callt. If not enough data is received to extract the number of samples, the samples received so far are kept. If not provided, the default is 1000.
- rapAlignement: boolean, indicating if sample arrays should start with a RAP. If not provided, the default is true.
mp4box;
unsetExtractionOptions(track_id)####
Indicates that the samples for the track with the given track_id
should not be extracted.
mp4box;
onSamples(id, user, samples)####
Callback called when a set of samples is ready, according to the options passed in setExtractionOptions. user
is the caller of the segmentation, for this track, and samples
is an Array of samples.
mp4box { console;}
Each sample has the following structure:
seek(time, useRap)####
Indicates that the next samples to process (for extraction or segmentation) start at the given time (Number, in seconds) or at the time of the previous Random Access Point (if useRap is true, default is false). Returns the offset in the file of the next bytes to be provided via appendBuffer .
mp4box;
Dependencies
This code uses DataStream.js, with some modifications for Uint24 and Uint64 types.
Browser Usage
There is currently no build system. In order to use the MP4Box.js
in a browser, you need to include all files as follows.
MP4Box.js in the browser...