1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-16 06:15:02 -05:00
filebot/source/net/filebot/web/ID3Lookup.java

96 lines
2.6 KiB
Java
Raw Normal View History

2014-04-19 02:30:29 -04:00
package net.filebot.web;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
2014-11-11 03:51:20 -05:00
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
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;
public class ID3Lookup implements MusicIdentificationService {
@Override
public String getName() {
return "ID3 Tags";
}
@Override
public Icon getIcon() {
return ResourceManager.getIcon("search.mediainfo");
}
@Override
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>();
MediaInfo mediaInfo = new MediaInfo();
for (File f : files) {
if (!mediaInfo.open(f)) {
throw new IOException("MediaInfo failed to open file: " + f);
}
try {
2015-11-06 04:15:37 -05:00
// artist and song title information is required
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));
}
} catch (Throwable e) {
2015-11-06 04:15:37 -05:00
Logger.getLogger(ID3Lookup.class.getName()).log(Level.WARNING, e.toString());
} finally {
mediaInfo.close();
}
}
return info;
}
2015-11-06 04:15:37 -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;
}
}