WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Sound and Media

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

MP3 Extract Information

Keywords: 	 MP3 Extract Information.wbt

Get MP3 Information.wbt:
#DefineFunction GetBits(dIN,off)
   mp3bin=ArrDimension(3)
   for xx=0 to 2
   mp3Bin[xx]=BinaryPeek(dIN,off+xx)
   next
   return(mp3Bin)
#EndFunction

#DefineFunction ShiftBits(sd)
   mp3shift=ArrDimension(2)
   mp3shift[0]= ((sd[0] & 15) << 4) | ((sd[1] & 240) >> 4)
   mp3shift[1]= ((sd[1] & 15) << 4) | ((sd[2] & 240) >> 4)
   return (mp3shift)
#EndFunction

#DefineFunction GetShiftedBits(dIn,off)
   mp3shift=ArrDimension(2)
   mp3shift[0]= BinaryPeek(din,off)
   mp3shift[1]= BinaryPeek(din,off+1)
   return (mp3shift)
#EndFunction

#DefineFunction GetMP3Information(file)
;Initialization
bitrate_lookup=ArrDimension(8,15)

bitrate_data = "032,032,032,032,008,008,"
bitrate_data = StrCat(bitrate_data , "064,048,040,048,016,016,")
bitrate_data = StrCat(bitrate_data , "096,056,048,056,024,024,")
bitrate_data = StrCat(bitrate_data , "128,064,056,064,032,032,")
bitrate_data = StrCat(bitrate_data , "160,080,064,080,040,040,")
bitrate_data = StrCat(bitrate_data , "192,096,080,096,048,048,")
bitrate_data = StrCat(bitrate_data , "224,112,096,112,056,056,")
bitrate_data = StrCat(bitrate_data , "256,128,112,128,064,064,")
bitrate_data = StrCat(bitrate_data , "288,160,128,144,080,080,")
bitrate_data = StrCat(bitrate_data , "320,192,160,160,096,096,")
bitrate_data = StrCat(bitrate_data , "352,224,192,176,112,112,")
bitrate_data = StrCat(bitrate_data , "384,256,224,192,128,128,")
bitrate_data = StrCat(bitrate_data , "416,320,256,224,144,144,")
bitrate_data = StrCat(bitrate_data , "448,384,320,256,160,160")

index=1
For Y = 1 To 14
   For X = 7 To 5 by -1
      ;bitrate_lookup(X, Y) = Left(bitrate_data, 3)
      ;bitrate_data = Right(bitrate_data, Len(bitrate_data) - 4)
      bitrate_lookup[X,Y] = ItemExtract(index,bitrate_data,",")
      index=index+1
   Next
   For X = 3 To 1 by -1
      ;bitrate_lookup(X, Y) = Left(bitrate_data, 3)
      ;bitrate_data = Right(bitrate_data, Len(bitrate_data) - 4)
      bitrate_lookup[X,Y] = ItemExtract(index,bitrate_data,",")
      index=index+1
   Next
Next


;start real work



fsize=FileSize(file)
grabsize=min(fsize,4096)
dIN=BinaryAlloc(grabsize)
BinaryReadEx(dIN,0,file,0,grabsize)

grabsizem1=grabsize-1
Found=0

for i=0 to grabsizem1
  d1=BinaryPeek(dIN,i)
  d2=BinaryPeek(dIN,i+1)

  if d1==255 && ((d2 & 240) == 240)
     mp3bits_array = ShiftBits(GetBits(dIN,i+1))
     Found=1
     break
  endif

  dSHIFT=ShiftBits(GetBits(dIN,i))
  dd1=dSHIFT[0]
  dd2=dSHIFT[1]

  if dd1 == 255 && ((dd2 & 240) == 240)
     mp3bits_array=GetShiftedBits(dIN,i+2)
     Found=1
     break
  endif
next
If Found==0 then return("ERROR")

mp3_id=   (128 & mp3bits_array[0]) / 128
mp3_layer=(96 & mp3bits_array[0]) / 32
mp3_prot =(16 & mp3bits_array[0])
mp3_bitrate=(15 & mp3bits_array[0])
mp3_freq=(192 & mp3bits_array[1])
mp3_pad = (32 * mp3bits_array[1]) / 2
actual_bitrate = 1000 * bitrate_lookup[(mp3_id*4) | mp3_layer, mp3_bitrate]

if mp3_id == 0 then GETMP3ID="MPEG-2"
else GETMP3ID="MPEG-1"

GETMP3LAYER="?unknown"
if mp3_layer==1 then GETMP3LAYER="Layer III"
if mp3_layer==2 then GETMP3LAYER="Layer II"
if mp3_layer==3 then GETMP3LAYER="Layer I"

GETMP3SAMPLERATE=0
temp= (mp3_id*4) | mp3_freq
if temp==0 then  GETMP3SAMPLERATE=22050
if temp==1 then  GETMP3SAMPLERATE=24000
if temp==2 then  GETMP3SAMPLERATE=16000
if temp==4 then  GETMP3SAMPLERATE=44100
if temp==5 then  GETMP3SAMPLERATE=48000
if temp==6 then  GETMP3SAMPLERATE=32000

framesize=((144.0 * actual_bitrate) / GETMP3SAMPLERATE) + mp3_pad
total_frames = fsize / framesize
track_length= total_frames / 38.5 

GETMP3FRAMES = Int(total_frames)
GETMP3DURATION = Int(track_length)


MP3INFO=strcat(GETMP3ID,@tab,GETMP3LAYER,@tab,GETMP3SAMPLERATE,@tab,GETMP3FRAMES,@tab,GETMP3DURATION)
Return(MP3INFO)
#EndFunction

;Test Case

myfile="C:\BizDocs\Incoming\1 work\This_is_your_Capt.mp3"
mp3info=GetMP3Information(myfile)

if mp3info=="ERROR"
      Message("Error","Error Occurred")
else
   ; Format Message
   line=""
   line=strcat(line,"Id="      ,@tab,ItemExtract(1,mp3info,@tab),@crlf)
   line=strcat(line,"Layer="   ,@tab,ItemExtract(2,mp3info,@tab),@crlf)
   line=strcat(line,"SampRate=",@tab,ItemExtract(3,mp3info,@tab),@crlf)
   line=strcat(line,"Frames="  ,@tab,ItemExtract(4,mp3info,@tab),@crlf)
   line=strcat(line,"Time="    ,@tab,ItemExtract(5,mp3info,@tab),@crlf)
   Message(myfile,line)
endif

Article ID:   W15707
File Created: 2003:05:13:11:29:44
Last Updated: 2003:05:13:11:29:44