-
Notifications
You must be signed in to change notification settings - Fork 0
/
photosorter.py
executable file
·158 lines (134 loc) · 5.4 KB
/
photosorter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python
"""Simple photo and video sorter
This
https://exiftool.org/
Example:
$ ./photosorter.py SRCDIR DESTDIR
"""
import argparse
import json
import os
import pathlib
import shutil
import sys
from datetime import datetime
from plumbum.cmd import exiftool, md5sum
class CustomHelpFormatter(argparse.ArgumentDefaultsHelpFormatter,
argparse.RawDescriptionHelpFormatter):
pass
def get_metadata(filename):
"""Get EXIF metadata in JSON format using ExifTool"""
exiftool_json = exiftool["-j", "-G", "-n", filename]
try:
metadata = json.loads(exiftool_json())[0]
except Exception as e:
print("SKIP: Error getting metadata for %s: %s" % (filename, str(e)))
return None
return metadata
def get_md5(filename):
"""Get MD5 sum without EXIF information"""
exiftool_md5 = exiftool["-all=", "-o", "-", "-b", filename] | md5sum["-b", "-"]
try:
md5 = exiftool_md5()
md5 = md5.split()[0]
except Exception as e:
print("SKIP: Error getting md5sum for %s: %s" % (filename, str(e)))
return None
return md5
def sorter(srcdir, destdir, move, dryrun, overwrite):
for path in pathlib.Path(srcdir).rglob("*"):
filename = str(path)
if os.path.isdir(filename):
continue
# Get timestamp using exiftool
if filename.lower().endswith(('.jpg', '.mp4', '.3gp', '.mov')):
extension = filename.lower()[-3:]
metadata = get_metadata(filename)
if not metadata:
continue
if "EXIF:DateTimeOriginal" in metadata:
timestamp = metadata["EXIF:DateTimeOriginal"]
elif "QuickTime:CreateDate" in metadata:
timestamp = metadata["QuickTime:CreateDate"]
else:
print("SKIP: Date missing %s" % filename)
continue
else:
print("SKIP: Unknown %s" % filename)
continue
# Parse timestamp
try:
dateobj = datetime.strptime(timestamp, "%Y:%m:%d %H:%M:%S")
except:
print("SKIP: timestamp error (%s) %s" % (timestamp, filename))
continue
# Sanity check year
if dateobj.year < 1990 or dateobj.year > datetime.now().year:
print("SKIP: wrong date (%s) %s" % (dateobj.strftime("%Y%m%d_%H%M%S"), filename))
continue
# Create destination dir
new_filepath = os.path.join(destdir, dateobj.strftime("%Y"), dateobj.strftime("%m"))
if not os.path.isdir(new_filepath):
pathlib.Path(new_filepath).mkdir(parents=True)
# Check for duplicates
for i in range(sys.maxsize):
new_filename = os.path.join(new_filepath, "%s%s.%s" %
(dateobj.strftime("%Y%m%d_%H%M%S"),
"_%03d" % i if i > 0 else "", extension))
if os.path.exists(new_filename):
src_md5 = get_md5(filename)
if not src_md5:
new_filename = None
break
dst_md5 = get_md5(new_filename)
if src_md5 == dst_md5:
if overwrite:
print("Remove duplicate %s" % (new_filename))
if not dryrun:
os.remove(new_filename)
else:
print("SKIP: duplicate %s %s" % (filename, new_filename))
new_filename = None
if move and not dryrun:
os.remove(filename)
break
else:
break
# Copy or move file
if new_filename:
print("%s -> %s" % (filename, new_filename))
if not dryrun:
if move:
shutil.move(filename, new_filename)
else:
shutil.copyfile(filename, new_filename)
if __name__ == "__main__":
description = __doc__.splitlines()[0]
epilog = "\n".join(__doc__.splitlines()[1:])
parser = argparse.ArgumentParser(description=description,
epilog=epilog,
formatter_class=CustomHelpFormatter)
parser.add_argument("-v", "--verbose", default=False, action="store_true",
help="Verbose output")
parser.add_argument("--dryrun", default=False, action="store_true",
help="Do not copy/move any files")
parser.add_argument("--overwrite", default=False, action="store_true",
help="Overwrite duplicates")
parser.add_argument("--move", default=False, action="store_true",
help="Move files instead of copy")
parser.add_argument("srcdir", metavar="SRCDIR", nargs=1,
help="source directory")
parser.add_argument("destdir", metavar="DESTDIR", nargs=1,
help="destination directory")
args = parser.parse_args()
if args.verbose:
print(args)
srcdir = args.srcdir[0]
destdir = args.destdir[0]
if not os.path.isdir(srcdir):
print("Could not find source directory %s" % srcdir)
sys.exit(1)
if not os.path.isdir(destdir):
print("Could not find destination directory %s" % destdir)
sys.exit(1)
sorter(srcdir, destdir, args.move, args.dryrun, args.overwrite)