KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > content > metadata > MP3MetadataExtracter


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.content.metadata;
18
19 import java.io.File JavaDoc;
20 import java.io.Serializable JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.alfresco.model.ContentModel;
25 import org.alfresco.repo.content.MimetypeMap;
26 import org.alfresco.service.cmr.repository.ContentReader;
27 import org.alfresco.service.namespace.QName;
28 import org.alfresco.util.TempFileProvider;
29 import org.farng.mp3.AbstractMP3FragmentBody;
30 import org.farng.mp3.MP3File;
31 import org.farng.mp3.id3.AbstractID3v2;
32 import org.farng.mp3.id3.AbstractID3v2Frame;
33 import org.farng.mp3.id3.ID3v1;
34 import org.farng.mp3.lyrics3.AbstractLyrics3;
35 import org.farng.mp3.lyrics3.Lyrics3v2;
36 import org.farng.mp3.lyrics3.Lyrics3v2Field;
37
38 /**
39  * @author Roy Wetherall
40  */

41 public class MP3MetadataExtracter extends AbstractMetadataExtracter
42 {
43     private static final QName PROP_ALBUM_TITLE = QName.createQName("{music}albumTitle");
44     private static final QName PROP_SONG_TITLE = QName.createQName("{music}songTitle");;
45     private static final QName PROP_ARTIST = QName.createQName("{music}artist");;
46     private static final QName PROP_COMMENT = QName.createQName("{music}comment");;
47     private static final QName PROP_YEAR_RELEASED = QName.createQName("{music}yearReleased");;
48     private static final QName PROP_TRACK_NUMBER = QName.createQName("{music}trackNumber");;
49     private static final QName PROP_GENRE = QName.createQName("{music}genre");;
50     private static final QName PROP_COMPOSER = QName.createQName("{music}composer");;
51     private static final QName PROP_LYRICS = QName.createQName("{music}lyrics");;
52
53     public MP3MetadataExtracter()
54     {
55         super(MimetypeMap.MIMETYPE_MP3, 1.0, 1000);
56     }
57
58     public void extractInternal(
59             ContentReader reader,
60             Map JavaDoc<QName, Serializable JavaDoc> destination) throws Throwable JavaDoc
61     {
62         Map JavaDoc<QName, Serializable JavaDoc> props = new HashMap JavaDoc<QName, Serializable JavaDoc>();
63         
64         // Create a temp file
65
File JavaDoc tempFile = TempFileProvider.createTempFile("MP3MetadataExtracter_", ".tmp");
66         try
67         {
68             reader.getContent(tempFile);
69             
70             // Create the MP3 object from the file
71
MP3File mp3File = new MP3File(tempFile);
72             
73             ID3v1 id3v1 = mp3File.getID3v1Tag();
74             if (id3v1 != null)
75             {
76                 setTagValue(props, PROP_ALBUM_TITLE, id3v1.getAlbum());
77                 setTagValue(props, PROP_SONG_TITLE, id3v1.getTitle());
78                 setTagValue(props, PROP_ARTIST, id3v1.getArtist());
79                 setTagValue(props, PROP_COMMENT, id3v1.getComment());
80                 setTagValue(props, PROP_YEAR_RELEASED, id3v1.getYear());
81                 
82                 // TODO sort out the genre
83
//setTagValue(props, MusicModel.PROP_GENRE, id3v1.getGenre());
84

85                 // TODO sort out the size
86
//setTagValue(props, MusicModel.PROP_SIZE, id3v1.getSize());
87
}
88             
89             AbstractID3v2 id3v2 = mp3File.getID3v2Tag();
90             if (id3v2 != null)
91             {
92                 setTagValue(props, PROP_SONG_TITLE, getID3V2Value(id3v2, "TIT2"));
93                 setTagValue(props, PROP_ARTIST, getID3V2Value(id3v2, "TPE1"));
94                 setTagValue(props, PROP_ALBUM_TITLE, getID3V2Value(id3v2, "TALB"));
95                 setTagValue(props, PROP_YEAR_RELEASED, getID3V2Value(id3v2, "TDRC"));
96                 setTagValue(props, PROP_COMMENT, getID3V2Value(id3v2, "COMM"));
97                 setTagValue(props, PROP_TRACK_NUMBER, getID3V2Value(id3v2, "TRCK"));
98                 setTagValue(props, PROP_GENRE, getID3V2Value(id3v2, "TCON"));
99                 setTagValue(props, PROP_COMPOSER, getID3V2Value(id3v2, "TCOM"));
100                 
101                 // TODO sort out the lyrics
102
//System.out.println("Lyrics: " + getID3V2Value(id3v2, "SYLT"));
103
//System.out.println("Lyrics: " + getID3V2Value(id3v2, "USLT"));
104
}
105             
106             AbstractLyrics3 lyrics3Tag = mp3File.getLyrics3Tag();
107             if (lyrics3Tag != null)
108             {
109                 System.out.println("Lyrics3 tag found.");
110                 if (lyrics3Tag instanceof Lyrics3v2)
111                 {
112                     setTagValue(props, PROP_SONG_TITLE, getLyrics3v2Value((Lyrics3v2)lyrics3Tag, "TIT2"));
113                     setTagValue(props, PROP_ARTIST, getLyrics3v2Value((Lyrics3v2)lyrics3Tag, "TPE1"));
114                     setTagValue(props, PROP_ALBUM_TITLE, getLyrics3v2Value((Lyrics3v2)lyrics3Tag, "TALB"));
115                     setTagValue(props, PROP_COMMENT, getLyrics3v2Value((Lyrics3v2)lyrics3Tag, "COMM"));
116                     setTagValue(props, PROP_LYRICS, getLyrics3v2Value((Lyrics3v2)lyrics3Tag, "SYLT"));
117                     setTagValue(props, PROP_COMPOSER, getLyrics3v2Value((Lyrics3v2)lyrics3Tag, "TCOM"));
118                 }
119             }
120             
121         }
122         finally
123         {
124             tempFile.delete();
125         }
126         
127         // Set the destination values
128
if (props.get(PROP_SONG_TITLE) != null)
129         {
130             destination.put(ContentModel.PROP_TITLE, props.get(PROP_SONG_TITLE));
131         }
132         if (props.get(PROP_ARTIST) != null)
133         {
134             destination.put(ContentModel.PROP_AUTHOR, props.get(PROP_ARTIST));
135         }
136         String JavaDoc description = getDescription(props);
137         if (description != null)
138         {
139             destination.put(ContentModel.PROP_DESCRIPTION, description);
140         }
141     }
142     
143
144     /**
145      * Generate the description
146      *
147      * @param props the properties extracted from the file
148      * @return the description
149      */

150     private String JavaDoc getDescription(Map JavaDoc<QName, Serializable JavaDoc> props)
151     {
152         StringBuilder JavaDoc result = new StringBuilder JavaDoc();
153         if (props.get(PROP_SONG_TITLE) != null && props.get(PROP_ARTIST) != null && props.get(PROP_ALBUM_TITLE) != null)
154         {
155             result
156                 .append(props.get(PROP_SONG_TITLE))
157                 .append(" - ")
158                 .append(props.get(PROP_ALBUM_TITLE))
159                 .append(" (")
160                 .append(props.get(PROP_ARTIST))
161                 .append(")");
162                 
163         }
164         
165         return result.toString();
166     }
167
168     /**
169      *
170      * @param props
171      * @param propQName
172      * @param propvalue
173      */

174     private void setTagValue(Map JavaDoc<QName, Serializable JavaDoc> props, QName propQName, String JavaDoc propvalue)
175     {
176         if (propvalue != null && propvalue.length() != 0)
177         {
178             trimPut(propQName, propvalue, props);
179         }
180     }
181
182     /**
183      *
184      * @param lyrics3Tag
185      * @param name
186      * @return
187      */

188     private String JavaDoc getLyrics3v2Value(Lyrics3v2 lyrics3Tag, String JavaDoc name)
189     {
190         String JavaDoc result = "";
191         Lyrics3v2Field field = lyrics3Tag.getField(name);
192         if (field != null)
193         {
194             AbstractMP3FragmentBody body = field.getBody();
195             if (body != null)
196             {
197                 result = (String JavaDoc)body.getObject("Text");
198             }
199         }
200         return result;
201     }
202
203     /**
204      * Get the ID3V2 tag value in a safe way
205      *
206      * @param id3v2
207      * @param name
208      * @return
209      */

210     private String JavaDoc getID3V2Value(AbstractID3v2 id3v2, String JavaDoc name)
211     {
212         String JavaDoc result = "";
213         
214         AbstractID3v2Frame frame = id3v2.getFrame(name);
215         if (frame != null)
216         {
217             AbstractMP3FragmentBody body = frame.getBody();
218             if (body != null)
219             {
220                 result = (String JavaDoc)body.getObject("Text");
221             }
222         }
223         
224         return result;
225     }
226
227 }
228
Popular Tags