# mux.js [](https://travis-ci.org/videojs/mux.js) [](https://greenkeeper.io/) Lightweight utilities for inspecting and manipulating video container formats. Maintenance Status: Stable ## Diagram  ## MPEG2-TS to fMP4 Transmuxer Before making use of the Transmuxer it is best to understand the structure of a fragmented MP4 (fMP4). fMP4's are structured in *boxes* as described in the ISOBMFF spec. For a basic fMP4 to be valid it needs to have the following boxes. 1) ftyp (File Type Box) 2) moov (Movie Header Box) 3) moof (Movie Fragment Box) 4) mdat (Movie Data Box) Every fMP4 stream needs to start with an ftyp and moov box which is then followed by many moof and mdat pairs. This is important to understand as when you append your first segment to Media Source Extensions that this segment will need to start with an ftyp and moov followed by a moof and mdat. If you would like to see a clearer representation of your fMP4 you can use the muxjs.mp4.tools.inspect() method. To make use of the Transmuxer method you will need to push data to the transmuxer you have created. Feed in `Uint8Array`s of an MPEG-2 transport stream, get out a fragmented MP4. Lets look at a very basic representation of what needs to happen the first time you want to append a fMP4 to an MSE buffer. ```js // Create your transmuxer: // initOptions is optional and can be omitted at this time. var transmuxer = new muxjs.mp4.Transmuxer(initOptions); // Create an event listener which will be triggered after the transmuxer processes data: // 'data' events signal a new fMP4 segment is ready transmuxer.on('data', function (segment) { // This code will be executed when the event listener is triggered by a Transmuxer.push() method execution. // Create an empty Uint8Array with the summed value of both the initSegment and data byteLength properties. let data = new Uint8Array(segment.initSegment.byteLength + segment.data.byteLength); // Add the segment.initSegment (ftyp/moov) starting at position 0 data.set(segment.initSegment, 0); // Add the segment.data (moof/mdat) starting after the initSegment data.set(segment.data, segment.initSegment.byteLength); // Uncomment this line below to see the structure of your new fMP4 // console.log(muxjs.mp4.tools.inspect(data)); // Add your brand new fMP4 segment to your MSE Source Buffer sourceBuffer.appendBuffer(data); }); // When you push your starting MPEG-TS segment it will cause the 'data' event listener above to run. // It is important to push after your event listener has been defined. transmuxer.push(transportStreamSegment); transmuxer.flush(); ``` Above we are adding in the initSegment (ftyp/moov) to our data array before appending to the MSE Source Buffer. This is required for the first part of data we append to the MSE Source Buffer but we will omit the initSegment for our remaining chunks (moof/mdat)'s of video we are going to append to our Source Buffer. In the case of appending additional segments after your first segment we will just need to use the following event listener anonymous function. ```js transmuxer.on('data', function(segment){ sourceBuffer.appendBuffer(new Uint8Array(segment.data)); }); ``` Here we put all of this together in a very basic example player. ```html