1 17 package org.alfresco.repo.content.metadata; 18 19 import java.io.File ; 20 import java.io.Serializable ; 21 import java.util.HashMap ; 22 import java.util.Map ; 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 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 <QName, Serializable > destination) throws Throwable 61 { 62 Map <QName, Serializable > props = new HashMap <QName, Serializable >(); 63 64 File tempFile = TempFileProvider.createTempFile("MP3MetadataExtracter_", ".tmp"); 66 try 67 { 68 reader.getContent(tempFile); 69 70 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 85 } 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 } 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 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 description = getDescription(props); 137 if (description != null) 138 { 139 destination.put(ContentModel.PROP_DESCRIPTION, description); 140 } 141 } 142 143 144 150 private String getDescription(Map <QName, Serializable > props) 151 { 152 StringBuilder result = new StringBuilder (); 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 174 private void setTagValue(Map <QName, Serializable > props, QName propQName, String propvalue) 175 { 176 if (propvalue != null && propvalue.length() != 0) 177 { 178 trimPut(propQName, propvalue, props); 179 } 180 } 181 182 188 private String getLyrics3v2Value(Lyrics3v2 lyrics3Tag, String name) 189 { 190 String 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 )body.getObject("Text"); 198 } 199 } 200 return result; 201 } 202 203 210 private String getID3V2Value(AbstractID3v2 id3v2, String name) 211 { 212 String 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 )body.getObject("Text"); 221 } 222 } 223 224 return result; 225 } 226 227 } 228 | Popular Tags |