2014-04-19 02:30:29 -04:00
|
|
|
package net.filebot.web;
|
2013-01-12 10:21:33 -05:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2015-01-03 16:16:13 -05:00
|
|
|
import java.util.Collection;
|
2014-11-11 03:51:20 -05:00
|
|
|
import java.util.LinkedHashMap;
|
2013-01-12 10:21:33 -05:00
|
|
|
import java.util.Map;
|
2013-12-18 10:42:42 -05:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2013-01-12 10:21:33 -05:00
|
|
|
|
|
|
|
import javax.swing.Icon;
|
|
|
|
|
2014-04-19 02:30:29 -04:00
|
|
|
import net.filebot.ResourceManager;
|
|
|
|
import net.filebot.mediainfo.MediaInfo;
|
|
|
|
import net.filebot.mediainfo.MediaInfo.StreamKind;
|
2013-01-12 10:21:33 -05:00
|
|
|
|
|
|
|
public class ID3Lookup implements MusicIdentificationService {
|
2013-11-14 08:24:30 -05:00
|
|
|
|
2013-01-12 10:21:33 -05:00
|
|
|
@Override
|
|
|
|
public String getName() {
|
2013-11-14 08:24:30 -05:00
|
|
|
return "ID3 Tags";
|
2013-01-12 10:21:33 -05:00
|
|
|
}
|
2013-11-14 08:24:30 -05:00
|
|
|
|
2013-01-12 10:21:33 -05:00
|
|
|
@Override
|
|
|
|
public Icon getIcon() {
|
2013-11-14 08:24:30 -05:00
|
|
|
return ResourceManager.getIcon("search.mediainfo");
|
2013-01-12 10:21:33 -05:00
|
|
|
}
|
2013-11-14 08:24:30 -05:00
|
|
|
|
2013-01-12 10:21:33 -05:00
|
|
|
@Override
|
2015-01-03 16:16:13 -05:00
|
|
|
public Map<File, AudioTrack> lookup(Collection<File> files) throws Exception {
|
2014-11-11 03:51:20 -05:00
|
|
|
Map<File, AudioTrack> info = new LinkedHashMap<File, AudioTrack>();
|
2013-11-14 08:24:30 -05:00
|
|
|
|
2013-01-12 10:21:33 -05:00
|
|
|
MediaInfo mediaInfo = new MediaInfo();
|
|
|
|
for (File f : files) {
|
|
|
|
if (!mediaInfo.open(f)) {
|
|
|
|
throw new IOException("MediaInfo failed to open file: " + f);
|
|
|
|
}
|
2013-11-14 08:24:30 -05:00
|
|
|
|
2013-12-18 10:42:42 -05:00
|
|
|
try {
|
2015-11-06 04:15:37 -05:00
|
|
|
// artist and song title information is required
|
2015-11-07 08:24:44 -05:00
|
|
|
String artist = getString(mediaInfo, "Performer", "Composer");
|
2015-11-06 04:15:37 -05:00
|
|
|
String title = getString(mediaInfo, "Title");
|
|
|
|
|
|
|
|
if (artist != null && title != null) {
|
|
|
|
// all other properties are optional
|
|
|
|
String album = getString(mediaInfo, "Album");
|
|
|
|
String albumArtist = getString(mediaInfo, "Album/Performer");
|
|
|
|
String trackTitle = null;
|
|
|
|
SimpleDate albumReleaseDate = null;
|
|
|
|
Integer mediumIndex = null;
|
|
|
|
Integer mediumCount = null;
|
|
|
|
Integer trackIndex = getInteger(mediaInfo, "Track/Position");
|
|
|
|
Integer trackCount = getInteger(mediaInfo, "Track/Position_Total");
|
|
|
|
String mbid = null;
|
|
|
|
|
|
|
|
Integer year = getInteger(mediaInfo, "Recorded_Date");
|
|
|
|
if (year != null) {
|
|
|
|
albumReleaseDate = new SimpleDate(year, 1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
info.put(f, new AudioTrack(artist, title, album, albumArtist, trackTitle, albumReleaseDate, mediumIndex, mediumCount, trackIndex, trackCount, mbid));
|
2013-12-18 10:42:42 -05:00
|
|
|
}
|
|
|
|
} catch (Throwable e) {
|
2015-11-06 04:15:37 -05:00
|
|
|
Logger.getLogger(ID3Lookup.class.getName()).log(Level.WARNING, e.toString());
|
2013-12-18 10:42:42 -05:00
|
|
|
} finally {
|
|
|
|
mediaInfo.close();
|
2013-12-07 03:05:35 -05:00
|
|
|
}
|
2013-01-12 10:21:33 -05:00
|
|
|
}
|
2013-11-14 08:24:30 -05:00
|
|
|
|
2013-01-12 10:21:33 -05:00
|
|
|
return info;
|
|
|
|
}
|
2015-11-06 04:15:37 -05:00
|
|
|
|
2015-11-07 08:24:44 -05:00
|
|
|
private String getString(MediaInfo mediaInfo, String... keys) {
|
|
|
|
for (String key : keys) {
|
|
|
|
String value = mediaInfo.get(StreamKind.General, 0, key);
|
|
|
|
if (value.length() > 0) {
|
|
|
|
return value;
|
|
|
|
}
|
2015-11-06 04:15:37 -05:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Integer getInteger(MediaInfo mediaInfo, String field) {
|
|
|
|
String value = getString(mediaInfo, field);
|
|
|
|
if (value != null) {
|
|
|
|
try {
|
|
|
|
return new Integer(value);
|
|
|
|
} catch (Exception e) {
|
|
|
|
Logger.getLogger(ID3Lookup.class.getName()).log(Level.WARNING, e.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-01-12 10:21:33 -05:00
|
|
|
}
|