Solved

reading mp3 id3 tags in java...

Posted on 2001-06-04
8
456 Views
Last Modified: 2010-03-31
I am trying to figure out how to read the id3 tag of an mp3 file with java. I think it is a fixed width thing at the end of the file... so is there a way to open a file and get to the end and work backwards or something?
0
Comment
Question by:AndyBeckwith
  • 5
  • 2
8 Comments
 
LVL 5

Expert Comment

by:Droby10
ID: 6154612
sure...it's been awhile, i'm going off the top of my head, and i don't have a reference available...so this may be a little off.

import java.io.*;

public static void main ( String args[] ) {
  try {
    File f = new File ( args[0] );
    FileInputStream fis = new FileInputStream ( f );
    int flen = fis.available();
    byte sig[] = new byte [ 3 ];
    byte artist[] = new byte [ 30 ];
    byte title[] = new byte [ 30 ];
    byte album[] = new byte [30];
    byte genre[] = new byte [1];
    byte comments = new byte [30];

    int id3len = sig.length + artist.length + title.length + album.length + genre.length + comment.length;

    fis.skip ( len - id3len );    
    fis.read ( sig );
    fis.read ( artist );
    fis.read ( title );
    fis.read ( album );
    fis.read ( genre );
    fis.read ( comments );
  } catch ( Exception e ) {
    e.printStackTrace();
  }
}


also, this code is for id3 version 1 only...which isn't used as much anymore...

id3 version 2 allowed title-streaming, embeded content...etc, and is much more robust...it's also much more difficult to filter as its embeded within the frames of an mp3 document.

0
 
LVL 5

Expert Comment

by:Droby10
ID: 6154619
just realized how ugly the code was...be sure to check verify after all of the reads...and close the inputstream at the end.
0
 

Author Comment

by:AndyBeckwith
ID: 6154690
do you have any info on the v2 tags?
0
 
LVL 5

Expert Comment

by:Droby10
ID: 6154714
no...but here's the definitive resource..

http://www.id3.org/
http://www.id3.org/develop.html
http://www.id3.org/id3v2.4.0-structure.txt

you'll also find links for source documentation at

http://id3lib.sourceforge.net/

-hope this helps.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 5

Expert Comment

by:Droby10
ID: 6154718
and actually here's some java source...

http://www.rabbitfarm.com/site/id3.html

0
 

Author Comment

by:AndyBeckwith
ID: 6155273
Back to your original code...

This is what I did, and it still wont work...

~~~ code ~~~
import java.io.*;
public class ReadId3 {

  public static void main ( String args[] ) {
    try {
      File f = new File ( args[0] );
      FileInputStream fis = new FileInputStream ( f );
      int flen = fis.available();
      byte [] sig = new byte [ 3 ];
      byte artist[] = new byte [ 30 ];
      byte [] title = new byte [ 30 ];
      byte album[] = new byte [30];
      byte genre[] = new byte [1];
      byte comments[] = new byte [30];

      int id3len = 124;

      fis.skip ( flen - id3len );    
      fis.read ( sig );
      fis.read ( artist );
      fis.read ( title );
      fis.read ( album );
      fis.read ( genre );
      fis.read ( comments );
    } catch ( Exception e ) {
      e.printStackTrace();
    }

    for( int i = 0; i < 30; i++ ){
      System.out.print( "" + title[ i ] );
    }
     
  }
}
~~~ end code ~~~

I still get this error:

~~~ error ~~~
ReadId3.java:31: cannot resolve symbol
symbol  : variable title
location: class ReadId3
        System.out.print( "" + title[ i ] );
                               ^
1 error
~~~ end error ~~~

I cant seem to force it to a string...
0
 
LVL 5

Accepted Solution

by:
Droby10 earned 100 total points
ID: 6155911
your print statement is outside of the try loop where title is defined...title is out-of-scope...if you predeclare it, it should work.

import java.io.*;
public class ReadId3 {

 public static void main ( String args[] ) {
   byte [] sig = new byte [ 3 ];
   byte artist[] = new byte [ 30 ];
   byte [] title = new byte [ 30 ];
   byte album[] = new byte [30];
   byte genre[] = new byte [1];
   byte comments[] = new byte [30];
   try {
     File f = new File ( args[0] );
     FileInputStream fis = new FileInputStream ( f );
     int flen = fis.available();
     int id3len = 124;

     fis.skip ( flen - id3len );    
     fis.read ( sig );
     fis.read ( artist );
     fis.read ( title );
     fis.read ( album );
     fis.read ( genre );
     fis.read ( comments );
   } catch ( Exception e ) {
     e.printStackTrace();
   }

   for( int i = 0; i < 30; i++ ){
     System.out.print( "" + title[ i ] );
   }
   
 }
}
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 8898913


No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:


--  Points to Droby10


Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
sudhakar_koundinya
EE Cleanup Volunteer
---------------------
If you feel that your question was not properly addressed, or that none of the comments received were appropriate answers, please post your concern in THIS thread.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now