Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: Parsing ID3 tags from mp3

  1. #1
    Junior Member
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Parsing ID3 tags from mp3

    Hi all, i'm new here.

    Just have a question regarding the parsing of ID3 tags. Basically i'm trying to write a program that parses the ID3 tags from an mp3. Just curious does anyone know of any good resources on how to do this?

    Thanks

    John


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Parsing ID3 tags

    Hello John and welcome to the Java programming forums

    What do you mean by parse? Are you looking to print an mp3s ID3 tag information to the console?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Parsing ID3 tags

    Quote Originally Posted by JavaPF View Post
    Hello John and welcome to the Java programming forums

    What do you mean by parse? Are you looking to print an mp3s ID3 tag information to the console?
    Hey thanks for the reply

    Yeah thats basically what i need to do. I need to print the ID3 tag info in which i'll probably use a GUI to display the info, but i'm a bit of a novice with java and was just curious if anyone knew of any good resources on how to do this.

    Thanks

    John

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Parsing ID3 tags

    I haven't tested this code myself but try this:

    import java.io.*; 
     
        public class ReadID3 { 
           public static void main(String[] arguments) { 
             try { 
                File song = new File("."); 
                File [] list = song.listFiles();
                for(int i=0;i<list.length;i++)
                {
                   FileInputStream file = new FileInputStream(list[i]); 
                   int size = (int)list[i].length(); 
                   file.skip(size - 128); 
                   byte[] last128 = new byte[128]; 
                   file.read(last128); 
                   String id3 = new String(last128); 
                   String tag = id3.substring(0, 3); 
                   if (tag.equals("TAG")) { 
                      System.out.println("Title: " + id3.substring(3, 32)); 
                      System.out.println("Artist: " + id3.substring(33, 62)); 
                      System.out.println("Album: " + id3.substring(63, 91)); 
                      System.out.println("Year: " + id3.substring(93, 97)); 
                   } 
                   else 
                      System.out.println(" does not contain" 
                         + " ID3 info.");  
                   file.close();
                } 
             } 
                 catch (Exception e) { 
                   System.out.println("Error ? " + e.toString()); 
                }
     
          } 
       }
    You need to place the .mp3 files in the application directory to get this program to work correctly.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. The Following User Says Thank You to JavaPF For This Useful Post:

    John (April 16th, 2009)

  6. #5
    Junior Member
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Parsing ID3 tags

    wow cheers for the quick response and the code.

    i'll have a mess around with it and then get back to you

    Thanks

  7. #6

    Default Re: Parsing ID3 tags

    Hi,

    I recommend to use the JavaMusictag library: Java ID3 Tag Library

    Download the jar from here.

    You only need to add the jar to the class path and then you can write this very simple code:

    public static void main(String[] args) throws IOException, TagException {
    		File sourceFile = new File("C://song.mp3");
    		MP3File mp3file = new MP3File(sourceFile);
    		ID3v1 tag = mp3file.getID3v1Tag();
    		System.out.println(tag.getAlbum());
    		System.out.println(tag.getAlbumTitle());
    		System.out.println(tag.getTitle());
    	}

    With this library you can handle specific tags version (ID3v1_1, ID3v2_4, etc).

    Also is really easy to create id3 tags.

    More info here: Introduction to tags

    (JavaPF I hope you don't be upset because I provide another solution)

  8. The Following User Says Thank You to leandro For This Useful Post:

    John (April 16th, 2009)

  9. #7
    Junior Member
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Parsing ID3 tags

    Thanks leandro,

    I will be sure to check that out as well.

    I'm open to all solutions! lol

    Thanks again

  10. #8
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Parsing ID3 tags

    lol leandro, of course I do not mind! The more solutions the better...
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  11. #9
    Junior Member
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Parsing ID3 tags

    Quote Originally Posted by JavaPF View Post
    I haven't tested this code myself but try this:

    import java.io.*; 
     
        public class ReadID3 { 
           public static void main(String[] arguments) { 
             try { 
                File song = new File("."); 
                File [] list = song.listFiles();
                for(int i=0;i<list.length;i++)
                {
                   FileInputStream file = new FileInputStream(list[i]); 
                   int size = (int)list[i].length(); 
                   file.skip(size - 128); 
                   byte[] last128 = new byte[128]; 
                   file.read(last128); 
                   String id3 = new String(last128); 
                   String tag = id3.substring(0, 3); 
                   if (tag.equals("TAG")) { 
                      System.out.println("Title: " + id3.substring(3, 32)); 
                      System.out.println("Artist: " + id3.substring(33, 62)); 
                      System.out.println("Album: " + id3.substring(63, 91)); 
                      System.out.println("Year: " + id3.substring(93, 97)); 
                   } 
                   else 
                      System.out.println(" does not contain" 
                         + " ID3 info.");  
                   file.close();
                } 
             } 
                 catch (Exception e) { 
                   System.out.println("Error ? " + e.toString()); 
                }
     
          } 
       }
    You need to place the .mp3 files in the application directory to get this program to work correctly.
    Hey JavaPF

    I appear to be getting the following error whenever i try to run the program:
    Error ? java.lang.NullPointerException
    Basically i've copied the mp3 into the directory were the program is saved and then input the file name were it says File song = new File(".");

    Do you know why this is happening? The program itself doesn't bring up any errors when i build it, only when i run it.

    Thanks

    John

  12. #10
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Parsing ID3 tags

    You do not need to edit this code to include the file name, keep it the same and make sure the file is in the correct directory.

    Your Java workspace will contain a 'bin' and 'src' directory. Place your MP3 in the same place.

    I am currently at work so I cannot test this myself.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  13. #11
    Junior Member
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Parsing ID3 tags

    Ahh right i see, got you now

    It worked, thanks

  14. #12
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Parsing ID3 tags

    Quote Originally Posted by John View Post
    Ahh right i see, got you now

    It worked, thanks
    Brilliant. Does it work as expected?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  15. #13
    Junior Member
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Parsing ID3 tags

    Quote Originally Posted by JavaPF View Post
    Brilliant. Does it work as expected?
    Yeah it did thanks. It printed the artist and track title which was all the file contained.

    On the subject i was also trying to print the content of a .wmdb (windows media database) file. This is basically a file used to store information about all the media that has been played using wmp. I read that .wmdb files also store some media metadata in the form of ID3 tags, however it doesn't appear so when viewing the file with notepad. The file appears to store it in what looks like html tags. For instance the following is some data taken from the file:

    < t r a c k >

    < W M C o n t e n t I D > 8 f 2 7 9 1 6 6 - 2 6 1 d - 4 a 9 f - 8 a f 1 - 3 b d 0 e a 7 e 3 d 0 5 < / W M C o n t e n t I D >

    < t r a c k T i t l e > S q u a r e O n e < / t r a c k T i t l e >

    < u n i q u e F i l e I D > A M G p _ i d = P 4 3 5 0 2 3 ; A M G t _ i d = T 7 7 3 2 1 6 9 < / u n i q u e F i l e I D >

    < t r a c k N u m b e r > 1 < / t r a c k N u m b e r >

    < t r a c k P e r f o r m e r > C o l d p l a y < / t r a c k P e r f o r m e r >

    < e x p l i c i t L y r i c s > 0 < / e x p l i c i t L y r i c s >

    < / t r a c k >

    The file however contains much more data than this, but the above is just an example of what you would expect to find.

    I hope i'm not bugging you guys with these questions, but i'm just curious how would i go about printing this data?

    Thanks

    John

  16. #14
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Parsing ID3 tags

    This looks a lot like XML.

    There is a tutorial on parsing XML here:

    http://www.javaprogrammingforums.com...dom-parse.html

    But you might be able to read the file in line by line and manipulate the data.

    http://www.javaprogrammingforums.com...line-line.html
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  17. #15
    Junior Member
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Parsing ID3 tags

    Quote Originally Posted by JavaPF View Post
    This looks a lot like XML.

    There is a tutorial on parsing XML here:

    http://www.javaprogrammingforums.com...dom-parse.html

    But you might be able to read the file in line by line and manipulate the data.

    http://www.javaprogrammingforums.com...line-line.html
    Cool i will check that out

    Thanks again for your help

    John

Similar Threads

  1. [SOLVED] Java code to embedding xml tags at start and end of file
    By John in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 30th, 2009, 03:02 PM
  2. Printing xml to the console from .wmdb without printing junks
    By John in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: April 24th, 2009, 03:44 AM

Tags for this Thread