From 61780da8a98723ce9fa0d085474625d62f2af849 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Wed, 30 Mar 2016 08:47:09 +0000 Subject: [PATCH] Refactor --- source/net/filebot/cli/CmdlineOperations.java | 1 + source/net/filebot/media/MediaDetection.java | 52 ------------------- source/net/filebot/media/MediaSize.java | 49 +++++++++++++++++ 3 files changed, 50 insertions(+), 52 deletions(-) create mode 100644 source/net/filebot/media/MediaSize.java diff --git a/source/net/filebot/cli/CmdlineOperations.java b/source/net/filebot/cli/CmdlineOperations.java index ba9c788a..307d78a8 100644 --- a/source/net/filebot/cli/CmdlineOperations.java +++ b/source/net/filebot/cli/CmdlineOperations.java @@ -10,6 +10,7 @@ import static net.filebot.Settings.*; import static net.filebot.WebServices.*; import static net.filebot.hash.VerificationUtilities.*; import static net.filebot.media.MediaDetection.*; +import static net.filebot.media.MediaSize.*; import static net.filebot.media.XattrMetaInfo.*; import static net.filebot.subtitle.SubtitleUtilities.*; import static net.filebot.util.FileUtilities.*; diff --git a/source/net/filebot/media/MediaDetection.java b/source/net/filebot/media/MediaDetection.java index b29f4333..ed1bc74c 100644 --- a/source/net/filebot/media/MediaDetection.java +++ b/source/net/filebot/media/MediaDetection.java @@ -21,9 +21,7 @@ import java.net.URL; import java.text.CollationKey; import java.text.Collator; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; -import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -47,7 +45,6 @@ import java.util.regex.Pattern; import net.filebot.Resource; import net.filebot.WebServices; import net.filebot.archive.Archive; -import net.filebot.format.MediaBindingBean; import net.filebot.similarity.CommonSequenceMatcher; import net.filebot.similarity.DateMatcher; import net.filebot.similarity.EpisodeMetrics; @@ -1408,55 +1405,6 @@ public class MediaDetection { } } - public static Comparator VIDEO_SIZE_ORDER = new Comparator() { - - @Override - public int compare(File f1, File f2) { - long[] v1 = getSizeValues(f1); - long[] v2 = getSizeValues(f2); - - for (int i = 0; i < v1.length; i++) { - // best to worst - int d = new Long(v1[i]).compareTo(new Long(v2[i])); - if (d != 0) { - return d; - } - } - return 0; - } - - public long[] getSizeValues(File f) { - long[] v = new long[] { 0, 0 }; - - try { - if (VIDEO_FILES.accept(f) || SUBTITLE_FILES.accept(f)) { - MediaBindingBean media = new MediaBindingBean(null, f, null); - - // 1. Video Resolution - List dim = media.getDimension(); - v[0] = dim.get(0).longValue() * dim.get(1).longValue(); - - // 2. File Size - v[1] = media.getInferredMediaFile().length(); - } else if (AUDIO_FILES.accept(f)) { - // 1. Audio BitRate - v[0] = 0; - - // 2. File Size - v[1] = f.length(); - } - } catch (Exception e) { - // negative values for invalid files - debug.warning(format("Unable to read media info: %s [%s]", e.getMessage(), f.getName())); - - Arrays.fill(v, -1); - return v; - } - - return v; - } - }; - public static List getMediaUnits(File folder) { if (folder.isHidden()) { return emptyList(); diff --git a/source/net/filebot/media/MediaSize.java b/source/net/filebot/media/MediaSize.java new file mode 100644 index 00000000..6a3ce2ca --- /dev/null +++ b/source/net/filebot/media/MediaSize.java @@ -0,0 +1,49 @@ +package net.filebot.media; + +import static net.filebot.Logging.*; +import static net.filebot.MediaTypes.*; + +import java.io.File; +import java.util.Comparator; + +import net.filebot.format.MediaBindingBean; + +public class MediaSize implements Comparator { + + public static final Comparator VIDEO_SIZE_ORDER = new MediaSize(); + + @Override + public int compare(File f1, File f2) { + long[] v1 = getSizeValues(f1); + long[] v2 = getSizeValues(f2); + + // best to worst + for (int i = 0; i < v1.length; i++) { + int d = Long.compare(v1[i], v2[i]); + if (d != 0) { + return d; + } + } + + return 0; + } + + public long[] getSizeValues(File f) { + long[] v = { -1, -1 }; + + if (VIDEO_FILES.accept(f) || SUBTITLE_FILES.accept(f)) { + try { + MediaBindingBean media = new MediaBindingBean(null, f, null); + v[1] = media.getInferredMediaFile().length(); // File Size + v[0] = media.getDimension().stream().mapToLong(Number::longValue).reduce((a, b) -> a * b).orElse(-1); // Video Resolution + } catch (Throwable e) { + debug.warning(format("Failed to read media info: %s [%s]", e.getMessage(), f.getName())); + } + } else { + v[1] = f.length(); // File Size + } + + return v; + } + +}