mirror of
https://github.com/mitb-archive/filebot
synced 2025-01-10 13:28:02 -05:00
Refactor VideoQuality
This commit is contained in:
parent
e2d9b9dd81
commit
11ad79db06
@ -302,25 +302,23 @@ public class ReleaseInfo {
|
|||||||
public Pattern getVideoFormatPattern(boolean strict) {
|
public Pattern getVideoFormatPattern(boolean strict) {
|
||||||
// pattern matching any video source name
|
// pattern matching any video source name
|
||||||
String pattern = getProperty("pattern.video.format");
|
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() {
|
public Pattern getVideoSourcePattern() {
|
||||||
// pattern matching any video source name, like BluRay
|
return compileWordPattern(getProperty("pattern.video.source")); // pattern matching any video source name, like BluRay
|
||||||
String pattern = getProperty("pattern.video.source");
|
|
||||||
return compile("(?<!\\p{Alnum})(" + pattern + ")(?!\\p{Alnum})", CASE_INSENSITIVE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Pattern getVideoTagPattern() {
|
public Pattern getVideoTagPattern() {
|
||||||
// pattern matching any video tag, like Directors Cut
|
return compileWordPattern(getProperty("pattern.video.tags")); // pattern matching any video tag, like Directors Cut
|
||||||
String pattern = getProperty("pattern.video.tags");
|
|
||||||
return compile("(?<!\\p{Alnum})(" + pattern + ")(?!\\p{Alnum})", CASE_INSENSITIVE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Pattern getStereoscopic3DPattern() {
|
public Pattern getStereoscopic3DPattern() {
|
||||||
// pattern matching any 3D flags like 3D.HSBS
|
return compileWordPattern(getProperty("pattern.video.s3d")); // pattern matching any 3D flags like 3D.HSBS
|
||||||
String pattern = getProperty("pattern.video.s3d");
|
}
|
||||||
return compile("(?<!\\p{Alnum})(" + pattern + ")(?!\\p{Alnum})", CASE_INSENSITIVE);
|
|
||||||
|
public Pattern getRepackPattern() {
|
||||||
|
return compileWordPattern(getProperty("pattern.video.repack"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Pattern getClutterBracketPattern(boolean strict) {
|
public Pattern getClutterBracketPattern(boolean strict) {
|
||||||
@ -352,17 +350,23 @@ public class ReleaseInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Pattern getBlacklistPattern() throws Exception {
|
public Pattern getBlacklistPattern() throws Exception {
|
||||||
// pattern matching any release group name enclosed in separators
|
return compileWordPattern(queryBlacklist.get()); // pattern matching any release group name enclosed in separators
|
||||||
return compile("(?<!\\p{Alnum})" + or(queryBlacklist.get()) + "(?!\\p{Alnum})", CASE_INSENSITIVE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Pattern getExcludePattern() throws Exception {
|
public Pattern getExcludePattern() throws Exception {
|
||||||
// pattern matching any release group name enclosed in separators
|
return compileWordPattern(excludeBlacklist.get()); // pattern matching any release group name enclosed in separators
|
||||||
return compile(or(excludeBlacklist.get()), CASE_INSENSITIVE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Pattern getCustomRemovePattern(Collection<String> terms) throws IOException {
|
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 {
|
public Map<Pattern, String> getSeriesMappings() throws Exception {
|
||||||
|
@ -7,6 +7,9 @@ pattern.video.tags: Extended|Uncensored|Remastered|Unrated|Uncut|IMAX|(Ultimate.
|
|||||||
# patterns for stereoscopic 3D tags
|
# patterns for stereoscopic 3D tags
|
||||||
pattern.video.s3d: ((H|HALF|F|FULL)[^\\p{Alnum}]{0,2})?(SBS|TAB|OU)
|
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
|
# patterns for all subtitle tags
|
||||||
pattern.subtitle.tags: forced|HI|SDH
|
pattern.subtitle.tags: forced|HI|SDH
|
||||||
|
|
||||||
|
@ -3,65 +3,53 @@ package net.filebot.media;
|
|||||||
import static java.util.Comparator.*;
|
import static java.util.Comparator.*;
|
||||||
import static net.filebot.Logging.*;
|
import static net.filebot.Logging.*;
|
||||||
import static net.filebot.MediaTypes.*;
|
import static net.filebot.MediaTypes.*;
|
||||||
|
import static net.filebot.media.MediaDetection.*;
|
||||||
import static net.filebot.util.StringUtilities.*;
|
import static net.filebot.util.StringUtilities.*;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.function.ToDoubleFunction;
|
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
import net.filebot.format.MediaBindingBean;
|
import net.filebot.format.MediaBindingBean;
|
||||||
|
|
||||||
public class VideoQuality implements Comparator<File> {
|
public class VideoQuality implements Comparator<File> {
|
||||||
|
|
||||||
|
public static final Comparator<File> DESCENDING_ORDER = new VideoQuality().reversed();
|
||||||
|
|
||||||
public static boolean isBetter(File f1, File f2) {
|
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
|
@Override
|
||||||
public int compare(File f1, File f2) {
|
public int compare(File f1, File f2) {
|
||||||
ToDoubleFunction<File> repack = this::isRepack;
|
return chain.compare(f1, f2);
|
||||||
ToDoubleFunction<File> resolution = this::getResolution;
|
}
|
||||||
ToDoubleFunction<File> size = this::getSize;
|
|
||||||
|
|
||||||
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 {
|
try {
|
||||||
return comparingDouble(c).compare(f1, f2);
|
return m.getDimension().stream().mapToInt(Number::intValue).reduce((a, b) -> a * b).orElse(0);
|
||||||
} catch (Throwable e) {
|
} catch (Exception e) {
|
||||||
debug.warning(format("Failed to read media info: %s", e));
|
debug.warning("Failed to read media info: " + e);
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
}).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 long getSize(MediaBindingBean m) {
|
||||||
private static final double ZERO = 0;
|
return m.getInferredMediaFile().length();
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user