From 1a0fd86ba581df4868333898fa6d08ddda8a7aca Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Thu, 10 Mar 2016 06:47:51 +0000 Subject: [PATCH] Fail gracefully when doing ID3 lookups --- .../filebot/mediainfo/MediaInfoException.java | 4 ++ source/net/filebot/web/ID3Lookup.java | 65 ++++++++++--------- 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/source/net/filebot/mediainfo/MediaInfoException.java b/source/net/filebot/mediainfo/MediaInfoException.java index 467b804e..8c0992a1 100644 --- a/source/net/filebot/mediainfo/MediaInfoException.java +++ b/source/net/filebot/mediainfo/MediaInfoException.java @@ -4,6 +4,10 @@ import com.sun.jna.Platform; public class MediaInfoException extends RuntimeException { + public MediaInfoException(String message) { + super(message); + } + public MediaInfoException(LinkageError e) { super(getLinkageErrorMessage(e), e); } diff --git a/source/net/filebot/web/ID3Lookup.java b/source/net/filebot/web/ID3Lookup.java index f020af26..5162319d 100644 --- a/source/net/filebot/web/ID3Lookup.java +++ b/source/net/filebot/web/ID3Lookup.java @@ -1,10 +1,11 @@ package net.filebot.web; import static net.filebot.Logging.*; +import static net.filebot.MediaTypes.*; +import static net.filebot.util.FileUtilities.*; import static net.filebot.util.StringUtilities.*; import java.io.File; -import java.io.IOException; import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; @@ -15,6 +16,7 @@ import javax.swing.Icon; import net.filebot.ResourceManager; import net.filebot.mediainfo.MediaInfo; import net.filebot.mediainfo.MediaInfo.StreamKind; +import net.filebot.mediainfo.MediaInfoException; public class ID3Lookup implements MusicIdentificationService { @@ -32,40 +34,39 @@ public class ID3Lookup implements MusicIdentificationService { public Map lookup(Collection files) throws Exception { Map info = new LinkedHashMap(); - MediaInfo mediaInfo = new MediaInfo(); - for (File f : files) { - if (!mediaInfo.open(f)) { - throw new IOException("MediaInfo failed to open file: " + f); - } - - try { - // 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"); - 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); + try (MediaInfo mediaInfo = new MediaInfo()) { + for (File f : filter(files, AUDIO_FILES, VIDEO_FILES)) { + try { + if (!mediaInfo.open(f)) { + throw new MediaInfoException("Failed to read media info: " + f); } - info.put(f, new AudioTrack(artist, title, album, albumArtist, trackTitle, albumReleaseDate, mediumIndex, mediumCount, trackIndex, trackCount, mbid)); + // 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"); + 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) { + debug.warning(e.getMessage()); } - } catch (Throwable e) { - debug.log(Level.WARNING, e.toString()); - } finally { - mediaInfo.close(); } }