1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-12-23 16:28:51 -05:00

Refactor media info cache synchronization

This commit is contained in:
Reinhard Pointner 2016-08-10 03:26:55 +08:00
parent 8f12961fe8
commit 53226d0809
2 changed files with 26 additions and 32 deletions

View File

@ -1013,7 +1013,7 @@ public class MediaBindingBean {
return getMediaFile();
}
private static final Map<File, MediaInfo> sharedMediaInfoObjects = new WeakValueHashMap<File, MediaInfo>(64);
private static final Map<File, MediaInfo> sharedMediaInfoObjects = synchronizedMap(new WeakValueHashMap<File, MediaInfo>(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;

View File

@ -163,7 +163,7 @@ public enum SubtitleMetrics implements SimilarityMetric {
private Map<String, Object> getSubtitleProperties(OpenSubtitlesSubtitleDescriptor subtitle) {
try {
Map<String, Object> props = new HashMap<String, Object>();
Map<String, Object> props = new HashMap<String, Object>(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<File, Map<String, Object>> mediaInfoCache = new WeakHashMap<File, Map<String, Object>>(64);
private final Map<File, Map<String, Object>> mediaInfoCache = synchronizedMap(new WeakHashMap<File, Map<String, Object>>(64));
private Map<String, Object> getVideoProperties(File file) {
synchronized (mediaInfoCache) {
return mediaInfoCache.computeIfAbsent(file, (f) -> {
try {
Map<String, Object> props = new HashMap<String, Object>();
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<String, Object> props = new HashMap<String, Object>(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();
});
}
});