From 9dd4a82e04154a61642e01bf30d8da5faec79456 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Wed, 10 Aug 2016 03:42:32 +0800 Subject: [PATCH] Refactor SubtitleMetrics --- .../net/filebot/subtitle/SubtitleMetrics.java | 37 ++++++++----------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/source/net/filebot/subtitle/SubtitleMetrics.java b/source/net/filebot/subtitle/SubtitleMetrics.java index c21a14d8..fb7e81ab 100644 --- a/source/net/filebot/subtitle/SubtitleMetrics.java +++ b/source/net/filebot/subtitle/SubtitleMetrics.java @@ -161,18 +161,20 @@ public enum SubtitleMetrics implements SimilarityMetric { return emptyMap(); }; + private Map getProperties(float fps, long millis) { + Map props = new HashMap(2); + if (fps > 0) { + props.put(FPS, Math.round(fps)); // round because most FPS values in the database are bad anyway + } + if (millis > 0) { + props.put(SECONDS, Math.round(Math.floor(millis / 1000d))); + } + return props; + } + private Map getSubtitleProperties(OpenSubtitlesSubtitleDescriptor subtitle) { try { - Map props = new HashMap(2); - float fps = Math.round(subtitle.getMovieFPS()); // round because most FPS values in the database are bad anyway - if (fps > 0) { - props.put(FPS, fps); - } - long seconds = (long) Math.floor(subtitle.getMovieTimeMS() / (double) 1000); - if (seconds > 0) { - props.put(SECONDS, seconds); - } - return props; + return getProperties(subtitle.getMovieFPS(), subtitle.getMovieTimeMS()); } catch (Exception e) { debug.warning("Failed to read subtitle properties: " + e); } @@ -183,17 +185,10 @@ public enum SubtitleMetrics implements SimilarityMetric { private Map getVideoProperties(File file) { return mediaInfoCache.computeIfAbsent(file, key -> { - try (MediaInfo mediaInfo = new MediaInfo().open(file)) { - Map props = new HashMap(2); - float fps = Math.round(Float.parseFloat(mediaInfo.get(StreamKind.Video, 0, "FrameRate"))); - if (fps > 0) { - props.put(FPS, fps); - } - long seconds = (long) Math.floor(Long.parseLong(mediaInfo.get(StreamKind.Video, 0, "Duration")) / (double) 1000); - if (seconds > 0) { - props.put(SECONDS, seconds); - } - return props; + try (MediaInfo mi = new MediaInfo().open(file)) { + float fps = Float.parseFloat(mi.get(StreamKind.Video, 0, "FrameRate")); + long millis = Long.parseLong(mi.get(StreamKind.Video, 0, "Duration")); + return getProperties(fps, millis); } catch (Exception e) { debug.warning("Failed to read video properties: " + e.getMessage()); }