From 556696f0f828b364c3fb06555430a73bcf1bf3a5 Mon Sep 17 00:00:00 2001 From: Olivier 'reivilibre Date: Sun, 18 Jul 2021 19:12:44 +0100 Subject: [PATCH] Use exiftool instead because it handles more filetypes including MP4 (!!!) --- README.md | 2 ++ phototrie/datename.py | 17 +++++++++++------ setup.py | 2 +- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 5be6d52..51511dc 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,8 @@ with the same name will also be processed. Once you are ready to apply the changes for real, pass `--apply` (or `-y`). +**datename requires `exiftool` to be installed.** + ### batchrename `batchrename photo1.jpg photo2.jpg` (etc) diff --git a/phototrie/datename.py b/phototrie/datename.py index 140860b..705e89a 100644 --- a/phototrie/datename.py +++ b/phototrie/datename.py @@ -1,12 +1,12 @@ import argparse import os import sys -import exifread import re from exifread.classes import IfdTag +from exiftool import ExifTool -DATETIME_KEY = "Image DateTime" +DATETIME_KEY = "EXIF:CreateDate" DATETIME_PATTERN = re.compile(r"^(\d{4}):(\d{2}):(\d{2}) (\d{2}):(\d{2}):(\d{2})$") @@ -55,16 +55,19 @@ def cli(): args = parser.parse_args(sys.argv[1:]) + exifreader = ExifTool() + exifreader.start() + for filename in args.files: f = open(filename, "rb") - tags = exifread.process_file(f) + + tags = exifreader.get_metadata(filename) if DATETIME_KEY not in tags: - print(f"No datetime tag found for: {filename}", file=sys.stderr) + print(f"No datetime tag found for: {filename}. Only {tags.keys()}", file=sys.stderr) continue - ifd_tag: IfdTag = tags[DATETIME_KEY] - match = DATETIME_PATTERN.match(ifd_tag.values) + match = DATETIME_PATTERN.match(tags[DATETIME_KEY]) if match is None: print( f"Invalid datetime tag ({tags[DATETIME_KEY]!r}) found for: {filename}", @@ -98,6 +101,8 @@ def cli(): if found_raw is not None: print(found_raw, "→", get_new_name(found_raw, match)) + exifreader.terminate() + if __name__ == "__main__": cli() diff --git a/setup.py b/setup.py index 814752c..d6a5f98 100644 --- a/setup.py +++ b/setup.py @@ -19,7 +19,7 @@ VERSION = "0.1.0" # What packages are required for this module to be executed? REQUIRED = [ - "ExifRead", + "PyExifTool", "Pillow", ]