-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcopy_from_gopro.py
executable file
·104 lines (83 loc) · 3.23 KB
/
copy_from_gopro.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
#!/usr/bin/env python3
# Script to copy/move files from GoPro to a directory orderd by date directory
import os
import argparse
import datetime
import exifread
import shutil
from alive_progress import alive_bar
def dir_path(string):
if os.path.isdir(string):
return string
else:
raise NotADirectoryError(string)
parser = argparse.ArgumentParser(description="")
parser.add_argument("-s", "--source", help="Source Direcory of GoPro images (recursive)", type=dir_path, required=True)
parser.add_argument("-d", "--destination", help="Destination directory where the images will be copied", type=dir_path, required=True)
parser.add_argument("-r", "--replace", help="Replace if file exists", action='store_true')
parser.add_argument("-m", "--move", help="Move anstead of copy", action='store_true')
args = parser.parse_args()
source_directory = args.source
destination_directory = args.destination
count_copy = 0
count_move = 0
count_replace = 0
count_create = 0
count_skip = 0
count_error = 0
def get_datetime_exifread(filename):
with open(filename, 'rb') as image_file:
tags = exifread.process_file(image_file, details=False)
retdate = None
retdate = tags['Image DateTime'] if 'Image DateTime' in tags else retdate
retdate = tags['DateTimeOriginal'] if 'DateTimeOriginal' in tags else retdate
retdate = tags['GPS GPSDate'] if 'GPS GPSDate' in tags else retdate
if retdate is None:
# No exif data found - Linux only TODO: Windows
return os.stat(filename).st_ctime
retdate = datetime.datetime.strptime(str(retdate)[0:10], '%Y:%m:%d')
return retdate
def print_stat():
global count_skip, count_move, count_copy, count_create, count_replace, count_error
print("Copy: " + str(count_copy) + " Move: " + str(count_move) + " Replace: " + str(count_replace) + " Skipped: " + str(count_skip) + " Created directories: " + str(count_create))
def copy_or_move(src, dest):
global count_move, count_copy
if args.move:
shutil.move(src,dest)
count_move += 1
else:
shutil.copy(src,dest)
shutil.copystat(src,dest)
count_copy += 1
def copy_from(current_dir):
global count_skip, count_move, count_copy, count_create, count_replace, count_error
for filedir in os.listdir(current_dir):
full_filedir = os.path.join(current_dir, filedir)
if os.path.isfile(full_filedir) and full_filedir.upper().endswith('.JPG'):
# print(full_filedir)
# check date
file_date = get_datetime_exifread(full_filedir)
# create destination-directory
target_dir = os.path.join(destination_directory, file_date.strftime('%Y-%m-%d'))
target_file = os.path.join(target_dir, filedir)
# check destination file
if not os.path.exists(target_dir):
print("Create destination directory " + target_dir)
os.mkdir(target_dir)
# copy/move
if os.path.exists(target_file):
if not args.replace:
count_skip += 1
else:
copy_or_move(full_filedir,target_file)
count_replace += 1
else:
copy_or_move(full_filedir,target_file)
# TODO copy file create
bar()
elif os.path.isdir(full_filedir):
# scan subdir
copy_from(full_filedir)
with alive_bar(0) as bar:
copy_from(source_directory)
print_stat()