Parsing / Reading SWF Header


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. var xURLLoader : URLLoader = new URLLoader();
  2. var xByte : uint;
  3. var xNBits : int;
  4. var xOffset : int;
  5.  
  6. pbGet.addEventListener(MouseEvent.CLICK, fOnClick);
  7. xURLLoader.addEventListener(Event.COMPLETE, fOnComplete);
  8. xURLLoader.dataFormat = URLLoaderDataFormat.BINARY;
  9.  
  10. function fParse(v : ByteArray) : Object
  11. {
  12. var vFormat : String;
  13. var vSwfVersion : int;
  14. var vFileLength : int;
  15. var vFrameRate : int;
  16. var vTotalFrames : int;
  17. var vFrameSize : Rectangle;
  18.  
  19. v.endian = Endian.LITTLE_ENDIAN;
  20.  
  21. vFormat = v.readUTFBytes(3);
  22. vSwfVersion = v.readByte();
  23. vFileLength = v.readUnsignedInt();
  24. v.readBytes(v);
  25. v.length -= 8;
  26. if (vFormat == "CWS")
  27. v.uncompress();
  28. v.position = 0;
  29. vFrameSize = new Rectangle();
  30. vFrameSize.left = xfReadNBits(v, true) / 20;
  31. vFrameSize.right = xfReadNBits(v) / 20;
  32. vFrameSize.top = xfReadNBits(v) / 20;
  33. vFrameSize.bottom = xfReadNBits(v) / 20;
  34. vFrameRate = v.readUnsignedByte() / 256 + v.readUnsignedByte();
  35. vTotalFrames = v.readUnsignedShort();
  36. return {
  37. mFormat: vFormat,
  38. mSwfVersion: vSwfVersion,
  39. mFileLength: vFileLength,
  40. mFrameSize: vFrameSize,
  41. mFrameRate: vFrameRate,
  42. mTotalFrames: vTotalFrames
  43. };
  44. }
  45.  
  46. function xfReadNBits(v : ByteArray, vStart : Boolean = false) : uint
  47. {
  48. var n : uint;
  49.  
  50. if (vStart)
  51. {
  52. xByte = v.readUnsignedByte();
  53. xNBits = xByte >> 3;
  54. xOffset = 3;
  55. }
  56.  
  57. n = xByte << (32 - xOffset) >> (32 - xNBits);
  58. xOffset -= xNBits;
  59. while (xOffset < 0)
  60. {
  61. xByte = v.readUnsignedByte();
  62. n |= (xOffset < -8) ? (xByte << (-xOffset - 8)) : (xByte >> (-xOffset - 8));
  63. xOffset += 8;
  64. }
  65. return n;
  66. }
  67.  
  68. function fOnClick(e : MouseEvent) : void
  69. {
  70. xURLLoader.load(new URLRequest("http://" + tfUrl.text));
  71. }
  72.  
  73. function fOnComplete(e : Event) : void
  74. {
  75. var i : int;
  76. var vData : Object;
  77.  
  78. vData = fParse(xURLLoader.data);
  79. tfFormat.text = vData.mFormat;
  80. tfVersion.text = vData.mSwfVersion;
  81. tfFileLength.text = vData.mFileLength;
  82. tfWidth.text = vData.mFrameSize.width;
  83. tfHeight.text = vData.mFrameSize.height;
  84. tfFrameRate.text = vData.mFrameRate;
  85. tfTotalFrames.text = vData.mTotalFrames;
  86. }

URL: http://simplistika.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.