Language: ChineseGermanSpanishFrenchDutchItalianRussian
123 Flash Chat Forums

Go Back   TOPCMM Community > SWFKit > SWFKit Support

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-16-2002, 11:53 PM
Senior Member
 
Join Date: Dec 2002
Posts: 2,015
Default [quote]File

Scopas says:

Quote:

Anyone knows how to access a given files summary? Or a mp3 file id3 info..??

The reason is that I need the bit rate..

/Scopas


Reply With Quote
  #2 (permalink)  
Old 12-16-2002, 11:54 PM
Senior Member
 
Join Date: Dec 2002
Posts: 2,015
Default Re: [quote]File

http://home.swipnet.se/grd/mp3info
The following code is a FFish script clone of the c++ code from the site. you can write your own id3 tag reader.

We found a design fault of the Stream object. It's only designed to access small text files, requires both reading and writing permission. It cannot handle read-only files. However, the fault is fixed. The following code can't work on read-only files before the new update come out.

Code:
function mp3Header(head)
{
this.frameHeader = head;

this.getVersion = function ()
{
var version = [2.5, 0.0, 2.0, 1.0];
return version[this.getVersionIndex()];
}

this.getLayer = function ()
{
return 4 - this.getLayerIndex();
}

this.getBitrate = function ()
{
var table = [0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0,
0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0,
0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0,
0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0,
0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0,
0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0];

var index = 48 * (this.getVersionIndex() & 1) + 16 * (this.getLayerIndex() - 1) + this.getBitrateIndex();
return table[index];
}

this.getFrequency = function ()
{
var table = [32000, 16000, 8000, 0, 0, 0, 22050, 24000, 16000, 44100, 48000, 32000];
var index = this.getVersionIndex() * 3 + this.getFrequencyIndex();

return table[index];
}

this.getMode = function ()
{
switch (this.getModeIndex())
{
case 1: return "Joint Stereo";
case 2: return "Dual Channel";
case 3: return "Single Channel";
default: return "Stereo";
}
}

this.isValid = function ()
{
return (((this.getFrameSync() & 2047) == 2047) &&
     ((this.getVersionIndex() & 3) != 1) &&
       ((this.getLayerIndex() & 3) != 0) && 
       ((this.getBitrateIndex() & 15) != 0) && 
       ((this.getBitrateIndex() & 15) != 15) &&
       ((this.getFrequencyIndex() & 3) != 3) &&
       ((this.getEmphasisIndex() & 3) != 2));
}

function getFrameSync()   { return ((this.frameHeader >> 21) & 2047); };
   function getVersionIndex() { return ((this.frameHeader >> 19) & 3); };
   function getLayerIndex()  { return ((this.frameHeader >> 17) & 3); };
   function getProtectionBit() { return ((this.frameHeader >> 16) & 1); };
   function getBitrateIndex() { return ((this.frameHeader >> 12) & 15); };
   function getFrequencyIndex(){ return ((this.frameHeader >> 10) & 3); };
   function getPaddingBit()  { return ((this.frameHeader >> 9) & 1); };
   function getPrivateBit()  { return ((this.frameHeader >> 8) & 1); };
   function getModeIndex()   { return ((this.frameHeader >> 6) & 3); };
   function getModeExtIndex() { return ((this.frameHeader >> 4) & 3); };
   function getCoprightBit()  { return ((this.frameHeader >> 3) & 1); };
   function getOrginalBit()  { return ((this.frameHeader >> 2) & 1); };
   function getEmphasisIndex() { return ((this.frameHeader) & 3); };
}

function getMP3Header(name)
{
var stream = new Stream(name);
var mp3HeadInfo = new mp3Header(0);
var head = 0;

do 
{
if (stream.pos > 200 || stream.pos >= stream.length)
{
stream.close();
return null;
}

var s = stream.read(4);

mp3HeadInfo.frameHeader = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3];

processMsg();
}
while (!mp3HeadInfo.isValid());

stream.close();

if (mp3HeadInfo.isValid()) return mp3HeadInfo;

return null; 
}

var info = getMP3Header("c:\\songs\\1.mp3");
if (info)
{
trace(info.getVersion());
trace(info.getLayer());
trace(info.getBitrate());
trace(info.getFrequency());
trace(info.getMode());
}
Reply With Quote
  #3 (permalink)  
Old 12-16-2002, 11:54 PM
Senior Member
 
Join Date: Dec 2002
Posts: 2,015
Default Re: [quote]File

Scopas says:

Quote:
It works fine.. But.. the reason why I needed the bitrate was actually because the mediaplayer can't figure out the length of a vbr mp3.. I thought that if I could get the vbr of a file then I can calculate the length of an mp3..

I checked out the link you reffered to http://home.swipnet.se/grd/mp3info/vbrheader/index.html

But I've spent quite a large amount of time figuring aout how to get the right information.. The problem is that I can't figure out to work the stream object right.. Anyway I was figuring if you had also translated this script into ffish script..??

/Scopas


Reply With Quote
  #4 (permalink)  
Old 12-16-2002, 11:56 PM
Senior Member
 
Join Date: Dec 2002
Posts: 2,015
Default Re: [quote]File

Scopas says:

Quote:


I figured it out at last..
It helped when I learned the stream.pos to know..

Anyway the code is pretty smashed up right now but if anyone wants it I can post it when I cleaned it up a bit..

/Scopas
Reply With Quote
  #5 (permalink)  
Old 12-16-2002, 11:57 PM
Senior Member
 
Join Date: Dec 2002
Posts: 2,015
Default OK,post please,and share with us!

OK,post please,and share with us!
Reply With Quote
  #6 (permalink)  
