From bd826cb297a4a19269da272905b534e43d76deca Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 25 Feb 2017 19:13:33 +0800 Subject: [PATCH] {d} and {y} now evaluate to Exif Date-Taken if possible when dealing with generic files --- .classpath | 2 +- .../format/ExpressionFormatMethods.java | 3 +- .../net/filebot/format/MediaBindingBean.java | 21 ++++++++++---- .../net/filebot/mediainfo/ImageMetadata.java | 28 +++++++++++++++++++ 4 files changed, 46 insertions(+), 8 deletions(-) diff --git a/.classpath b/.classpath index f64665d9..541f0ea3 100644 --- a/.classpath +++ b/.classpath @@ -35,7 +35,7 @@ - + diff --git a/source/net/filebot/format/ExpressionFormatMethods.java b/source/net/filebot/format/ExpressionFormatMethods.java index 11484fa0..1f31f57e 100644 --- a/source/net/filebot/format/ExpressionFormatMethods.java +++ b/source/net/filebot/format/ExpressionFormatMethods.java @@ -10,7 +10,6 @@ import static net.filebot.media.MediaDetection.*; import static net.filebot.util.RegularExpressions.*; import java.io.File; -import java.io.IOException; import java.nio.file.Files; import java.nio.file.attribute.BasicFileAttributeView; import java.nio.file.attribute.BasicFileAttributes; @@ -505,7 +504,7 @@ public class ExpressionFormatMethods { return creationDate; } return attr.lastModifiedTime().toMillis(); - } catch (IOException e) { + } catch (Exception e) { throw new RuntimeException(e); } } diff --git a/source/net/filebot/format/MediaBindingBean.java b/source/net/filebot/format/MediaBindingBean.java index fea5a108..0bb9129b 100644 --- a/source/net/filebot/format/MediaBindingBean.java +++ b/source/net/filebot/format/MediaBindingBean.java @@ -125,10 +125,8 @@ public class MediaBindingBean { return getEpisode().getSeriesInfo().getStartDate().getYear(); if (infoObject instanceof Movie) return getMovie().getYear(); - if (infoObject instanceof AudioTrack) - return getMusic().getAlbumReleaseDate().getYear(); - return null; + return getReleaseDate().getYear(); } @Define("ny") @@ -204,8 +202,21 @@ public class MediaBindingBean { return getMovieInfo().getReleased(); if (infoObject instanceof AudioTrack) return getMusic().getAlbumReleaseDate(); - if (infoObject instanceof File) - return new SimpleDate(getCreationDate(((File) infoObject))); + + // try EXIF Date-Taken for image files or File Last-Modified for generic files + if (infoObject instanceof File) { + File f = (File) infoObject; + + ZonedDateTime d = ImageMetadata.getDateTaken(f); + if (d != null) { + return new SimpleDate(d); + } + + long t = getCreationDate(f); + if (t > 0) { + return new SimpleDate(t); + } + } return null; } diff --git a/source/net/filebot/mediainfo/ImageMetadata.java b/source/net/filebot/mediainfo/ImageMetadata.java index 494857ff..c973615d 100644 --- a/source/net/filebot/mediainfo/ImageMetadata.java +++ b/source/net/filebot/mediainfo/ImageMetadata.java @@ -4,7 +4,11 @@ import static net.filebot.Logging.*; import static net.filebot.similarity.Normalization.*; import java.io.File; +import java.io.FileFilter; import java.io.IOException; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.util.Date; import java.util.LinkedHashMap; import com.drew.imaging.ImageMetadataReader; @@ -12,6 +16,10 @@ import com.drew.imaging.ImageProcessingException; import com.drew.metadata.Directory; import com.drew.metadata.Metadata; import com.drew.metadata.Tag; +import com.drew.metadata.exif.ExifIFD0Directory; +import com.drew.metadata.exif.ExifSubIFDDirectory; + +import net.filebot.util.FileUtilities.ExtensionFileFilter; public class ImageMetadata extends LinkedHashMap { @@ -34,4 +42,24 @@ public class ImageMetadata extends LinkedHashMap { } } + public static ZonedDateTime getDateTaken(File file) { + if (SUPPORTED_FILE_TYPES.accept(file)) { + try { + Metadata metadata = ImageMetadataReader.readMetadata(file); + ExifIFD0Directory directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class); + Date date = directory.getDate(ExifSubIFDDirectory.TAG_DATETIME); + + if (date != null) { + return date.toInstant().atZone(ZoneOffset.UTC); + } + } catch (Exception e) { + debug.warning(e::toString); + } + } + + return null; + } + + public static final FileFilter SUPPORTED_FILE_TYPES = new ExtensionFileFilter("jpg", "jpeg", "png", "webp", "gif", "ico", "bmp", "tiff", "psd", "pcx", "raw", "crw", "cr2", "nef", "orf", "raf", "rw2", "rwl", "srw", "arw", "dng", "x3f", "mov", "mp4", "m4v", "3g2", "3gp", "3gp"); + }