From 404ea2714a0157350d7e26aa3d74b25a3703fe07 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 15 Jun 2019 15:14:22 +0700 Subject: [PATCH] Accept small files (useful for automated image processing) --- source/net/filebot/media/AutoDetection.java | 2 +- .../media/MediaCharacteristicsParser.java | 7 ++++ source/net/filebot/media/MediaDetection.java | 2 +- source/net/filebot/media/VideoQuality.java | 4 +-- .../filebot/similarity/EpisodeMetrics.java | 2 +- .../net/filebot/subtitle/SubtitleMetrics.java | 10 +++--- .../net/filebot/ui/filter/MediaInfoTool.java | 32 +++++++++++-------- .../net/filebot/web/OpenSubtitlesClient.java | 12 ++++--- 8 files changed, 42 insertions(+), 29 deletions(-) diff --git a/source/net/filebot/media/AutoDetection.java b/source/net/filebot/media/AutoDetection.java index e32261d4..51a24480 100644 --- a/source/net/filebot/media/AutoDetection.java +++ b/source/net/filebot/media/AutoDetection.java @@ -118,7 +118,7 @@ public class AutoDetection { return true; } - if (VIDEO_FILES.accept(f) && f.length() > ONE_MEGABYTE) { + if (MediaCharacteristicsParser.DEFAULT.acceptVideoFile(f)) { // check for Japanese audio or characteristic subtitles try (MediaCharacteristics mi = MediaCharacteristicsParser.DEFAULT.open(f)) { return mi.getDuration().toMinutes() < 60 || find(mi.getAudioLanguage(), JAPANESE_AUDIO_LANGUAGE_PATTERN) && find(mi.getSubtitleCodec(), JAPANESE_SUBTITLE_CODEC_PATTERN); diff --git a/source/net/filebot/media/MediaCharacteristicsParser.java b/source/net/filebot/media/MediaCharacteristicsParser.java index c4c3c108..932f783e 100644 --- a/source/net/filebot/media/MediaCharacteristicsParser.java +++ b/source/net/filebot/media/MediaCharacteristicsParser.java @@ -1,5 +1,8 @@ package net.filebot.media; +import static net.filebot.MediaTypes.*; +import static net.filebot.util.FileUtilities.*; + import java.io.File; import net.filebot.mediainfo.MediaInfo; @@ -25,6 +28,10 @@ public enum MediaCharacteristicsParser { public abstract MediaCharacteristics open(File f) throws Exception; + public boolean acceptVideoFile(File f) { + return VIDEO_FILES.accept(f) && f.length() > ONE_MEGABYTE; + } + public static MediaCharacteristicsParser getDefault() { return DEFAULT; } diff --git a/source/net/filebot/media/MediaDetection.java b/source/net/filebot/media/MediaDetection.java index f4cb89d7..23bc282a 100644 --- a/source/net/filebot/media/MediaDetection.java +++ b/source/net/filebot/media/MediaDetection.java @@ -1113,7 +1113,7 @@ public class MediaDetection { } filesByExtension.stream().collect(groupingBy(f -> { - if (VIDEO_FILES.accept(f) && f.length() > ONE_MEGABYTE) { + if (MediaCharacteristicsParser.DEFAULT.acceptVideoFile(f)) { try (MediaCharacteristics mi = MediaCharacteristicsParser.DEFAULT.open(f)) { ChronoUnit d = mi.getDuration().toMinutes() < 10 ? ChronoUnit.MINUTES : ChronoUnit.HOURS; String v = mi.getVideoCodec(); diff --git a/source/net/filebot/media/VideoQuality.java b/source/net/filebot/media/VideoQuality.java index d12745c0..8f0d6d2a 100644 --- a/source/net/filebot/media/VideoQuality.java +++ b/source/net/filebot/media/VideoQuality.java @@ -2,9 +2,7 @@ package net.filebot.media; import static java.util.Comparator.*; import static net.filebot.Logging.*; -import static net.filebot.MediaTypes.*; import static net.filebot.media.MediaDetection.*; -import static net.filebot.util.FileUtilities.*; import static net.filebot.util.StringUtilities.*; import java.io.File; @@ -38,7 +36,7 @@ public class VideoQuality implements Comparator { // use primary video file when checking video resolution of subtitle files or disk folders f = new MediaBindingBean(f, f).getInferredMediaFile(); - if (VIDEO_FILES.accept(f) && f.length() > ONE_MEGABYTE) { + if (MediaCharacteristicsParser.DEFAULT.acceptVideoFile(f)) { try (MediaCharacteristics mi = MediaCharacteristicsParser.DEFAULT.open(f)) { return mi.getWidth() * mi.getHeight() * mi.getBitRate(); } catch (Exception e) { diff --git a/source/net/filebot/similarity/EpisodeMetrics.java b/source/net/filebot/similarity/EpisodeMetrics.java index d17af039..102676ae 100644 --- a/source/net/filebot/similarity/EpisodeMetrics.java +++ b/source/net/filebot/similarity/EpisodeMetrics.java @@ -614,7 +614,7 @@ public class EpisodeMetrics { private long getTimeStamp(File file) { return cache.computeIfAbsent(file, f -> { - if (VIDEO_FILES.accept(file) && file.length() > ONE_MEGABYTE) { + if (MediaCharacteristicsParser.DEFAULT.acceptVideoFile(f)) { try (MediaCharacteristics mi = MediaCharacteristicsParser.DEFAULT.open(file)) { Instant t = mi.getCreationTime(); if (t != null) { diff --git a/source/net/filebot/subtitle/SubtitleMetrics.java b/source/net/filebot/subtitle/SubtitleMetrics.java index 8cf86a9e..f377fb24 100644 --- a/source/net/filebot/subtitle/SubtitleMetrics.java +++ b/source/net/filebot/subtitle/SubtitleMetrics.java @@ -203,10 +203,12 @@ public class SubtitleMetrics extends EpisodeMetrics { private Map getVideoProperties(File file) { return cache.computeIfAbsent(file, f -> { - try (MediaCharacteristics mi = MediaCharacteristicsParser.DEFAULT.open(f)) { - return getProperties(mi.getFrameRate(), mi.getDuration().toMillis()); - } catch (Exception e) { - debug.warning(cause("Failed to read video properties", e)); + if (MediaCharacteristicsParser.DEFAULT.acceptVideoFile(f)) { + try (MediaCharacteristics mi = MediaCharacteristicsParser.DEFAULT.open(f)) { + return getProperties(mi.getFrameRate(), mi.getDuration().toMillis()); + } catch (Exception e) { + debug.warning(cause("Failed to read video properties", e)); + } } return emptyMap(); }); diff --git a/source/net/filebot/ui/filter/MediaInfoTool.java b/source/net/filebot/ui/filter/MediaInfoTool.java index 89c62834..26b8415a 100644 --- a/source/net/filebot/ui/filter/MediaInfoTool.java +++ b/source/net/filebot/ui/filter/MediaInfoTool.java @@ -59,25 +59,29 @@ class MediaInfoTool extends Tool { return new MediaInfoTableModel(); } - List files = listFiles(root, filter(VIDEO_FILES, AUDIO_FILES), HUMAN_NAME_ORDER); + List files = listFiles(root, filter(VIDEO_FILES, AUDIO_FILES, IMAGE_FILES), HUMAN_NAME_ORDER); Map data = new TreeMap(); try (MediaInfo mi = new MediaInfo()) { - IntStream.range(0, files.size()).forEach(f -> { - try { - mi.open(files.get(f)); - mi.snapshot().forEach((kind, streams) -> { - IntStream.range(0, streams.size()).forEach(i -> { - streams.get(i).forEach((name, value) -> { - String[] values = data.computeIfAbsent(new MediaInfoKey(kind, i, name), k -> new String[files.size()]); - values[f] = value; + IntStream.range(0, files.size()).forEach(fileIndex -> { + File f = files.get(fileIndex); + + if ((VIDEO_FILES.accept(f) && f.length() > ONE_MEGABYTE) || (AUDIO_FILES.accept(f) && f.length() > ONE_KILOBYTE) || (IMAGE_FILES.accept(f) && f.length() > 0)) { + try { + mi.open(f); + mi.snapshot().forEach((kind, streams) -> { + IntStream.range(0, streams.size()).forEach(streamIndex -> { + streams.get(streamIndex).forEach((name, value) -> { + String[] values = data.computeIfAbsent(new MediaInfoKey(kind, streamIndex, name), k -> new String[files.size()]); + values[fileIndex] = value; + }); }); }); - }); - } catch (IllegalArgumentException e) { - debug.finest(e::toString); - } catch (Exception e) { - debug.warning(e::toString); + } catch (IllegalArgumentException e) { + debug.finest(e::toString); + } catch (Exception e) { + debug.warning(e::toString); + } } if (Thread.interrupted()) { diff --git a/source/net/filebot/web/OpenSubtitlesClient.java b/source/net/filebot/web/OpenSubtitlesClient.java index 3b925cad..8f45067b 100644 --- a/source/net/filebot/web/OpenSubtitlesClient.java +++ b/source/net/filebot/web/OpenSubtitlesClient.java @@ -312,11 +312,13 @@ public class OpenSubtitlesClient implements SubtitleProvider, VideoHashSubtitleS sub.setSubContent(readFile(subtitleFile)); } - try (MediaCharacteristics mi = MediaCharacteristicsParser.DEFAULT.open(videoFile)) { - sub.setMovieFPS(String.valueOf(mi.getFrameRate())); - sub.setMovieTimeMS(String.valueOf(mi.getDuration().toMillis())); - } catch (Throwable e) { - debug.log(Level.SEVERE, "Failed to read media info", e); + if (MediaCharacteristicsParser.DEFAULT.acceptVideoFile(videoFile)) { + try (MediaCharacteristics mi = MediaCharacteristicsParser.DEFAULT.open(videoFile)) { + sub.setMovieFPS(String.valueOf(mi.getFrameRate())); + sub.setMovieTimeMS(String.valueOf(mi.getDuration().toMillis())); + } catch (Throwable e) { + debug.log(Level.SEVERE, "Failed to read media info", e); + } } return sub;