Use exiftool instead because it handles more filetypes

including MP4 (!!!)
This commit is contained in:
Olivier 'reivilibre' 2021-07-18 19:12:44 +01:00
parent df3e79b9e6
commit 556696f0f8
3 changed files with 14 additions and 7 deletions

View File

@ -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`). Once you are ready to apply the changes for real, pass `--apply` (or `-y`).
**datename requires `exiftool` to be installed.**
### batchrename ### batchrename
`batchrename <regex> <replacement> photo1.jpg photo2.jpg` (etc) `batchrename <regex> <replacement> photo1.jpg photo2.jpg` (etc)

View File

@ -1,12 +1,12 @@
import argparse import argparse
import os import os
import sys import sys
import exifread
import re import re
from exifread.classes import IfdTag 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})$") 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:]) args = parser.parse_args(sys.argv[1:])
exifreader = ExifTool()
exifreader.start()
for filename in args.files: for filename in args.files:
f = open(filename, "rb") f = open(filename, "rb")
tags = exifread.process_file(f)
tags = exifreader.get_metadata(filename)
if DATETIME_KEY not in tags: 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 continue
ifd_tag: IfdTag = tags[DATETIME_KEY] match = DATETIME_PATTERN.match(tags[DATETIME_KEY])
match = DATETIME_PATTERN.match(ifd_tag.values)
if match is None: if match is None:
print( print(
f"Invalid datetime tag ({tags[DATETIME_KEY]!r}) found for: {filename}", f"Invalid datetime tag ({tags[DATETIME_KEY]!r}) found for: {filename}",
@ -98,6 +101,8 @@ def cli():
if found_raw is not None: if found_raw is not None:
print(found_raw, "", get_new_name(found_raw, match)) print(found_raw, "", get_new_name(found_raw, match))
exifreader.terminate()
if __name__ == "__main__": if __name__ == "__main__":
cli() cli()

View File

@ -19,7 +19,7 @@ VERSION = "0.1.0"
# What packages are required for this module to be executed? # What packages are required for this module to be executed?
REQUIRED = [ REQUIRED = [
"ExifRead", "PyExifTool",
"Pillow", "Pillow",
] ]