From a4096b464911dff4159f8a9bb1f6775b410f82b6 Mon Sep 17 00:00:00 2001 From: Jeena Date: Sat, 3 Sep 2022 13:55:06 +0900 Subject: [PATCH] Fix timezone date --- README.md | 4 ++-- heic2date.py | 9 +++++---- mov2date.py | 54 ++++++---------------------------------------------- 3 files changed, 13 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index 219a5da..fbda978 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ Installation ------------ git clone https://github.com/jeena/heic2jpeg.git + sudo pacman -S perl-image-exiftool pipenv install pipenv shell @@ -31,8 +32,7 @@ This will get the date out of your .mov file and rename the file into IMG_YYYYMM You can automate it to do it for every specific file in a directory like this: - for i in `ls *.HEIC -1 | grep -v 2022`; do ~/Projects/heic2jpeg/heic2date.py $i; done - + find . -iname "*.HEIC" -exec ~/Projects/heic2jpeg/heic2date.py {} \; License ------- diff --git a/heic2date.py b/heic2date.py index a265137..4ee9190 100755 --- a/heic2date.py +++ b/heic2date.py @@ -3,11 +3,9 @@ import sys import os import datetime -#import exifread import io import pyheif import piexif -#from PIL import Image from pathlib import Path class Heic2Date: @@ -22,8 +20,11 @@ class Heic2Date: return piexif.load(metadata['data']) def date(self): - dto = self.exif()["Exif"][piexif.ExifIFD.DateTimeOriginal].decode('utf-8') - return datetime.datetime.strptime(dto, "%Y:%m:%d %H:%M:%S") + exif = self.exif()["Exif"] + date_time_original = exif[piexif.ExifIFD.DateTimeOriginal].decode('utf-8') + offset_time_original = exif[piexif.ExifIFD.OffsetTimeOriginal].decode('utf-8') + dto = date_time_original + offset_time_original + return datetime.datetime.strptime(dto, "%Y:%m:%d %H:%M:%S%z") def rename(self, prefix='', postfix=''): with open(self.path, 'rb') as image: diff --git a/mov2date.py b/mov2date.py index 56aac25..a650899 100755 --- a/mov2date.py +++ b/mov2date.py @@ -4,66 +4,24 @@ import sys import os import datetime from pathlib import Path +from datetime import datetime +import subprocess class Mov2Date: def __init__(self, path): self.path = Path(path) def date(self): - creation_time, _ = get_mov_timestamps(self.path) - return creation_time + result = subprocess.check_output(['exiftool', '-time:CreationDate', str(self.path)]).decode('utf-8') + return datetime.strptime(result, "Creation Date : %Y:%m:%d %H:%M:%S%z\n") def rename(self, prefix='', postfix=''): name = prefix + self.date().strftime("%Y%m%d_%H%M%S") + postfix new_path = self.path.with_name(name).with_suffix(self.path.suffix) print(new_path) os.rename(self.path, new_path) - -def get_mov_timestamps(filename): - ''' Get the creation and modification date-time from .mov metadata. - - Returns None if a value is not available. - ''' - from datetime import datetime as DateTime - import struct - - ATOM_HEADER_SIZE = 8 - # difference between Unix epoch and QuickTime epoch, in seconds - EPOCH_ADJUSTER = 2082844800 - - creation_time = modification_time = None - - # search for moov item - with open(filename, "rb") as f: - while True: - atom_header = f.read(ATOM_HEADER_SIZE) - #~ print('atom header:', atom_header) # debug purposes - if atom_header[4:8] == b'moov': - break # found - else: - atom_size = struct.unpack('>I', atom_header[0:4])[0] - f.seek(atom_size - 8, 1) - - # found 'moov', look for 'mvhd' and timestamps - atom_header = f.read(ATOM_HEADER_SIZE) - if atom_header[4:8] == b'cmov': - raise RuntimeError('moov atom is compressed') - elif atom_header[4:8] != b'mvhd': - raise RuntimeError('expected to find "mvhd" header.') - else: - f.seek(4, 1) - creation_time = struct.unpack('>I', f.read(4))[0] - EPOCH_ADJUSTER - creation_time = DateTime.fromtimestamp(creation_time) - if creation_time.year < 1990: # invalid or censored data - creation_time = None - - modification_time = struct.unpack('>I', f.read(4))[0] - EPOCH_ADJUSTER - modification_time = DateTime.fromtimestamp(modification_time) - if modification_time.year < 1990: # invalid or censored data - modification_time = None - - return creation_time, modification_time - + + if __name__ == "__main__": if len(sys.argv) != 2: print("Usage: mov2date.py path/to/movie.mov")