Advertisement
filebot

VideoQuality.java

Jun 30th, 2023
1,257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | Source Code | 0 0
  1. package net.filebot.media;
  2.  
  3. import static java.util.Comparator.*;
  4. import static net.filebot.MediaTypes.*;
  5. import static net.filebot.media.CachedMediaCharacteristics.*;
  6. import static net.filebot.media.MediaDetection.*;
  7. import static net.filebot.media.MediaFileUtilities.*;
  8. import static net.filebot.util.StringUtilities.*;
  9.  
  10. import java.io.File;
  11. import java.util.Comparator;
  12. import java.util.regex.Pattern;
  13.  
  14. public class VideoQuality implements Comparator<File> {
  15.  
  16.     public static final Comparator<File> DESCENDING_ORDER = new VideoQuality(VideoFormat.DEFAULT_GROUPS, releaseInfo.getRepackPattern()).reversed();
  17.  
  18.     public static boolean isBetter(File f1, File f2) {
  19.         return DESCENDING_ORDER.compare(f1, f2) < 0;
  20.     }
  21.  
  22.     private final VideoFormat quality;
  23.     private final Pattern repack;
  24.  
  25.     public VideoQuality(VideoFormat quality, Pattern repack) {
  26.         this.quality = quality;
  27.         this.repack = repack;
  28.     }
  29.  
  30.     private final Comparator<File> chain = comparingInt(this::getQuality)
  31.             .thenComparing(VideoCodec::compare)
  32.             .thenComparingInt(this::getRepack)
  33.             .thenComparingDouble(this::getScore)
  34.             .thenComparingLong(File::length);
  35.  
  36.     @Override
  37.     public int compare(File f1, File f2) {
  38.         return chain.compare(getPrimaryFile(f1), getPrimaryFile(f2));
  39.     }
  40.  
  41.     private File getPrimaryFile(File f) {
  42.         return findPrimaryFile(f, VIDEO_FILES).orElse(f);
  43.     }
  44.  
  45.     private int getRepack(File f) {
  46.         return find(f.getName(), repack) ? 1 : 0;
  47.     }
  48.  
  49.     private int getQuality(File f) {
  50.         // use primary video file when checking video resolution of subtitle files or disk folders
  51.         return getMediaCharacteristics(f, mi -> {
  52.             Integer w = mi.getWidth();
  53.             Integer h = mi.getHeight();
  54.  
  55.             // invalid media file
  56.             if (w == null || h == null) {
  57.                 return null;
  58.             }
  59.  
  60.             return quality.guessFormat(w, h);
  61.         }).orElse(Integer.MIN_VALUE);
  62.     }
  63.  
  64.     private double getScore(File f) {
  65.         // use primary video file when checking video resolution of subtitle files or disk folders
  66.         return getMediaCharacteristics(f, mi -> {
  67.             Integer w = mi.getWidth();
  68.             Integer h = mi.getHeight();
  69.             Double br = mi.getBitRate();
  70.  
  71.             // invalid media file
  72.             if (w == null || h == null || br == null) {
  73.                 return null;
  74.             }
  75.  
  76.             return w * h * br;
  77.         }).orElse(Double.MIN_VALUE);
  78.     }
  79.  
  80.     public enum VideoCodec {
  81.  
  82.         MPEG, AVC, HEVC;
  83.  
  84.         public static VideoCodec get(MediaCharacteristics mi) {
  85.             switch (mi.getVideoCodec()) {
  86.                 case "HEVC":
  87.                     return HEVC;
  88.                 case "AVC":
  89.                     return AVC;
  90.                 case "MPEG-4 Visual":
  91.                     return MPEG;
  92.             }
  93.  
  94.             // print missing codec name
  95.             throw new UnsupportedOperationException("Unknown Video Codec: " + mi.getVideoCodec());
  96.         }
  97.  
  98.         public static int compare(File f1, File f2) {
  99.             VideoCodec vc1 = getMediaCharacteristics(f1, VideoCodec::get).orElse(null);
  100.             VideoCodec vc2 = getMediaCharacteristics(f2, VideoCodec::get).orElse(null);
  101.  
  102.             if (vc1 == null || vc2 == null) {
  103.                 return 0;
  104.             }
  105.  
  106.             return vc1.compareTo(vc2);
  107.         }
  108.  
  109.     }
  110.  
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement