Refactor VideoQuality

This commit is contained in:
Reinhard Pointner 2016-10-24 23:43:20 +08:00
parent e2d9b9dd81
commit 11ad79db06
3 changed files with 48 additions and 53 deletions

View File

@ -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("(?<!\\p{Alnum})(" + pattern + ")(?!\\p{Alnum})", CASE_INSENSITIVE) : compile(pattern, CASE_INSENSITIVE);
return strict ? compileWordPattern(pattern) : compile(pattern, CASE_INSENSITIVE);
}
public Pattern getVideoSourcePattern() {
// pattern matching any video source name, like BluRay
String pattern = getProperty("pattern.video.source");
return compile("(?<!\\p{Alnum})(" + pattern + ")(?!\\p{Alnum})", CASE_INSENSITIVE);
return compileWordPattern(getProperty("pattern.video.source")); // pattern matching any video source name, like BluRay
}
public Pattern getVideoTagPattern() {
// pattern matching any video tag, like Directors Cut
String pattern = getProperty("pattern.video.tags");
return compile("(?<!\\p{Alnum})(" + pattern + ")(?!\\p{Alnum})", CASE_INSENSITIVE);
return compileWordPattern(getProperty("pattern.video.tags")); // pattern matching any video tag, like Directors Cut
}
public Pattern getStereoscopic3DPattern() {
// pattern matching any 3D flags like 3D.HSBS
String pattern = getProperty("pattern.video.s3d");
return compile("(?<!\\p{Alnum})(" + pattern + ")(?!\\p{Alnum})", CASE_INSENSITIVE);
return compileWordPattern(getProperty("pattern.video.s3d")); // pattern matching any 3D flags like 3D.HSBS
}
public Pattern getRepackPattern() {
return compileWordPattern(getProperty("pattern.video.repack"));
}
public Pattern getClutterBracketPattern(boolean strict) {
@ -352,17 +350,23 @@ public class ReleaseInfo {
}
public Pattern getBlacklistPattern() throws Exception {
// pattern matching any release group name enclosed in separators
return compile("(?<!\\p{Alnum})" + or(queryBlacklist.get()) + "(?!\\p{Alnum})", CASE_INSENSITIVE);
return compileWordPattern(queryBlacklist.get()); // pattern matching any release group name enclosed in separators
}
public Pattern getExcludePattern() throws Exception {
// pattern matching any release group name enclosed in separators
return compile(or(excludeBlacklist.get()), CASE_INSENSITIVE);
return compileWordPattern(excludeBlacklist.get()); // pattern matching any release group name enclosed in separators
}
public Pattern getCustomRemovePattern(Collection<String> terms) throws IOException {
return compile("(?<!\\p{Alnum})" + or(quoteAll(terms)) + "(?!\\p{Alnum})", CASE_INSENSITIVE);
return compileWordPattern(quoteAll(terms));
}
private Pattern compileWordPattern(String[] patterns) {
return compile("(?<!\\p{Alnum})" + or(patterns) + "(?!\\p{Alnum})", CASE_INSENSITIVE); // use | to join patterns
}
private Pattern compileWordPattern(String pattern) {
return compile("(?<!\\p{Alnum})(" + pattern + ")(?!\\p{Alnum})", CASE_INSENSITIVE);
}
public Map<Pattern, String> getSeriesMappings() throws Exception {

View File

@ -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

View File

@ -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<File> {
public static final Comparator<File> 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<File> 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<File> repack = this::isRepack;
ToDoubleFunction<File> resolution = this::getResolution;
ToDoubleFunction<File> 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<MediaBindingBean> 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("(?<!\\p{Alnum})(PROPER|REPACK)(?!\\p{Alnum})", Pattern.CASE_INSENSITIVE);
private static final double ZERO = 0;
public double isRepack(File f) {
return media(f).map(it -> {
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();
}
}