Advertisement
IWBH_01

mp4 file parse (WIP)

Aug 30th, 2021 (edited)
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. DataView.prototype.getString=function(bi,L){
  2.  var s="";
  3.  while(bi!=L){
  4.   s+=String.fromCharCode(this.getUint8(bi));
  5.   bi++;
  6.  }
  7.  return s;
  8. };
  9.  
  10.  
  11.  
  12. //mp4 file to arraybuffer (byteArray), then to dataview
  13.  
  14.  
  15. self.parse_mp4=function parse_mp4(dv,ind,L){
  16. var rez=[],clen,cuc=0,bL=dv.byteLength; ind=ind||0,L=L||bL;
  17. if(ind>bL||L>bL||L<8||ind<0){ return "input atom indexes are invalid"; }
  18. while(ind<L){
  19.  clen=dv.getUint32(ind); if((clen+ind)>L) console.log("error atom length overflow @ "+ind);
  20.  else if(clen<8){ console.log("atom length underflow: "+clen+", skipped 4 bits @ "+ind); clen=4; }
  21.  else rez.push([dv.getString(ind+4,ind+8),ind,clen]);
  22.  ind+=clen;
  23.   cuc++; if(cuc>65535){ console.log("infinite loop error @ "+ind); break; }
  24. }
  25. return rez;
  26. };
  27.  
  28.  
  29. //not use?
  30. self.parse_atoms=function parse_atoms(ay,dv){
  31.  var i2=ay.length,c;
  32.  if(i2>0)while(i2--){ c=ay[i2]; c.push(parse_mp4(dv,c[1]+8,c[2]+c[1])); }
  33. };
  34.  
  35.  
  36. self.parse_mp4_iter=function parse_mp4_iter(dv,ind,L,L0,Pi){ if(L0>165){ return "somthin ain't right..."; }
  37. var rez=[],clen,cuc=0,ye,bL=dv.byteLength; ind=ind||0,L=L||bL; L0=L0||0; Pi=Pi||0;
  38. if(ind>bL||L>bL||L<8||ind<0){ return "input atom indexes are invalid"; }
  39. while(ind<L){
  40.  clen=dv.getUint32(ind); if((clen+ind)>L) console.log("error atom length overflow @ "+ind);
  41.  else if(clen<8){clen=4; console.log("atom length underflow: "+clen+", skipped 4 bits @ "+ind);}
  42.  else{ rez.push([dv.getString(ind+4,ind+8),ind,clen,Pi,L0]); rez=rez.concat(parse_mp4_iter(dv,ind+8,ind+clen,(L0)+1,ind)); }
  43.  ind+=clen;
  44.   cuc++; if(cuc>65535){ console.log("infinite loop error @ "+ind); break; }
  45. }
  46. return rez;
  47. };
  48.  
  49. self.rMap=function reverseMap(parsa){ //input rez (output of parse_mp4_iter)
  50.  var map={},i=parsa.length;
  51.  while(i--){ map[parsa[i][1]]=i; }
  52.  parsa.pm=map; //parent map;
  53.  return map;
  54. };
  55.  
  56.  
  57. self.parse_stco=function parse_stco(stco,mp4Dv){ //input stco entry array from parse_mp4_iter
  58.  
  59. var si=stco[1],lim=si+stco[2], st=si+16,ne=mp4Dv.getUint32(si+12),i=st,
  60. ent=[]; //chunck index entries;
  61.  
  62. while(i<lim){ ent.push(mp4Dv.getUint32(i)); i+=4; }
  63. if(ent.length!=ne)console.error("error, chunk entry count is wrong?");
  64. return ent;
  65. };
  66.  
  67.  
  68. //ex:
  69. //rez[i]=[name,RealIndex,RealLength] //don't forget to add 8 to RealIndex (the length of the length store plus the type store)
  70.  
  71.  
  72.  
  73.  
  74.  
  75. //will need? https://web.archive.org/web/20201111155659im_/https://sole.github.io/Animated_GIF/dist/Animated_GIF.js
  76.  
  77. /*
  78. finp=document.querySelector("input[type=file]");
  79. fi=finp.files[0];
  80. fi.arrayBuffer().then(function(a){ self.mp4Dv=new DataView(a);
  81.  
  82. self.rez2=parse_mp4(mp4Dv);
  83.  
  84.  
  85.  
  86.  });
  87.  
  88.  
  89. */
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement