1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-12-23 08:18:52 -05:00

Experiment with reading iTunes metadata (TV Show type)

This commit is contained in:
Reinhard Pointner 2017-01-05 21:50:09 +08:00
parent ba772e03c8
commit 4217dc0dd1

View File

@ -6,9 +6,11 @@ import static net.filebot.util.FileUtilities.*;
import static net.filebot.util.StringUtilities.*;
import java.io.File;
import java.io.FileFilter;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;
import javax.swing.Icon;
@ -34,46 +36,25 @@ public class ID3Lookup implements MusicIdentificationService {
}
@Override
public Map<File, AudioTrack> lookup(Collection<File> files) throws Exception {
Map<File, AudioTrack> info = new LinkedHashMap<File, AudioTrack>();
public Map<File, AudioTrack> lookup(Collection<File> files) {
return read(files, this::getAudioTrack, AUDIO_FILES, VIDEO_FILES);
}
try (MediaInfo mediaInfo = new MediaInfo()) {
for (File f : filter(files, AUDIO_FILES, VIDEO_FILES)) {
private <T> Map<File, T> read(Collection<File> files, Function<MediaInfo, T> parse, FileFilter... filters) {
Map<File, T> info = new LinkedHashMap<File, T>(files.size());
try (MediaInfo m = new MediaInfo()) {
for (File f : filter(files, filters)) {
try {
// open or throw exception
mediaInfo.open(f);
m.open(f);
// artist and song title information is required
String artist = getString(mediaInfo, "Performer", "Composer");
String title = getString(mediaInfo, "Title", "Track");
if (artist != null && title != null) {
// all other properties are optional
String album = getString(mediaInfo, "Album");
String albumArtist = getString(mediaInfo, "Album/Performer");
String trackTitle = getString(mediaInfo, "Track");
Integer mediumIndex = null;
Integer mediumCount = null;
Integer trackIndex = getInteger(mediaInfo, "Track/Position");
Integer trackCount = getInteger(mediaInfo, "Track/Position_Total");
String mbid = getString(mediaInfo, "Acoustid Id");
// try to parse 2016-03-10
String dateString = getString(mediaInfo, "Recorded_Date");
SimpleDate albumReleaseDate = SimpleDate.parse(dateString);
// try to parse 2016
if (albumReleaseDate == null) {
Integer year = matchInteger(dateString);
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, getIdentifier()));
T object = parse.apply(m);
if (object != null) {
info.put(f, object);
}
} catch (Throwable e) {
debug.warning(e.getMessage());
debug.warning(e::getMessage);
}
}
}
@ -81,6 +62,73 @@ public class ID3Lookup implements MusicIdentificationService {
return info;
}
public AudioTrack getAudioTrack(File file) {
return read(file, this::getAudioTrack);
}
public Episode Episode(File file) {
return read(file, this::getEpisode);
}
private <T> T read(File file, Function<MediaInfo, T> parse) {
try (MediaInfo m = new MediaInfo()) {
try {
return parse.apply(m.open(file));
} catch (Throwable e) {
debug.warning(e::getMessage);
}
}
return null;
}
public AudioTrack getAudioTrack(MediaInfo m) {
// artist and song title information is required
String artist = getString(m, "Performer", "Composer");
String title = getString(m, "Title", "Track");
if (artist == null || title == null) {
return null;
}
// all other properties are optional
String album = getString(m, "Album");
String albumArtist = getString(m, "Album/Performer");
String trackTitle = getString(m, "Track");
Integer mediumIndex = null;
Integer mediumCount = null;
Integer trackIndex = getInteger(m, "Track/Position");
Integer trackCount = getInteger(m, "Track/Position_Total");
String mbid = getString(m, "Acoustid Id");
// try to parse 2016-03-10
String dateString = getString(m, "Recorded_Date");
SimpleDate albumReleaseDate = SimpleDate.parse(dateString);
// try to parse 2016
if (albumReleaseDate == null) {
Integer year = matchInteger(dateString);
if (year != null) {
albumReleaseDate = new SimpleDate(year, 1, 1);
}
}
return new AudioTrack(artist, title, album, albumArtist, trackTitle, albumReleaseDate, mediumIndex, mediumCount, trackIndex, trackCount, mbid, getIdentifier());
}
public Episode getEpisode(MediaInfo m) {
String series = getString(m, "Album");
Integer season = getInteger(m, "Season");
Integer episode = getInteger(m, "Part", "Track/Position");
String title = getString(m, "Title", "Track");
if (series == null || episode == null) {
return null;
}
return new Episode(series, season, episode, title);
}
private String getString(MediaInfo mediaInfo, String... keys) {
for (String key : keys) {
String value = mediaInfo.get(StreamKind.General, 0, key);
@ -91,8 +139,8 @@ public class ID3Lookup implements MusicIdentificationService {
return null;
}
private Integer getInteger(MediaInfo mediaInfo, String field) {
return matchInteger(getString(mediaInfo, field));
private Integer getInteger(MediaInfo mediaInfo, String... keys) {
return matchInteger(getString(mediaInfo, keys));
}
}