View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.sequencer.mp3;
25  
26  import java.io.File;
27  import java.io.FileOutputStream;
28  import java.io.InputStream;
29  import java.util.logging.Level;
30  import org.jaudiotagger.audio.AudioFile;
31  import org.jaudiotagger.audio.AudioFileIO;
32  import org.jaudiotagger.tag.Tag;
33  
34  /**
35   * Utility for extracting metadata from MP3 files.
36   */
37  public class Mp3Metadata {
38  
39      private String title;
40      private String author;
41      private String album;
42      private String year;
43      private String comment;
44  
45      private Mp3Metadata() {
46  
47      }
48  
49      public static Mp3Metadata instance( InputStream stream ) {
50  
51          Mp3Metadata me = null;
52          File tmpFile = null;
53          try {
54              tmpFile = File.createTempFile("dna-sequencer-mp3", ".mp3");
55              FileOutputStream writer = new FileOutputStream(tmpFile);
56              byte[] b = new byte[128];
57              while (stream.read(b) != -1) {
58                  writer.write(b);
59              }
60              writer.close();
61              AudioFileIO.logger.getParent().setLevel(Level.OFF);
62              AudioFile f = AudioFileIO.read(tmpFile);
63              Tag tag = f.getTag();
64  
65              me = new Mp3Metadata();
66  
67              me.author = tag.getFirstArtist();
68              me.album = tag.getFirstAlbum();
69              me.title = tag.getFirstTitle();
70              me.comment = tag.getFirstComment();
71              me.year = tag.getFirstYear();
72  
73          } catch (Exception e) {
74              e.printStackTrace();
75          } finally {
76              if (tmpFile != null) {
77                  tmpFile.delete();
78              }
79          }
80          return me;
81  
82      }
83  
84      public String getTitle() {
85          return title;
86      }
87  
88      public String getAuthor() {
89          return author;
90      }
91  
92      public String getAlbum() {
93          return album;
94      }
95  
96      public String getYear() {
97          return year;
98      }
99  
100     public String getComment() {
101         return comment;
102     }
103 
104 }