Old 12-16-2002, 11:58 PM
Senior Member
 
Join Date: Dec 2002
Posts: 2,015
Default Here you go..

scopas says:

Quote:
var head;
function mp3Header(head){
this.frameHeader = head;

this.getVersion = function (){
var version = [2.5, 0.0, 2.0, 1.0];
return version[this.getVersionIndex()];
}

this.getLayer = function (){
return 4 - this.getLayerIndex();
}

this.getBitrate = function (){
var table = [0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0,
0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0,
0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0,
0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0,
0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0,
0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0];

var index = 48 * (this.getVersionIndex() & 1) + 16 * (this.getLayerIndex() - 1) + this.getBitrateIndex();
return table[index];
}

this.getFrequency = function (){
var table = [32000, 16000, 8000, 0, 0, 0, 22050, 24000, 16000, 44100, 48000, 32000];
var index = this.getVersionIndex() * 3 + this.getFrequencyIndex();

return table[index];
}

this.getMode = function (){
switch (this.getModeIndex()){
case 1: return "Joint Stereo";
case 2: return "Dual Channel";
case 3: return "Single Channel";
default: return "Stereo";
}
}

this.isValid = function (){
return (((this.getFrameSync() & 2047) == 2047) &&
((this.getVersionIndex() & 3) != 1) &&
((this.getLayerIndex() & 3) != 0) &&
((this.getBitrateIndex() & 15) != 0) &&
((this.getBitrateIndex() & 15) != 15) &&
((this.getFrequencyIndex() & 3) != 3) &&
((this.getEmphasisIndex() & 3) != 2));
}

function getFrameSync() { return ((this.frameHeader >> 21) & 2047); };
function getVersionIndex() { return ((this.frameHeader >> 19) & 3); };
function getLayerIndex() { return ((this.frameHeader >> 17) & 3); };
function getProtectionBit() { return ((this.frameHeader >> 16) & 1); };
function getBitrateIndex() { return ((this.frameHeader >> 12) & 15); };
function getFrequencyIndex(){ return ((this.frameHeader >> 10) & 3); };
function getPaddingBit() { return ((this.frameHeader >> 9) & 1); };
function getPrivateBit() { return ((this.frameHeader >> 8) & 1); };
function getModeIndex() { return ((this.frameHeader >> 6) & 3); };
function getModeExtIndex() { return ((this.frameHeader >> 4) & 3); };
function getCoprightBit() { return ((this.frameHeader >> 3) & 1); };
function getOrginalBit() { return ((this.frameHeader >> 2) & 1); };
function getEmphasisIndex() { return ((this.frameHeader) & 3); };
}

function getMP3Header(name){
var stream = new Stream(name);
var mp3HeadInfo = new mp3Header(0);
head = 0;

do{
stream.pos = head;
if(stream.pos > (1024*200) || stream.pos >= stream.length){
stream.close();
return null;
}
var s = stream.read(4);
mp3HeadInfo.frameHeader = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3];
processMsg();
head++;
}while (!mp3HeadInfo.isValid());

stream.close();
if (mp3HeadInfo.isValid()) return mp3HeadInfo;
return null;
}


function mp3Xing(p){
this.p = p;

this.myFlag = function (){ return head - 1; }
this.myFrames = function (){ return (this.p[4] << 24) | (this.p[5] << 16) | (this.p[6] << 8) | this.p[7]; }
this.myFsize = function (){ return (this.p[8] << 24) | (this.p[9] << 16) | (this.p[10] << 8) | this.p[11]; }

this.isValid = function (){
if(this.myFrames != "" && this.myFsize != "" && this.myFlag != ""){
return true;
}else{
return false;
}
}
}

function getMp3Xing(name){
var mp3XingInfo = new mp3Xing(0);

var Xstream = new Stream(name);
var i = 0;
var j = 1;
do{
Xstream.pos = i;
s = Xstream.read(4);
if(s == "Xing"){
mp3XingInfo.p = Xstream.read(12);
j = 2;
}
i++;
if(i > (head + 10000)){ j = 2; }
}while (j == 1 || Xstream.pos >= Xstream.length);
Xstream.close();

if (mp3XingInfo.isValid()) return mp3XingInfo;
}

var f = "audio files(*.mp3)|*.mp3;|all files(*.*)|*.*|"
var res = Dialogs.fileOpen(f, "", "", true);
var path = res[0];

var info;
if(path){ info = getMP3Header(path); }
if(info){
trace("Version: "+ info.getVersion());
trace("Layer: "+ info.getLayer());
trace("Bitrate: "+ info.getBitrate());
trace("Frequency: "+ info.getFrequency());
trace("Mode: "+ info.getMode());
}
var info2;
if(path){ info2 = getMp3Xing(path);}
if(info2){
trace("Header position: "+ info2.myFlag());
trace("Total frames: "+ info2.myFrames());
trace("File size: "+ info2.myFsize());

if(info2.myFrames()){
if(info.getLayer() == 1){
var myVBR = ((info2.myFsize()/info2.myFrames())*info.getFrequency())/12000;
var myTime = (info2.myFsize()*8)/(myVBR*1000);
trace("VBR - "+ myVBR);
trace("Length in seconds - "+ myTime);
}else{
var myVBR = ((info2.myFsize()/info2.myFrames())*info.getFrequency())/144000;
var myTime = (info2.myFsize()*8)/(myVBR*1000);
trace("VBR - "+ myVBR);
trace("Length in seconds - "+ myTime);
}
}
}
Reply With Quote
Reply

Was this information helpful?    Yes No



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 02:03 AM.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.