001    /*
002     * JBoss, Home of Professional Open Source.
003     * Copyright 2008, Red Hat Middleware LLC, and individual contributors
004     * as indicated by the @author tags. See the copyright.txt file in the
005     * distribution for a full listing of individual contributors. 
006     *
007     * This is free software; you can redistribute it and/or modify it
008     * under the terms of the GNU Lesser General Public License as
009     * published by the Free Software Foundation; either version 2.1 of
010     * the License, or (at your option) any later version.
011     *
012     * This software is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015     * Lesser General Public License for more details.
016     *
017     * You should have received a copy of the GNU Lesser General Public
018     * License along with this software; if not, write to the Free
019     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021     */
022    package org.jboss.dna.sequencer.mp3;
023    
024    import java.io.File;
025    import java.io.FileOutputStream;
026    import java.io.InputStream;
027    import java.util.logging.Level;
028    import org.jaudiotagger.audio.AudioFile;
029    import org.jaudiotagger.audio.AudioFileIO;
030    import org.jaudiotagger.tag.Tag;
031    
032    /**
033     * Utility for extracting metadata from MP3 files.
034     * 
035     * @author Stefano Maestri
036     */
037    public class Mp3Metadata {
038    
039        private String title;
040        private String author;
041        private String album;
042        private String year;
043        private String comment;
044    
045        private Mp3Metadata() {
046    
047        }
048    
049        public static Mp3Metadata instance( InputStream stream ) {
050    
051            Mp3Metadata me = null;
052            File tmpFile = null;
053            try {
054                tmpFile = File.createTempFile("dna-sequencer-mp3", ".mp3");
055                FileOutputStream writer = new FileOutputStream(tmpFile);
056                byte[] b = new byte[128];
057                while (stream.read(b) != -1) {
058                    writer.write(b);
059                }
060                writer.close();
061                AudioFileIO.logger.getParent().setLevel(Level.OFF);
062                AudioFile f = AudioFileIO.read(tmpFile);
063                Tag tag = f.getTag();
064    
065                me = new Mp3Metadata();
066    
067                me.author = tag.getFirstArtist();
068                me.album = tag.getFirstAlbum();
069                me.title = tag.getFirstTitle();
070                me.comment = tag.getFirstComment();
071                me.year = tag.getFirstYear();
072    
073            } catch (Exception e) {
074                e.printStackTrace();
075            } finally {
076                if (tmpFile != null) {
077                    tmpFile.delete();
078                }
079            }
080            return me;
081    
082        }
083    
084        public String getTitle() {
085            return title;
086        }
087    
088        public String getAuthor() {
089            return author;
090        }
091    
092        public String getAlbum() {
093            return album;
094        }
095    
096        public String getYear() {
097            return year;
098        }
099    
100        public String getComment() {
101            return comment;
102        }
103    
104    }