Experiment with reading iTunes metadata (TV Show type)
This commit is contained in:
parent
ba772e03c8
commit
4217dc0dd1
|
@ -6,9 +6,11 @@ import static net.filebot.util.FileUtilities.*;
|
||||||
import static net.filebot.util.StringUtilities.*;
|
import static net.filebot.util.StringUtilities.*;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileFilter;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
import javax.swing.Icon;
|
import javax.swing.Icon;
|
||||||
|
|
||||||
|
@ -34,46 +36,25 @@ public class ID3Lookup implements MusicIdentificationService {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<File, AudioTrack> lookup(Collection<File> files) throws Exception {
|
public Map<File, AudioTrack> lookup(Collection<File> files) {
|
||||||
Map<File, AudioTrack> info = new LinkedHashMap<File, AudioTrack>();
|
return read(files, this::getAudioTrack, AUDIO_FILES, VIDEO_FILES);
|
||||||
|
}
|
||||||
|
|
||||||
try (MediaInfo mediaInfo = new MediaInfo()) {
|
private <T> Map<File, T> read(Collection<File> files, Function<MediaInfo, T> parse, FileFilter... filters) {
|
||||||
for (File f : filter(files, AUDIO_FILES, VIDEO_FILES)) {
|
Map<File, T> info = new LinkedHashMap<File, T>(files.size());
|
||||||
|
|
||||||
|
try (MediaInfo m = new MediaInfo()) {
|
||||||
|
for (File f : filter(files, filters)) {
|
||||||
try {
|
try {
|
||||||
// open or throw exception
|
// open or throw exception
|
||||||
mediaInfo.open(f);
|
m.open(f);
|
||||||
|
|
||||||
// artist and song title information is required
|
T object = parse.apply(m);
|
||||||
String artist = getString(mediaInfo, "Performer", "Composer");
|
if (object != null) {
|
||||||
String title = getString(mediaInfo, "Title", "Track");
|
info.put(f, object);
|
||||||
|
|
||||||
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()));
|
|
||||||
}
|
}
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
debug.warning(e.getMessage());
|
debug.warning(e::getMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,6 +62,73 @@ public class ID3Lookup implements MusicIdentificationService {
|
||||||
return info;
|
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) {
|
private String getString(MediaInfo mediaInfo, String... keys) {
|
||||||
for (String key : keys) {
|
for (String key : keys) {
|
||||||
String value = mediaInfo.get(StreamKind.General, 0, key);
|
String value = mediaInfo.get(StreamKind.General, 0, key);
|
||||||
|
@ -91,8 +139,8 @@ public class ID3Lookup implements MusicIdentificationService {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Integer getInteger(MediaInfo mediaInfo, String field) {
|
private Integer getInteger(MediaInfo mediaInfo, String... keys) {
|
||||||
return matchInteger(getString(mediaInfo, field));
|
return matchInteger(getString(mediaInfo, keys));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue