Comparing version 0.4.8 to 0.4.9
@@ -1,4 +0,4 @@ | ||
/*! mp4box 20-08-2021 */ | ||
/*! mp4box 28-08-2021 */ | ||
var Log=console;Log.setLogLevel=function(e){};var MP4BoxStream=function(e){if(!(e instanceof ArrayBuffer))throw"Needs an array buffer";this.buffer=e,this.dataview=new DataView(e),this.position=0};MP4BoxStream.prototype.getPosition=function(){return this.position},MP4BoxStream.prototype.getEndPosition=function(){return this.buffer.byteLength},MP4BoxStream.prototype.getLength=function(){return this.buffer.byteLength},MP4BoxStream.prototype.seek=function(e){e=Math.max(0,Math.min(this.buffer.byteLength,e));return this.position=isNaN(e)||!isFinite(e)?0:e,!0},MP4BoxStream.prototype.isEos=function(){return this.getPosition()>=this.getEndPosition()},MP4BoxStream.prototype.readAnyInt=function(e,t){var i=0;if(this.position+e<=this.buffer.byteLength){switch(e){case 1:i=t?this.dataview.getInt8(this.position):this.dataview.getUint8(this.position);break;case 2:i=t?this.dataview.getInt16(this.position):this.dataview.getUint16(this.position);break;case 3:if(t)throw"No method for reading signed 24 bits values";i=this.dataview.getUint8(this.position)<<16,i|=this.dataview.getUint8(this.position+1)<<8,i|=this.dataview.getUint8(this.position+2);break;case 4:i=t?this.dataview.getInt32(this.position):this.dataview.getUint32(this.position);break;case 8:if(t)throw"No method for reading signed 64 bits values";i=this.dataview.getUint32(this.position)<<32,i|=this.dataview.getUint32(this.position+4);break;default:throw"readInt method not implemented for size: "+e}return this.position+=e,i}throw"Not enough bytes in buffer"},MP4BoxStream.prototype.readUint8=function(){return this.readAnyInt(1,!1)},MP4BoxStream.prototype.readUint16=function(){return this.readAnyInt(2,!1)},MP4BoxStream.prototype.readUint24=function(){return this.readAnyInt(3,!1)},MP4BoxStream.prototype.readUint32=function(){return this.readAnyInt(4,!1)},MP4BoxStream.prototype.readUint64=function(){return this.readAnyInt(8,!1)},MP4BoxStream.prototype.readString=function(e){if(this.position+e<=this.buffer.byteLength){for(var t="",i=0;i<e;i++)t+=String.fromCharCode(this.readUint8());return t}throw"Not enough bytes in buffer"},MP4BoxStream.prototype.readCString=function(){for(var e=[];;){var t=this.readUint8();if(0===t)break;e.push(t)}return String.fromCharCode.apply(null,e)},MP4BoxStream.prototype.readInt8=function(){return this.readAnyInt(1,!0)},MP4BoxStream.prototype.readInt16=function(){return this.readAnyInt(2,!0)},MP4BoxStream.prototype.readInt32=function(){return this.readAnyInt(4,!0)},MP4BoxStream.prototype.readInt64=function(){return this.readAnyInt(8,!1)},MP4BoxStream.prototype.readUint8Array=function(e){for(var t=new Uint8Array(e),i=0;i<e;i++)t[i]=this.readUint8();return t},MP4BoxStream.prototype.readInt16Array=function(e){for(var t=new Int16Array(e),i=0;i<e;i++)t[i]=this.readInt16();return t},MP4BoxStream.prototype.readUint16Array=function(e){for(var t=new Int16Array(e),i=0;i<e;i++)t[i]=this.readUint16();return t},MP4BoxStream.prototype.readUint32Array=function(e){for(var t=new Uint32Array(e),i=0;i<e;i++)t[i]=this.readUint32();return t},MP4BoxStream.prototype.readInt32Array=function(e){for(var t=new Int32Array(e),i=0;i<e;i++)t[i]=this.readInt32();return t},"undefined"!=typeof exports&&(exports.MP4BoxStream=MP4BoxStream);var BoxParser={ERR_INVALID_DATA:-1,ERR_NOT_ENOUGH_DATA:0,OK:1,BASIC_BOXES:["mdat","idat","free","skip","meco","strk"],FULL_BOXES:["hmhd","nmhd","iods","xml ","bxml","ipro","mere"],CONTAINER_BOXES:[["moov",["trak","pssh"]],["trak"],["edts"],["mdia"],["minf"],["dinf"],["stbl",["sgpd","sbgp"]],["mvex",["trex"]],["moof",["traf"]],["traf",["trun","sgpd","sbgp"]],["vttc"],["tref"],["iref"],["mfra",["tfra"]],["meco"],["hnti"],["hinf"],["strk"],["strd"],["sinf"],["rinf"],["schi"],["trgr"],["udta",["kind"]],["iprp",["ipma"]],["ipco"]],boxCodes:[],fullBoxCodes:[],containerBoxCodes:[],sampleEntryCodes:{},sampleGroupEntryCodes:[],trackGroupTypes:[],UUIDBoxes:{},UUIDs:[],initialize:function(){BoxParser.FullBox.prototype=new BoxParser.Box,BoxParser.ContainerBox.prototype=new BoxParser.Box,BoxParser.SampleEntry.prototype=new BoxParser.Box,BoxParser.TrackGroupTypeBox.prototype=new BoxParser.FullBox,BoxParser.BASIC_BOXES.forEach(function(e){BoxParser.createBoxCtor(e)}),BoxParser.FULL_BOXES.forEach(function(e){BoxParser.createFullBoxCtor(e)}),BoxParser.CONTAINER_BOXES.forEach(function(e){BoxParser.createContainerBoxCtor(e[0],null,e[1])})},Box:function(e,t,i){this.type=e,this.size=t,this.uuid=i},FullBox:function(e,t,i){BoxParser.Box.call(this,e,t,i),this.flags=0,this.version=0},ContainerBox:function(e,t,i){BoxParser.Box.call(this,e,t,i),this.boxes=[]},SampleEntry:function(e,t,i,r){BoxParser.ContainerBox.call(this,e,t),this.hdr_size=i,this.start=r},SampleGroupEntry:function(e){this.grouping_type=e},TrackGroupTypeBox:function(e,t){BoxParser.FullBox.call(this,e,t)},createBoxCtor:function(t,e){BoxParser.boxCodes.push(t),BoxParser[t+"Box"]=function(e){BoxParser.Box.call(this,t,e)},BoxParser[t+"Box"].prototype=new BoxParser.Box,e&&(BoxParser[t+"Box"].prototype.parse=e)},createFullBoxCtor:function(t,i){BoxParser[t+"Box"]=function(e){BoxParser.FullBox.call(this,t,e)},BoxParser[t+"Box"].prototype=new BoxParser.FullBox,BoxParser[t+"Box"].prototype.parse=function(e){this.parseFullHeader(e),i&&i.call(this,e)}},addSubBoxArrays:function(e){if(e)for(var t=(this.subBoxNames=e).length,i=0;i<t;i++)this[e[i]+"s"]=[]},createContainerBoxCtor:function(t,e,i){BoxParser[t+"Box"]=function(e){BoxParser.ContainerBox.call(this,t,e),BoxParser.addSubBoxArrays.call(this,i)},BoxParser[t+"Box"].prototype=new BoxParser.ContainerBox,e&&(BoxParser[t+"Box"].prototype.parse=e)},createMediaSampleEntryCtor:function(e,t,i){BoxParser.sampleEntryCodes[e]=[],BoxParser[e+"SampleEntry"]=function(e,t){BoxParser.SampleEntry.call(this,e,t),BoxParser.addSubBoxArrays.call(this,i)},BoxParser[e+"SampleEntry"].prototype=new BoxParser.SampleEntry,t&&(BoxParser[e+"SampleEntry"].prototype.parse=t)},createSampleEntryCtor:function(t,i,e,r){BoxParser.sampleEntryCodes[t].push(i),BoxParser[i+"SampleEntry"]=function(e){BoxParser[t+"SampleEntry"].call(this,i,e),BoxParser.addSubBoxArrays.call(this,r)},BoxParser[i+"SampleEntry"].prototype=new BoxParser[t+"SampleEntry"],e&&(BoxParser[i+"SampleEntry"].prototype.parse=e)},createEncryptedSampleEntryCtor:function(e,t,i){BoxParser.createSampleEntryCtor.call(this,e,t,i,["sinf"])},createSampleGroupCtor:function(t,e){BoxParser[t+"SampleGroupEntry"]=function(e){BoxParser.SampleGroupEntry.call(this,t,e)},BoxParser[t+"SampleGroupEntry"].prototype=new BoxParser.SampleGroupEntry,e&&(BoxParser[t+"SampleGroupEntry"].prototype.parse=e)},createTrackGroupCtor:function(t,e){BoxParser[t+"TrackGroupTypeBox"]=function(e){BoxParser.TrackGroupTypeBox.call(this,t,e)},BoxParser[t+"TrackGroupTypeBox"].prototype=new BoxParser.TrackGroupTypeBox,e&&(BoxParser[t+"TrackGroupTypeBox"].prototype.parse=e)},createUUIDBox:function(t,i,r,s){BoxParser.UUIDs.push(t),BoxParser.UUIDBoxes[t]=function(e){(i?BoxParser.FullBox:r?BoxParser.ContainerBox:BoxParser.Box).call(this,"uuid",e,t)},BoxParser.UUIDBoxes[t].prototype=new(i?BoxParser.FullBox:r?BoxParser.ContainerBox:BoxParser.Box),s&&(BoxParser.UUIDBoxes[t].prototype.parse=i?function(e){this.parseFullHeader(e),s&&s.call(this,e)}:s)}};BoxParser.initialize(),BoxParser.TKHD_FLAG_ENABLED=1,BoxParser.TKHD_FLAG_IN_MOVIE=2,BoxParser.TKHD_FLAG_IN_PREVIEW=4,BoxParser.TFHD_FLAG_BASE_DATA_OFFSET=1,BoxParser.TFHD_FLAG_SAMPLE_DESC=2,BoxParser.TFHD_FLAG_SAMPLE_DUR=8,BoxParser.TFHD_FLAG_SAMPLE_SIZE=16,BoxParser.TFHD_FLAG_SAMPLE_FLAGS=32,BoxParser.TFHD_FLAG_DUR_EMPTY=65536,BoxParser.TFHD_FLAG_DEFAULT_BASE_IS_MOOF=131072,BoxParser.TRUN_FLAGS_DATA_OFFSET=1,BoxParser.TRUN_FLAGS_FIRST_FLAG=4,BoxParser.TRUN_FLAGS_DURATION=256,BoxParser.TRUN_FLAGS_SIZE=512,BoxParser.TRUN_FLAGS_FLAGS=1024,BoxParser.TRUN_FLAGS_CTS_OFFSET=2048,BoxParser.Box.prototype.add=function(e){return this.addBox(new BoxParser[e+"Box"])},BoxParser.Box.prototype.addBox=function(e){return this.boxes.push(e),this[e.type+"s"]?this[e.type+"s"].push(e):this[e.type]=e,e},BoxParser.Box.prototype.set=function(e,t){return this[e]=t,this},BoxParser.Box.prototype.addEntry=function(e,t){t=t||"entries";return this[t]||(this[t]=[]),this[t].push(e),this},"undefined"!=typeof exports&&(exports.BoxParser=BoxParser),BoxParser.parseUUID=function(e){return BoxParser.parseHex16(e)},BoxParser.parseHex16=function(e){for(var t="",i=0;i<16;i++){var r=e.readUint8().toString(16);t+=1===r.length?"0"+r:r}return t},BoxParser.parseOneBox=function(e,t,i){var r,s,a=e.getPosition(),o=0;if(e.getEndPosition()-a<8)return Log.debug("BoxParser","Not enough data in stream to parse the type and size of the box"),{code:BoxParser.ERR_NOT_ENOUGH_DATA};if(i&&i<8)return Log.debug("BoxParser","Not enough bytes left in the parent box to parse a new box"),{code:BoxParser.ERR_NOT_ENOUGH_DATA};var n=e.readUint32(),h=e.readString(4),d=h;if(Log.debug("BoxParser","Found box of type '"+h+"' and size "+n+" at position "+a),o=8,"uuid"==h){if(e.getEndPosition()-e.getPosition()<16||i-o<16)return e.seek(a),Log.debug("BoxParser","Not enough bytes left in the parent box to parse a UUID box"),{code:BoxParser.ERR_NOT_ENOUGH_DATA};o+=16,d=s=BoxParser.parseUUID(e)}if(1==n){if(e.getEndPosition()-e.getPosition()<8||i&&i-o<8)return e.seek(a),Log.warn("BoxParser",'Not enough data in stream to parse the extended size of the "'+h+'" box'),{code:BoxParser.ERR_NOT_ENOUGH_DATA};n=e.readUint64(),o+=8}else if(0===n)if(i)n=i;else if("mdat"!==h)return Log.error("BoxParser","Unlimited box size not supported for type: '"+h+"'"),r=new BoxParser.Box(h,n),{code:BoxParser.OK,box:r,size:r.size};return n<o?(Log.error("BoxParser","Box of type "+h+" has an invalid size "+n+" (too small to be a box)"),{code:BoxParser.ERR_NOT_ENOUGH_DATA,type:h,size:n,hdr_size:o,start:a}):i&&i<n?(Log.error("BoxParser","Box of type '"+h+"' has a size "+n+" greater than its container size "+i),{code:BoxParser.ERR_NOT_ENOUGH_DATA,type:h,size:n,hdr_size:o,start:a}):a+n>e.getEndPosition()?(e.seek(a),Log.info("BoxParser","Not enough data in stream to parse the entire '"+h+"' box"),{code:BoxParser.ERR_NOT_ENOUGH_DATA,type:h,size:n,hdr_size:o,start:a}):t?{code:BoxParser.OK,type:h,size:n,hdr_size:o,start:a}:(BoxParser[h+"Box"]?r=new BoxParser[h+"Box"](n):"uuid"!==h?(Log.warn("BoxParser","Unknown box type: '"+h+"'"),(r=new BoxParser.Box(h,n)).has_unparsed_data=!0):BoxParser.UUIDBoxes[s]?r=new BoxParser.UUIDBoxes[s](n):(Log.warn("BoxParser","Unknown uuid type: '"+s+"'"),(r=new BoxParser.Box(h,n)).uuid=s,r.has_unparsed_data=!0),r.hdr_size=o,r.start=a,r.write===BoxParser.Box.prototype.write&&"mdat"!==r.type&&(Log.info("BoxParser","'"+d+"' box writing not yet implemented, keeping unparsed data in memory for later write"),r.parseDataAndRewind(e)),r.parse(e),(a=e.getPosition()-(r.start+r.size))<0?(Log.warn("BoxParser","Parsing of box '"+d+"' did not read the entire indicated box data size (missing "+-a+" bytes), seeking forward"),e.seek(r.start+r.size)):0<a&&(Log.error("BoxParser","Parsing of box '"+d+"' read "+a+" more bytes than the indicated box data size, seeking backwards"),e.seek(r.start+r.size)),{code:BoxParser.OK,box:r,size:r.size})},BoxParser.Box.prototype.parse=function(e){"mdat"!=this.type?this.data=e.readUint8Array(this.size-this.hdr_size):0===this.size?e.seek(e.getEndPosition()):e.seek(this.start+this.size)},BoxParser.Box.prototype.parseDataAndRewind=function(e){this.data=e.readUint8Array(this.size-this.hdr_size),e.position-=this.size-this.hdr_size},BoxParser.FullBox.prototype.parseDataAndRewind=function(e){this.parseFullHeader(e),this.data=e.readUint8Array(this.size-this.hdr_size),this.hdr_size-=4,e.position-=this.size-this.hdr_size},BoxParser.FullBox.prototype.parseFullHeader=function(e){this.version=e.readUint8(),this.flags=e.readUint24(),this.hdr_size+=4},BoxParser.FullBox.prototype.parse=function(e){this.parseFullHeader(e),this.data=e.readUint8Array(this.size-this.hdr_size)},BoxParser.ContainerBox.prototype.parse=function(e){for(;e.getPosition()<this.start+this.size;){if((t=BoxParser.parseOneBox(e,!1,this.size-(e.getPosition()-this.start))).code!==BoxParser.OK)return;var t,i=t.box;this.boxes.push(i),this.subBoxNames&&-1!=this.subBoxNames.indexOf(i.type)?this[this.subBoxNames[this.subBoxNames.indexOf(i.type)]+"s"].push(i):this[t="uuid"!==i.type?i.type:i.uuid]?Log.warn("Box of type "+t+" already stored in field of this type"):this[t]=i}},BoxParser.Box.prototype.parseLanguage=function(e){this.language=e.readUint16();e=[];e[0]=this.language>>10&31,e[1]=this.language>>5&31,e[2]=31&this.language,this.languageString=String.fromCharCode(e[0]+96,e[1]+96,e[2]+96)},BoxParser.createFullBoxCtor("emsg",function(e){1==this.version?(this.timescale=e.readUint32(),this.presentation_time=e.readUint64(),this.event_duration=e.readUint32(),this.id=e.readUint32(),this.scheme_id_uri=e.readCString(),this.value=e.readCString()):(this.scheme_id_uri=e.readCString(),this.value=e.readCString(),this.timescale=e.readUint32(),this.presentation_time_delta=e.readUint32(),this.event_duration=e.readUint32(),this.id=e.readUint32());var t=this.size-this.hdr_size-(16+(this.scheme_id_uri.length+1)+(this.value.length+1));1==this.version&&(t-=4),this.message_data=e.readUint8Array(t)}),BoxParser.createBoxCtor("styp",function(e){BoxParser.ftypBox.prototype.parse.call(this,e)}),BoxParser.createBoxCtor("ftyp",function(e){var t=this.size-this.hdr_size;this.major_brand=e.readString(4),this.minor_version=e.readUint32(),t-=8,this.compatible_brands=[];for(var i=0;4<=t;)this.compatible_brands[i]=e.readString(4),t-=4,i++}),BoxParser.createFullBoxCtor("mdhd",function(e){1==this.version?(this.creation_time=e.readUint64(),this.modification_time=e.readUint64(),this.timescale=e.readUint32(),this.duration=e.readUint64()):(this.creation_time=e.readUint32(),this.modification_time=e.readUint32(),this.timescale=e.readUint32(),this.duration=e.readUint32()),this.parseLanguage(e),e.readUint16()}),BoxParser.createFullBoxCtor("mfhd",function(e){this.sequence_number=e.readUint32()}),BoxParser.createFullBoxCtor("mvhd",function(e){1==this.version?(this.creation_time=e.readUint64(),this.modification_time=e.readUint64(),this.timescale=e.readUint32(),this.duration=e.readUint64()):(this.creation_time=e.readUint32(),this.modification_time=e.readUint32(),this.timescale=e.readUint32(),this.duration=e.readUint32()),this.rate=e.readUint32(),this.volume=e.readUint16()>>8,e.readUint16(),e.readUint32Array(2),this.matrix=e.readUint32Array(9),e.readUint32Array(6),this.next_track_id=e.readUint32()}),BoxParser.createFullBoxCtor("sidx",function(e){this.reference_ID=e.readUint32(),this.timescale=e.readUint32(),0===this.version?(this.earliest_presentation_time=e.readUint32(),this.first_offset=e.readUint32()):(this.earliest_presentation_time=e.readUint64(),this.first_offset=e.readUint64()),e.readUint16(),this.references=[];for(var t=e.readUint16(),i=0;i<t;i++){var r={};this.references.push(r);var s=e.readUint32();r.reference_type=s>>31&1,r.referenced_size=2147483647&s,r.subsegment_duration=e.readUint32(),s=e.readUint32(),r.starts_with_SAP=s>>31&1,r.SAP_type=s>>28&7,r.SAP_delta_time=268435455&s}}),BoxParser.createFullBoxCtor("ssix",function(e){this.subsegments=[];for(var t=e.readUint32(),i=0;i<t;i++){var r={};this.subsegments.push(r),r.ranges=[];for(var s=e.readUint32(),a=0;a<s;a++){var o={};r.ranges.push(o),o.level=e.readUint8(),o.range_size=e.readUint24()}}}),BoxParser.createFullBoxCtor("tkhd",function(e){1==this.version?(this.creation_time=e.readUint64(),this.modification_time=e.readUint64(),this.track_id=e.readUint32(),e.readUint32(),this.duration=e.readUint64()):(this.creation_time=e.readUint32(),this.modification_time=e.readUint32(),this.track_id=e.readUint32(),e.readUint32(),this.duration=e.readUint32()),e.readUint32Array(2),this.layer=e.readInt16(),this.alternate_group=e.readInt16(),this.volume=e.readInt16()>>8,e.readUint16(),this.matrix=e.readInt32Array(9),this.width=e.readUint32(),this.height=e.readUint32()}),BoxParser.createFullBoxCtor("tfhd",function(e){var t=0;this.track_id=e.readUint32(),this.size-this.hdr_size>t&&this.flags&BoxParser.TFHD_FLAG_BASE_DATA_OFFSET?(this.base_data_offset=e.readUint64(),t+=8):this.base_data_offset=0,this.size-this.hdr_size>t&&this.flags&BoxParser.TFHD_FLAG_SAMPLE_DESC?(this.default_sample_description_index=e.readUint32(),t+=4):this.default_sample_description_index=0,this.size-this.hdr_size>t&&this.flags&BoxParser.TFHD_FLAG_SAMPLE_DUR?(this.default_sample_duration=e.readUint32(),t+=4):this.default_sample_duration=0,this.size-this.hdr_size>t&&this.flags&BoxParser.TFHD_FLAG_SAMPLE_SIZE?(this.default_sample_size=e.readUint32(),t+=4):this.default_sample_size=0,this.size-this.hdr_size>t&&this.flags&BoxParser.TFHD_FLAG_SAMPLE_FLAGS?(this.default_sample_flags=e.readUint32(),t+=4):this.default_sample_flags=0}),BoxParser.createFullBoxCtor("tfdt",function(e){1==this.version?this.baseMediaDecodeTime=e.readUint64():this.baseMediaDecodeTime=e.readUint32()}),BoxParser.createFullBoxCtor("trun",function(e){var t=0;if(this.sample_count=e.readUint32(),t+=4,this.size-this.hdr_size>t&&this.flags&BoxParser.TRUN_FLAGS_DATA_OFFSET?(this.data_offset=e.readInt32(),t+=4):this.data_offset=0,this.size-this.hdr_size>t&&this.flags&BoxParser.TRUN_FLAGS_FIRST_FLAG?(this.first_sample_flags=e.readUint32(),t+=4):this.first_sample_flags=0,this.sample_duration=[],this.sample_size=[],this.sample_flags=[],this.sample_composition_time_offset=[],this.size-this.hdr_size>t)for(var i=0;i<this.sample_count;i++)this.flags&BoxParser.TRUN_FLAGS_DURATION&&(this.sample_duration[i]=e.readUint32()),this.flags&BoxParser.TRUN_FLAGS_SIZE&&(this.sample_size[i]=e.readUint32()),this.flags&BoxParser.TRUN_FLAGS_FLAGS&&(this.sample_flags[i]=e.readUint32()),this.flags&BoxParser.TRUN_FLAGS_CTS_OFFSET&&(0===this.version?this.sample_composition_time_offset[i]=e.readUint32():this.sample_composition_time_offset[i]=e.readInt32())});var ISOFile=function(e){this.stream=e||new MultiBufferStream,this.boxes=[],this.mdats=[],this.moofs=[],this.isProgressive=!1,this.moovStartFound=!1,this.onMoovStart=null,this.moovStartSent=!1,this.onReady=null,this.readySent=!1,this.onSegment=null,this.onSamples=null,this.onError=null,this.sampleListBuilt=!1,this.fragmentedTracks=[],this.extractedTracks=[],this.isFragmentationInitialized=!1,this.sampleProcessingStarted=!1,this.nextMoofNumber=0,this.itemListBuilt=!1,this.onSidx=null,this.sidxSent=!1};ISOFile.prototype.setSegmentOptions=function(e,t,i){var r,s=this.getTrackById(e);s&&(r={},this.fragmentedTracks.push(r),r.id=e,r.user=t,(r.trak=s).nextSample=0,r.segmentStream=null,r.nb_samples=1e3,r.rapAlignement=!0,i&&(i.nbSamples&&(r.nb_samples=i.nbSamples),i.rapAlignement&&(r.rapAlignement=i.rapAlignement)))},ISOFile.prototype.unsetSegmentOptions=function(e){for(var t=-1,i=0;i<this.fragmentedTracks.length;i++)this.fragmentedTracks[i].id==e&&(t=i);-1<t&&this.fragmentedTracks.splice(t,1)},ISOFile.prototype.setExtractionOptions=function(e,t,i){var r,s=this.getTrackById(e);s&&(r={},this.extractedTracks.push(r),r.id=e,r.user=t,(r.trak=s).nextSample=0,r.nb_samples=1e3,r.samples=[],i&&i.nbSamples&&(r.nb_samples=i.nbSamples))},ISOFile.prototype.unsetExtractionOptions=function(e){for(var t=-1,i=0;i<this.extractedTracks.length;i++)this.extractedTracks[i].id==e&&(t=i);-1<t&&this.extractedTracks.splice(t,1)},ISOFile.prototype.parse=function(){var e;if(!this.restoreParsePosition||this.restoreParsePosition())for(;;)if(this.hasIncompleteMdat&&this.hasIncompleteMdat()){if(!this.processIncompleteMdat())return}else if(this.saveParsePosition&&this.saveParsePosition(),(e=BoxParser.parseOneBox(this.stream,!1)).code===BoxParser.ERR_NOT_ENOUGH_DATA){if(!this.processIncompleteBox)return;if(!this.processIncompleteBox(e))return}else{var t,i="uuid"!==(t=e.box).type?t.type:t.uuid;switch(this.boxes.push(t),i){case"mdat":this.mdats.push(t);break;case"moof":this.moofs.push(t);break;case"moov":this.moovStartFound=!0,0===this.mdats.length&&(this.isProgressive=!0);default:void 0!==this[i]&&Log.warn("ISOFile","Duplicate Box of type: "+i+", overriding previous occurrence"),this[i]=t}this.updateUsedBytes&&this.updateUsedBytes(t,e)}},ISOFile.prototype.checkBuffer=function(e){if(null==e)throw"Buffer must be defined and non empty";if(void 0===e.fileStart)throw"Buffer must have a fileStart property";return 0===e.byteLength?(Log.warn("ISOFile","Ignoring empty buffer (fileStart: "+e.fileStart+")"),this.stream.logBufferLevel(),!1):(Log.info("ISOFile","Processing buffer (fileStart: "+e.fileStart+")"),e.usedBytes=0,this.stream.insertBuffer(e),this.stream.logBufferLevel(),!!this.stream.initialized()||(Log.warn("ISOFile","Not ready to start parsing"),!1))},ISOFile.prototype.appendBuffer=function(e,t){var i;if(this.checkBuffer(e))return this.parse(),this.moovStartFound&&!this.moovStartSent&&(this.moovStartSent=!0,this.onMoovStart&&this.onMoovStart()),this.moov?(this.sampleListBuilt||(this.buildSampleLists(),this.sampleListBuilt=!0),this.updateSampleLists(),this.onReady&&!this.readySent&&(this.readySent=!0,this.onReady(this.getInfo())),this.processSamples(t),this.nextSeekPosition?(i=this.nextSeekPosition,this.nextSeekPosition=void 0):i=this.nextParsePosition,this.stream.getEndFilePositionAfter&&(i=this.stream.getEndFilePositionAfter(i))):i=this.nextParsePosition||0,this.sidx&&this.onSidx&&!this.sidxSent&&(this.onSidx(this.sidx),this.sidxSent=!0),this.meta&&(this.flattenItemInfo&&!this.itemListBuilt&&(this.flattenItemInfo(),this.itemListBuilt=!0),this.processItems&&this.processItems(this.onItem)),this.stream.cleanBuffers&&(Log.info("ISOFile","Done processing buffer (fileStart: "+e.fileStart+") - next buffer to fetch should have a fileStart position of "+i),this.stream.logBufferLevel(),this.stream.cleanBuffers(),this.stream.logBufferLevel(!0),Log.info("ISOFile","Sample data size in memory: "+this.getAllocatedSampleDataSize())),i},ISOFile.prototype.getInfo=function(){var e,t,i,r,s,a={},o=new Date("1904-01-01T00:00:00Z").getTime();if(this.moov)for(a.hasMoov=!0,a.duration=this.moov.mvhd.duration,a.timescale=this.moov.mvhd.timescale,a.isFragmented=null!=this.moov.mvex,a.isFragmented&&this.moov.mvex.mehd&&(a.fragment_duration=this.moov.mvex.mehd.fragment_duration),a.isProgressive=this.isProgressive,a.hasIOD=null!=this.moov.iods,a.brands=[],a.brands.push(this.ftyp.major_brand),a.brands=a.brands.concat(this.ftyp.compatible_brands),a.created=new Date(o+1e3*this.moov.mvhd.creation_time),a.modified=new Date(o+1e3*this.moov.mvhd.modification_time),a.tracks=[],a.audioTracks=[],a.videoTracks=[],a.subtitleTracks=[],a.metadataTracks=[],a.hintTracks=[],a.otherTracks=[],e=0;e<this.moov.traks.length;e++){if(s=(i=this.moov.traks[e]).mdia.minf.stbl.stsd.entries[0],r={},a.tracks.push(r),r.id=i.tkhd.track_id,r.name=i.mdia.hdlr.name,r.references=[],i.tref)for(t=0;t<i.tref.boxes.length;t++)ref={},r.references.push(ref),ref.type=i.tref.boxes[t].type,ref.track_ids=i.tref.boxes[t].track_ids;i.edts&&(r.edits=i.edts.elst.entries),r.created=new Date(o+1e3*i.tkhd.creation_time),r.modified=new Date(o+1e3*i.tkhd.modification_time),r.movie_duration=i.tkhd.duration,r.movie_timescale=a.timescale,r.layer=i.tkhd.layer,r.alternate_group=i.tkhd.alternate_group,r.volume=i.tkhd.volume,r.matrix=i.tkhd.matrix,r.track_width=i.tkhd.width/65536,r.track_height=i.tkhd.height/65536,r.timescale=i.mdia.mdhd.timescale,r.cts_shift=i.mdia.minf.stbl.cslg,r.duration=i.mdia.mdhd.duration,r.samples_duration=i.samples_duration,r.codec=s.getCodec(),r.kind=i.udta&&i.udta.kinds.length?i.udta.kinds[0]:{schemeURI:"",value:""},r.language=i.mdia.elng?i.mdia.elng.extended_language:i.mdia.mdhd.languageString,r.nb_samples=i.samples.length,r.size=i.samples_size,r.bitrate=8*r.size*r.timescale/r.samples_duration,s.isAudio()?(r.type="audio",a.audioTracks.push(r),r.audio={},r.audio.sample_rate=s.getSampleRate(),r.audio.channel_count=s.getChannelCount(),r.audio.sample_size=s.getSampleSize()):s.isVideo()?(r.type="video",a.videoTracks.push(r),r.video={},r.video.width=s.getWidth(),r.video.height=s.getHeight()):s.isSubtitle()?(r.type="subtitles",a.subtitleTracks.push(r)):s.isHint()?(r.type="metadata",a.hintTracks.push(r)):s.isMetadata()?(r.type="metadata",a.metadataTracks.push(r)):(r.type="metadata",a.otherTracks.push(r))}else a.hasMoov=!1;if(a.mime="",a.hasMoov&&a.tracks){for(a.videoTracks&&0<a.videoTracks.length?a.mime+='video/mp4; codecs="':a.audioTracks&&0<a.audioTracks.length?a.mime+='audio/mp4; codecs="':a.mime+='application/mp4; codecs="',e=0;e<a.tracks.length;e++)0!==e&&(a.mime+=","),a.mime+=a.tracks[e].codec;a.mime+='"; profiles="',a.mime+=this.ftyp.compatible_brands.join(),a.mime+='"'}return a},ISOFile.prototype.processSamples=function(e){var t;if(this.sampleProcessingStarted){if(this.isFragmentationInitialized&&null!==this.onSegment)for(t=0;t<this.fragmentedTracks.length;t++)for(var i=this.fragmentedTracks[t],r=i.trak;r.nextSample<r.samples.length&&this.sampleProcessingStarted;){Log.debug("ISOFile","Creating media fragment on track #"+i.id+" for sample "+r.nextSample);var s=this.createFragment(i.id,r.nextSample,i.segmentStream);if(!s)break;if(i.segmentStream=s,r.nextSample++,(r.nextSample%i.nb_samples==0||e||r.nextSample>=r.samples.length)&&(Log.info("ISOFile","Sending fragmented data on track #"+i.id+" for samples ["+Math.max(0,r.nextSample-i.nb_samples)+","+(r.nextSample-1)+"]"),Log.info("ISOFile","Sample data size in memory: "+this.getAllocatedSampleDataSize()),this.onSegment&&this.onSegment(i.id,i.user,i.segmentStream.buffer,r.nextSample,e||r.nextSample>=r.samples.length),i.segmentStream=null,i!==this.fragmentedTracks[t]))break}if(null!==this.onSamples)for(t=0;t<this.extractedTracks.length;t++){var a=this.extractedTracks[t];for(r=a.trak;r.nextSample<r.samples.length&&this.sampleProcessingStarted;){Log.debug("ISOFile","Exporting on track #"+a.id+" sample #"+r.nextSample);var o=this.getSample(r,r.nextSample);if(!o)break;if(r.nextSample++,a.samples.push(o),(r.nextSample%a.nb_samples==0||r.nextSample>=r.samples.length)&&(Log.debug("ISOFile","Sending samples on track #"+a.id+" for sample "+r.nextSample),this.onSamples&&this.onSamples(a.id,a.user,a.samples),a.samples=[],a!==this.extractedTracks[t]))break}}}},ISOFile.prototype.getBox=function(e){e=this.getBoxes(e,!0);return e.length?e[0]:null},ISOFile.prototype.getBoxes=function(e,t){var i=[];return ISOFile._sweep.call(this,e,i,t),i},ISOFile._sweep=function(e,t,i){for(var r in this.type&&this.type==e&&t.push(this),this.boxes){if(t.length&&i)return;ISOFile._sweep.call(this.boxes[r],e,t,i)}},ISOFile.prototype.getTrackSamplesInfo=function(e){e=this.getTrackById(e);if(e)return e.samples},ISOFile.prototype.getTrackSample=function(e,t){e=this.getTrackById(e);return this.getSample(e,t)},ISOFile.prototype.releaseUsedSamples=function(e,t){var i=0,r=this.getTrackById(e);r.lastValidSample||(r.lastValidSample=0);for(var s=r.lastValidSample;s<t;s++)i+=this.releaseSample(r,s);Log.info("ISOFile","Track #"+e+" released samples up to "+t+" (released size: "+i+", remaining: "+this.samplesDataSize+")"),r.lastValidSample=t},ISOFile.prototype.start=function(){this.sampleProcessingStarted=!0,this.processSamples(!1)},ISOFile.prototype.stop=function(){this.sampleProcessingStarted=!1},ISOFile.prototype.flush=function(){Log.info("ISOFile","Flushing remaining samples"),this.updateSampleLists(),this.processSamples(!0),this.stream.cleanBuffers(),this.stream.logBufferLevel(!0)},ISOFile.prototype.seekTrack=function(e,t,i){var r,s,a,o,n=0,h=0;if(0===i.samples.length)return Log.info("ISOFile","No sample in track, cannot seek! Using time "+Log.getDurationString(0,1)+" and offset: 0"),{offset:0,time:0};for(r=0;r<i.samples.length;r++){if(s=i.samples[r],0===r)h=0,o=s.timescale;else if(s.cts>e*s.timescale){h=r-1;break}t&&s.is_sync&&(n=r)}for(t&&(h=n),e=i.samples[h].cts,i.nextSample=h;i.samples[h].alreadyRead===i.samples[h].size&&i.samples[h+1];)h++;return a=i.samples[h].offset+i.samples[h].alreadyRead,Log.info("ISOFile","Seeking to "+(t?"RAP":"")+" sample #"+i.nextSample+" on track "+i.tkhd.track_id+", time "+Log.getDurationString(e,o)+" and offset: "+a),{offset:a,time:e/o}},ISOFile.prototype.seek=function(e,t){var i,r,s=this.moov,a={offset:1/0,time:1/0};if(this.moov){for(r=0;r<s.traks.length;r++)i=s.traks[r],(i=this.seekTrack(e,t,i)).offset<a.offset&&(a.offset=i.offset),i.time<a.time&&(a.time=i.time);return Log.info("ISOFile","Seeking at time "+Log.getDurationString(a.time,1)+" needs a buffer with a fileStart position of "+a.offset),a.offset===1/0?a={offset:this.nextParsePosition,time:0}:a.offset=this.stream.getEndFilePositionAfter(a.offset),Log.info("ISOFile","Adjusted seek position (after checking data already in buffer): "+a.offset),a}throw"Cannot seek: moov not received!"},ISOFile.prototype.equal=function(e){for(var t=0;t<this.boxes.length&&t<e.boxes.length;){var i=this.boxes[t],r=e.boxes[t];if(!BoxParser.boxEqual(i,r))return!1;t++}return!0},"undefined"!=typeof exports&&(exports.ISOFile=ISOFile),BoxParser.Box.prototype.printHeader=function(e){this.size+=8,this.size>MAX_SIZE&&(this.size+=8),"uuid"===this.type&&(this.size+=16),e.log(e.indent+"size:"+this.size),e.log(e.indent+"type:"+this.type)},BoxParser.FullBox.prototype.printHeader=function(e){this.size+=4,BoxParser.Box.prototype.printHeader.call(this,e),e.log(e.indent+"version:"+this.version),e.log(e.indent+"flags:"+this.flags)},BoxParser.Box.prototype.print=function(e){this.printHeader(e)},BoxParser.ContainerBox.prototype.print=function(e){this.printHeader(e);for(var t,i=0;i<this.boxes.length;i++)this.boxes[i]&&(t=e.indent,e.indent+=" ",this.boxes[i].print(e),e.indent=t)},ISOFile.prototype.print=function(e){e.indent="";for(var t=0;t<this.boxes.length;t++)this.boxes[t]&&this.boxes[t].print(e)},BoxParser.mvhdBox.prototype.print=function(e){BoxParser.FullBox.prototype.printHeader.call(this,e),e.log(e.indent+"creation_time: "+this.creation_time),e.log(e.indent+"modification_time: "+this.modification_time),e.log(e.indent+"timescale: "+this.timescale),e.log(e.indent+"duration: "+this.duration),e.log(e.indent+"rate: "+this.rate),e.log(e.indent+"volume: "+(this.volume>>8)),e.log(e.indent+"matrix: "+this.matrix.join(", ")),e.log(e.indent+"next_track_id: "+this.next_track_id)},BoxParser.tkhdBox.prototype.print=function(e){BoxParser.FullBox.prototype.printHeader.call(this,e),e.log(e.indent+"creation_time: "+this.creation_time),e.log(e.indent+"modification_time: "+this.modification_time),e.log(e.indent+"track_id: "+this.track_id),e.log(e.indent+"duration: "+this.duration),e.log(e.indent+"volume: "+(this.volume>>8)),e.log(e.indent+"matrix: "+this.matrix.join(", ")),e.log(e.indent+"layer: "+this.layer),e.log(e.indent+"alternate_group: "+this.alternate_group),e.log(e.indent+"width: "+this.width),e.log(e.indent+"height: "+this.height)};var MP4Box={createFile:function(e,t){e=void 0===e||e,t=new ISOFile(t);return t.discardMdatData=!e,t}};"undefined"!=typeof exports&&(exports.createFile=MP4Box.createFile); | ||
//# sourceMappingURL=mp4box.simple.min.js.map |
{ | ||
"name": "mp4box", | ||
"version": "0.4.8", | ||
"version": "0.4.9", | ||
"description": "JavaScript version of GPAC's MP4Box tool", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -7,3 +7,3 @@ MP4Box.js | ||
JavaScript library to process MP4 files in the browser (and in NodeJS), with support for progressive parsing. | ||
Inspired by the [MP4Box](http://gpac.wp.mines-telecom.fr/mp4box/) tool from the [GPAC](http://gpac.wp.mines-telecom.fr) project. | ||
Inspired by the [MP4Box](https://github.com/gpac/gpac/wiki/MP4Box) tool from the [GPAC](http://gpac.io) project. | ||
It can be used to: | ||
@@ -10,0 +10,0 @@ - [get information about an MP4 file](#getting-information), |
@@ -11,8 +11,8 @@ ISOFile.prototype.add = BoxParser.Box.prototype.add; | ||
moov.add("mvhd").set("timescale", options.timescale || 600) | ||
.set("rate", options.rate || 1) | ||
.set("rate", options.rate || 1<<16) | ||
.set("creation_time", 0) | ||
.set("modification_time", 0) | ||
.set("duration", options.duration || 0) | ||
.set("volume", 1) | ||
.set("matrix", [ 0, 0, 0, 0, 0, 0, 0, 0, 0]) | ||
.set("volume", (options.width) ? 0 : 0x0100) | ||
.set("matrix", [ 1<<16, 0, 0, 0, 1<<16, 0, 0, 0, 0x40000000]) | ||
.set("next_track_id", 1); | ||
@@ -55,3 +55,3 @@ moov.add("mvex"); | ||
.set("duration", options.media_duration || 0) | ||
.set("language", options.language || 0); | ||
.set("language", options.language || "und"); | ||
@@ -87,9 +87,8 @@ mdia.add("hdlr").set("handler", options.hdlr || "vide") | ||
.set("depth", 0x18); | ||
// sample_description_entry.add("avcC").set("SPS", []) | ||
// .set("PPS", []) | ||
// .set("configurationVersion", 1) | ||
// .set("AVCProfileIndication",0) | ||
// .set("profile_compatibility", 0) | ||
// .set("AVCLevelIndication" ,0) | ||
// .set("lengthSizeMinusOne", 0); | ||
if (options.avcDecoderConfigRecord) { | ||
var avcC = new BoxParser.avcCBox(); | ||
var stream = new MP4BoxStream(options.avcDecoderConfigRecord); | ||
avcC.parse(stream); | ||
sample_description_entry.addBox(avcC); | ||
} | ||
break; | ||
@@ -170,3 +169,3 @@ case "Audio": | ||
sample.data = data; | ||
sample.size = data.length; | ||
sample.size = data.byteLength; | ||
sample.alreadyRead = sample.size; | ||
@@ -187,2 +186,5 @@ sample.duration = options.duration || 1; | ||
trak.samples_duration += sample.duration; | ||
if (!trak.first_dts) { | ||
trak.first_dts = options.dts; | ||
} | ||
@@ -196,3 +198,3 @@ this.processSamples(); | ||
moof.trafs[0].truns[0].data_offset = moof.size+8; //8 is mdat header | ||
this.add("mdat").data = data; | ||
this.add("mdat").data = new Uint8Array(data); | ||
return sample; | ||
@@ -212,5 +214,6 @@ } | ||
var traf = moof.add("traf"); | ||
var trak = this.getTrackById(sample.track_id); | ||
traf.add("tfhd").set("track_id", sample.track_id) | ||
.set("flags", BoxParser.TFHD_FLAG_DEFAULT_BASE_IS_MOOF); | ||
traf.add("tfdt").set("baseMediaDecodeTime", sample.dts); | ||
traf.add("tfdt").set("baseMediaDecodeTime", (sample.dts - trak.first_dts)); | ||
traf.add("trun").set("flags", BoxParser.TRUN_FLAGS_DATA_OFFSET | BoxParser.TRUN_FLAGS_DURATION | | ||
@@ -217,0 +220,0 @@ BoxParser.TRUN_FLAGS_SIZE | BoxParser.TRUN_FLAGS_FLAGS | |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17239
968163
227