Fix timezone date

This commit is contained in:
Jeena 2022-09-03 13:55:06 +09:00
parent dfb37d1db4
commit a4096b4649
3 changed files with 13 additions and 54 deletions

View file

@ -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
-------

View file

@ -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:

View file

@ -4,14 +4,16 @@ 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
@ -19,50 +21,6 @@ class Mov2Date:
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: