diff --git a/source/net/filebot/format/MediaBindingBean.java b/source/net/filebot/format/MediaBindingBean.java index 0f334106..ecb211a1 100644 --- a/source/net/filebot/format/MediaBindingBean.java +++ b/source/net/filebot/format/MediaBindingBean.java @@ -1013,7 +1013,7 @@ public class MediaBindingBean { return getMediaFile(); } - private static final Map sharedMediaInfoObjects = new WeakValueHashMap(64); + private static final Map sharedMediaInfoObjects = synchronizedMap(new WeakValueHashMap(64)); private synchronized MediaInfo getMediaInfo() { // lazy initialize @@ -1021,15 +1021,13 @@ public class MediaBindingBean { // use inferred media file (e.g. actual movie file instead of subtitle file) File inferredMediaFile = getInferredMediaFile(); - synchronized (sharedMediaInfoObjects) { - mediaInfo = sharedMediaInfoObjects.computeIfAbsent(inferredMediaFile, f -> { - try { - return new MediaInfo().open(f); - } catch (Exception e) { - throw new MediaInfoException(e.getMessage()); - } - }); - } + mediaInfo = sharedMediaInfoObjects.computeIfAbsent(inferredMediaFile, f -> { + try { + return new MediaInfo().open(f); + } catch (Exception e) { + throw new MediaInfoException(e.getMessage()); + } + }); } return mediaInfo; diff --git a/source/net/filebot/subtitle/SubtitleMetrics.java b/source/net/filebot/subtitle/SubtitleMetrics.java index 51b2dbd0..c21a14d8 100644 --- a/source/net/filebot/subtitle/SubtitleMetrics.java +++ b/source/net/filebot/subtitle/SubtitleMetrics.java @@ -163,7 +163,7 @@ public enum SubtitleMetrics implements SimilarityMetric { private Map getSubtitleProperties(OpenSubtitlesSubtitleDescriptor subtitle) { try { - Map props = new HashMap(); + 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); @@ -179,30 +179,26 @@ public enum SubtitleMetrics implements SimilarityMetric { return emptyMap(); } - private final Map> mediaInfoCache = new WeakHashMap>(64); + private final Map> mediaInfoCache = synchronizedMap(new WeakHashMap>(64)); private Map getVideoProperties(File file) { - synchronized (mediaInfoCache) { - return mediaInfoCache.computeIfAbsent(file, (f) -> { - try { - Map props = new HashMap(); - MediaInfo mediaInfo = new MediaInfo().open(file); - - 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; - } catch (Exception e) { - debug.warning("Failed to read video properties: " + e.getMessage()); + 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); } - return emptyMap(); - }); - } + long seconds = (long) Math.floor(Long.parseLong(mediaInfo.get(StreamKind.Video, 0, "Duration")) / (double) 1000); + if (seconds > 0) { + props.put(SECONDS, seconds); + } + return props; + } catch (Exception e) { + debug.warning("Failed to read video properties: " + e.getMessage()); + } + return emptyMap(); + }); } });