diff --git a/import_media.py b/import_media.py index 897d859..ed7df9b 100644 --- a/import_media.py +++ b/import_media.py @@ -34,6 +34,7 @@ import shutil import hashlib from datetime import datetime import exifread +import ffmpeg config_file = 'config.yaml' @@ -75,15 +76,17 @@ def get_capture_date(p, t): if t == 'image': with open(p, 'rb') as f: tags = exifread.process_file(f) - captured = tags['EXIF DateTimeOriginal'] - year = str(captured).split(' ')[0].split(':')[0] - month = str(captured).split(' ')[0].split(':')[1] - day = str(captured).split(' ')[0].split(':')[2] + stamp = datetime.strptime(str(tags['EXIF DateTimeOriginal']), '%Y:%m:%d %H:%M:%S') + elif t == 'video': + stamp = datetime.strptime(ffmpeg.probe(p)['format']['tags']['creation_time'], '%Y-%m-%dT%H:%M:%S.%f%z') + elif t == 'audio': + stamp = datetime.strptime(ffmpeg.probe(p)['format']['tags']['date'], '%Y-%m-%d') else: stamp = datetime.fromtimestamp(os.path.getctime(p)) - year = stamp.strftime("%Y") - month = stamp.strftime("%m") - day = stamp.strftime("%d") + + year = stamp.strftime("%Y") + month = stamp.strftime("%m") + day = stamp.strftime("%d") return year, month, day def create_folder(f): @@ -96,6 +99,7 @@ def copy_from_source(p, dest_folder, dest_orig_folder, file): if os.path.exists(os.path.join(dest_folder, file)): check_match = cmp_files(p, os.path.join(dest_folder, file)) if check_match == False: + print(f'Found duplicate for {p}, renaming destination with md5 appended.') base, extension = os.path.splitext(file) file_name_hash = base + '_' + md5_hash(os.path.join(dest_folder, file)) + extension os.rename(os.path.join(dest_folder, file), os.path.join(dest_folder, file_name_hash)) @@ -134,6 +138,10 @@ def copy_from_source(p, dest_folder, dest_orig_folder, file): print(dest_orig_folder + '/' + file, ': ', md5_hash(dest_orig_folder + '/' + file)) exit + # Blindly assume md5 check has passed... + if config['cleanup_sd'] == True: + os.remove(p) + def process_file(p, t, file, ext): capture_date = get_capture_date(p, t) y = capture_date[0]