From 11ad79db0682731ea07721c2fe32581ae8cd4cd1 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Mon, 24 Oct 2016 23:43:20 +0800 Subject: [PATCH] Refactor VideoQuality --- source/net/filebot/media/ReleaseInfo.java | 34 +++++----- .../net/filebot/media/ReleaseInfo.properties | 3 + source/net/filebot/media/VideoQuality.java | 64 ++++++++----------- 3 files changed, 48 insertions(+), 53 deletions(-) diff --git a/source/net/filebot/media/ReleaseInfo.java b/source/net/filebot/media/ReleaseInfo.java index e72ef285..5766f9c4 100644 --- a/source/net/filebot/media/ReleaseInfo.java +++ b/source/net/filebot/media/ReleaseInfo.java @@ -302,25 +302,23 @@ public class ReleaseInfo { public Pattern getVideoFormatPattern(boolean strict) { // pattern matching any video source name String pattern = getProperty("pattern.video.format"); - return strict ? compile("(? terms) throws IOException { - return compile("(? getSeriesMappings() throws Exception { diff --git a/source/net/filebot/media/ReleaseInfo.properties b/source/net/filebot/media/ReleaseInfo.properties index 3cf1935b..e9d81d05 100644 --- a/source/net/filebot/media/ReleaseInfo.properties +++ b/source/net/filebot/media/ReleaseInfo.properties @@ -7,6 +7,9 @@ pattern.video.tags: Extended|Uncensored|Remastered|Unrated|Uncut|IMAX|(Ultimate. # patterns for stereoscopic 3D tags pattern.video.s3d: ((H|HALF|F|FULL)[^\\p{Alnum}]{0,2})?(SBS|TAB|OU) +# patterns for repack tags +pattern.video.repack: REPACK|PROPER + # patterns for all subtitle tags pattern.subtitle.tags: forced|HI|SDH diff --git a/source/net/filebot/media/VideoQuality.java b/source/net/filebot/media/VideoQuality.java index 946ffc25..f0587ea7 100644 --- a/source/net/filebot/media/VideoQuality.java +++ b/source/net/filebot/media/VideoQuality.java @@ -3,65 +3,53 @@ 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.StringUtilities.*; import java.io.File; import java.util.Comparator; -import java.util.Optional; -import java.util.function.ToDoubleFunction; import java.util.regex.Pattern; -import java.util.stream.Stream; import net.filebot.format.MediaBindingBean; public class VideoQuality implements Comparator { + public static final Comparator DESCENDING_ORDER = new VideoQuality().reversed(); + public static boolean isBetter(File f1, File f2) { - return new VideoQuality().compare(f1, f2) > 0; + return DESCENDING_ORDER.compare(f1, f2) < 0; } + private final Comparator chain = comparing(f -> new MediaBindingBean(f, f), comparingInt(this::getRepack).thenComparingInt(this::getResolution).thenComparingLong(this::getSize)); + @Override public int compare(File f1, File f2) { - ToDoubleFunction repack = this::isRepack; - ToDoubleFunction resolution = this::getResolution; - ToDoubleFunction size = this::getSize; + return chain.compare(f1, f2); + } - return Stream.of(repack, resolution, size).mapToInt(c -> { + private final Pattern repack = releaseInfo.getRepackPattern(); + + private int getRepack(MediaBindingBean m) { + return find(m.getFileName(), repack) || find(m.getOriginalFileName(), repack) ? 1 : 0; + } + + private int getResolution(MediaBindingBean m) { + // use video file for video/subtitle pairs when comparing the subtitle file + File mediaFile = m.getInferredMediaFile(); + + if (VIDEO_FILES.accept(mediaFile)) { try { - return comparingDouble(c).compare(f1, f2); - } catch (Throwable e) { - debug.warning(format("Failed to read media info: %s", e)); - return 0; + return m.getDimension().stream().mapToInt(Number::intValue).reduce((a, b) -> a * b).orElse(0); + } catch (Exception e) { + debug.warning("Failed to read media info: " + e); } - }).filter(i -> i != 0).findFirst().orElse(0); - } - - private Optional media(File f) { - if (VIDEO_FILES.accept(f) || SUBTITLE_FILES.accept(f)) { - return Optional.of(new MediaBindingBean(f, f)); } - return Optional.empty(); + + return 0; } - private static final Pattern REPACK = Pattern.compile("(? { - return find(it.getFileName(), REPACK) || find(it.getOriginalFileName(), REPACK) ? 1 : ZERO; - }).orElse(ZERO); - } - - public double getResolution(File f) { - return media(f).map(it -> { - return it.getDimension().stream().mapToDouble(Number::doubleValue).reduce((a, b) -> a * b).orElse(ZERO); - }).orElse(ZERO); - } - - public double getSize(File f) { - return media(f).map(it -> { - return it.getInferredMediaFile().length(); - }).orElseGet(f::length); + private long getSize(MediaBindingBean m) { + return m.getInferredMediaFile().length(); } }