diff --git a/source/net/filebot/format/MediaBindingBean.java b/source/net/filebot/format/MediaBindingBean.java index 7bedef63..8f78e13a 100644 --- a/source/net/filebot/format/MediaBindingBean.java +++ b/source/net/filebot/format/MediaBindingBean.java @@ -53,6 +53,7 @@ import net.filebot.Settings; import net.filebot.hash.HashType; import net.filebot.media.MetaAttributes; import net.filebot.media.NamingStandard; +import net.filebot.media.VideoFormat; import net.filebot.mediainfo.ImageMetadata; import net.filebot.mediainfo.MediaInfo; import net.filebot.mediainfo.MediaInfo.StreamKind; @@ -352,27 +353,11 @@ public class MediaBindingBean { @Define("vf") public String getVideoFormat() { - int width = Integer.parseInt(getMediaInfo(StreamKind.Video, 0, "Width")); - int height = Integer.parseInt(getMediaInfo(StreamKind.Video, 0, "Height")); + int w = Integer.parseInt(getMediaInfo(StreamKind.Video, 0, "Width")); + int h = Integer.parseInt(getMediaInfo(StreamKind.Video, 0, "Height")); - int[] ws = new int[] { 15360, 7680, 3840, 1920, 1280, 1024, 854, 748, 720, 688, 512, 320 }; - int[] hs = new int[] { 8640, 4320, 2160, 1080, 720, 576, 576, 480, 480, 360, 240, 240 }; - - int ns = 0; - - for (int i = 0; i < ws.length - 1; i++) { - if ((width >= ws[i] || height >= hs[i]) || (width > ws[i + 1] && height > hs[i + 1])) { - ns = hs[i]; - break; - } - } - - if (ns > 0) { - // e.g. 720p, nobody actually wants files to be tagged as interlaced, e.g. 720i - return String.format("%dp", ns); - } - - return null; // video too small + // e.g. 720p, nobody actually wants files to be tagged as interlaced, e.g. 720i + return String.format("%dp", VideoFormat.DEFAULT_GROUPS.guessFormat(w, h)); } @Define("hpi") diff --git a/source/net/filebot/media/VideoFormat.java b/source/net/filebot/media/VideoFormat.java new file mode 100644 index 00000000..1fd29dd8 --- /dev/null +++ b/source/net/filebot/media/VideoFormat.java @@ -0,0 +1,39 @@ +package net.filebot.media; + +import static java.util.ResourceBundle.*; +import static net.filebot.util.RegularExpressions.*; + +public class VideoFormat { + + public static final VideoFormat DEFAULT_GROUPS = new VideoFormat(); + + private final int[] ws; + private final int[] hs; + + public VideoFormat() { + this.ws = getIntArrayProperty("resolution.steps.w"); + this.hs = getIntArrayProperty("resolution.steps.h"); + } + + public int guessFormat(int width, int height) { + int ns = 0; + + for (int i = 0; i < ws.length - 1; i++) { + if ((width >= ws[i] || height >= hs[i]) || (width > ws[i + 1] && height > hs[i + 1])) { + ns = hs[i]; + break; + } + } + + if (ns > 0) { + return ns; + } + + throw new IllegalArgumentException(String.format("Illegal resolution: [%d, %d]", width, height)); + } + + private int[] getIntArrayProperty(String key) { + return SPACE.splitAsStream(getBundle(getClass().getName()).getString(key)).mapToInt(Integer::parseInt).toArray(); + } + +} diff --git a/test/net/filebot/AllTests.java b/test/net/filebot/AllTests.java index b0ddeafa..37326a1f 100644 --- a/test/net/filebot/AllTests.java +++ b/test/net/filebot/AllTests.java @@ -8,6 +8,7 @@ import net.filebot.format.ExpressionFormatTest; import net.filebot.hash.VerificationFormatTest; import net.filebot.media.MediaDetectionTest; import net.filebot.media.ReleaseInfoTest; +import net.filebot.media.VideoFormatTest; import net.filebot.mediainfo.MediaInfoTest; import net.filebot.similarity.EpisodeMetricsTest; import net.filebot.similarity.SimilarityTestSuite; @@ -18,7 +19,7 @@ import net.filebot.util.UtilTestSuite; import net.filebot.web.WebTestSuite; @RunWith(Suite.class) -@SuiteClasses({ ExpressionFormatTest.class, VerificationFormatTest.class, MatchModelTest.class, SupportDialogTest.class, EpisodeMetricsTest.class, ReleaseInfoTest.class, MediaDetectionTest.class, MediaInfoTest.class, SimilarityTestSuite.class, WebTestSuite.class, SubtitleReaderTestSuite.class, UtilTestSuite.class }) +@SuiteClasses({ ExpressionFormatTest.class, VerificationFormatTest.class, MatchModelTest.class, SupportDialogTest.class, EpisodeMetricsTest.class, ReleaseInfoTest.class, VideoFormatTest.class, MediaDetectionTest.class, MediaInfoTest.class, SimilarityTestSuite.class, WebTestSuite.class, SubtitleReaderTestSuite.class, UtilTestSuite.class }) public class AllTests { } diff --git a/test/net/filebot/media/VideoFormatTest.java b/test/net/filebot/media/VideoFormatTest.java new file mode 100644 index 00000000..277630c2 --- /dev/null +++ b/test/net/filebot/media/VideoFormatTest.java @@ -0,0 +1,18 @@ +package net.filebot.media; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class VideoFormatTest { + + VideoFormat vf = new VideoFormat(); + + @Test + public void trickyResolutions() { + assertEquals(1080, vf.guessFormat(1920, 1040)); + assertEquals(720, vf.guessFormat(1280, 528)); + assertEquals(576, vf.guessFormat(748, 574)); + } + +}