From 86b7c4e4fd9fa86025973aad946034a64a5b36e6 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 25 Feb 2017 17:31:49 +0800 Subject: [PATCH] Added {exif} binding --- .../net/filebot/format/MediaBindingBean.java | 8 ++++- .../net/filebot/mediainfo/ImageMetadata.java | 32 +++++++++---------- source/net/filebot/mediainfo/MediaInfo.java | 10 ++++-- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/source/net/filebot/format/MediaBindingBean.java b/source/net/filebot/format/MediaBindingBean.java index b02ff6f1..fea5a108 100644 --- a/source/net/filebot/format/MediaBindingBean.java +++ b/source/net/filebot/format/MediaBindingBean.java @@ -51,6 +51,7 @@ import net.filebot.Settings; import net.filebot.hash.HashType; import net.filebot.media.MetaAttributes; import net.filebot.media.NamingStandard; +import net.filebot.mediainfo.ImageMetadata; import net.filebot.mediainfo.MediaInfo; import net.filebot.mediainfo.MediaInfo.StreamKind; import net.filebot.mediainfo.MediaInfoException; @@ -465,7 +466,7 @@ public class MediaBindingBean { // calculate checksum from file Cache cache = Cache.getCache("crc32", CacheType.Ephemeral); - return (String) cache.computeIfAbsent(inferredMediaFile.getCanonicalPath(), it -> crc32(inferredMediaFile)); + return (String) cache.computeIfAbsent(inferredMediaFile, it -> crc32(inferredMediaFile)); } @Define("fn") @@ -828,6 +829,11 @@ public class MediaBindingBean { return createMediaInfoBindings(StreamKind.Chapters); } + @Define("exif") + public AssociativeScriptObject getImageMetadata() throws Exception { + return new AssociativeScriptObject(new ImageMetadata(getMediaFile())); + } + @Define("artist") public String getArtist() { return getMusic().getArtist(); diff --git a/source/net/filebot/mediainfo/ImageMetadata.java b/source/net/filebot/mediainfo/ImageMetadata.java index 94844cdb..494857ff 100644 --- a/source/net/filebot/mediainfo/ImageMetadata.java +++ b/source/net/filebot/mediainfo/ImageMetadata.java @@ -4,35 +4,33 @@ import static net.filebot.Logging.*; import static net.filebot.similarity.Normalization.*; import java.io.File; +import java.io.IOException; import java.util.LinkedHashMap; import com.drew.imaging.ImageMetadataReader; +import com.drew.imaging.ImageProcessingException; import com.drew.metadata.Directory; import com.drew.metadata.Metadata; import com.drew.metadata.Tag; public class ImageMetadata extends LinkedHashMap { - public ImageMetadata(File file) { - try { - Metadata metadata = ImageMetadataReader.readMetadata(file); + public ImageMetadata(File file) throws ImageProcessingException, IOException { + Metadata metadata = ImageMetadataReader.readMetadata(file); - for (Directory directory : metadata.getDirectories()) { - for (Tag tag : directory.getTags()) { - String v = tag.getDescription(); - if (v != null && v.length() > 0) { - putIfAbsent(normalizeSpace(normalizePunctuation(tag.getTagName()), "_"), v); - } - } - - if (directory.hasErrors()) { - for (String error : directory.getErrors()) { - debug.warning(error); - } + for (Directory directory : metadata.getDirectories()) { + for (Tag tag : directory.getTags()) { + String v = tag.getDescription(); + if (v != null && v.length() > 0) { + putIfAbsent(normalizeSpace(normalizePunctuation(tag.getTagName()), "_"), v); + } + } + + if (directory.hasErrors()) { + for (String error : directory.getErrors()) { + debug.warning(error); } } - } catch (Throwable e) { - debug.warning(e::toString); } } diff --git a/source/net/filebot/mediainfo/MediaInfo.java b/source/net/filebot/mediainfo/MediaInfo.java index 8476ed1c..2fb77f29 100644 --- a/source/net/filebot/mediainfo/MediaInfo.java +++ b/source/net/filebot/mediainfo/MediaInfo.java @@ -1,6 +1,7 @@ package net.filebot.mediainfo; import static java.nio.charset.StandardCharsets.*; +import static net.filebot.Logging.*; import java.io.Closeable; import java.io.File; @@ -151,8 +152,13 @@ public class MediaInfo implements Closeable { // MediaInfo does not support EXIF image metadata natively so we use the metadata-extractor library and implicitly merge that information in if (streamKind == StreamKind.Image && streamNumber == 0) { - ImageMetadata exif = new ImageMetadata(new File(get(StreamKind.General, 0, "CompleteName"))); - exif.forEach(streamInfo::putIfAbsent); + String path = get(StreamKind.General, 0, "CompleteName"); + try { + ImageMetadata exif = new ImageMetadata(new File(path)); + exif.forEach(streamInfo::putIfAbsent); + } catch (Throwable e) { + debug.warning(format("%s: %s", e, path)); + } } return streamInfo;