-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexif-rename
More file actions
executable file
·231 lines (185 loc) · 4.79 KB
/
exif-rename
File metadata and controls
executable file
·231 lines (185 loc) · 4.79 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/bin/bash
#
# exif-rename
#
[ -n "$MYDIR" ] || {
declare MYDIR=
MYDIR="$(dirname "$(readlink -f "$0")")"
}
[ -n "$MYLIB" ] || {
declare MYLIB=
MYLIB="$MYDIR"/lib
}
[ "$ui_loaded" ] || source "$MYLIB"/ui.source
declare optcount=0
declare srcdir=
declare destdir=
declare prefix=
declare suffix=
declare tag="-CreateDate"
declare b_execute=false
declare usagestr=
usagestr=$(
cat <<EOF
$(basename "$0") [options] srcdir
This utility renames the photos or videos based on their creation or modify
dates, as selected by the user. Default is Create Date.
The utility prints the derived newnames to the screen without actually
renaming the files by default.
To actually rename the files, you must select the x (exexute) option.
Example:
exif-rename \\\
-x \\\
-m \\\
-p "TC-" \\\
-s "-mountains" \\\
-d "~/renamed-photos" \\\
/Pictures/Photos/2023
The files in /Pictures/Photos/2023 will be renamed (-x) based on their
File Modify Date (-m) with a prefix of "TC-" (-p) and a suffix of
"-mountains" (-s) and placed in directory "~/renamed-photos"
Arguments
---------
srcdir : location of the mp4 files to be renamed
Options
-------
p:prefix : prefix to be prepended to the name
s:suffix : suffix to be appended to the name
d:destdir : destination directory, if different from srcdir
c : use file creation date (default)
m : use file modify date
x : execute the rename (default is to just show the new names)
h : this help screen
\0
EOF
)
# control_c: run if user hits control-c
#
# Global
# CTLC_EXIT - bash environment variable
#
control_c() {
echo -e "\nCtrl-c detected\nCleaning up and exiting."
exit $CTLC_EXIT
}
# init
#
# GLOBALS
#
init() {
local -i bgtemp
ui_setbg bgtemp
which exiftool || \
ui_exit ui_err_missing_exec \
"\n$INF You must install$MNU exiftool$INF to run this script.$OFF\n"
}
# parseopts
#
# Globals
#
parseopts() {
local arg=
local opt=
for arg in $@; do
if [ "${arg:0:1}" == "-" ]; then
opt="${arg:1}"
shift
((optcount++))
case "$opt" in
p ) prefix=$1
shift
((optcount++))
;;
s ) suffix=$1
shift
((optcount++))
;;
d ) destdir=$1
shift
((optcount++))
;;
c ) tag="-CreateDate" # Default
;;
m ) tag="-FileModifyDate"
;;
x ) b_execute=true
;;
h ) echo -e "$usagestr"
exit 0
esac
fi
done
}
main() {
local -a mp4jpgary=()
local -A nameary # associate name with a bump number
local newname=
local b_destdir=false
local ext=
local path=
init
(($# >= 1)) || ui_exit ui_err_invargc "$usagestr"
parseopts "$@"
shift "$optcount"
srcdir="$1"
# Check the directories
[ -n "$srcdir" ] || ui_exit ui_err_missing_arg "Must provide src directory argument"
[ -d "$srcdir" ] || ui_exit ui_err_invdir "$srcdir"
if [ -n "$destdir" ]; then
[ -d "$destdir" ] || i_exit ui_err_invdir "$destdir"
b_destdir=true
path="$destdir"
else
path="$srcdir"
fi
path="$(realpath "$path")"
# Assure that there is one and only one trailing slash at the end
# of the directory path.
path="${path%/}"
path="$path/"
# Create an array of mp4 and jpg file names.
mapfile -t mp4jpgary < <(find "$srcdir" -type f \( -iname "*.mp4" -o -iname "*.jpg" \))
# Loop through the array of mp4 and jpg filenames to rename them
# according to exif tag for CreateDate (default) or FileModifyDate,
# as specified by user.
for mp4jpg in ${mp4jpgary[@]}; do
# Get the filename extension
ext="${mp4jpg##*.}"
# Get the exif date according to user preference
# CreateDate or FileModifyDate contained in $tag
newname=$(exiftool "$tag" "$mp4jpg")
# Isolate the date and time
newname=$(echo "$newname" | cut -d':' -f2-)
# Remove leading and trailing spaces
newname=$(echo $newname)
# Replace colons with dashes
newname=${newname//:/-}
# Replace spaces with double dashes
newname=${newname// /--}
# Get the full name of the file and its path
newname="$path$prefix$newname$suffix"
# Assure that each file has a unique filename, even if the
# time stamps are identical.
# If this newname already exists in the nameary, then bump its
# value by one.
# Else, if it's a NEW newname, then set its value to zero.
if [[ -v nameary["$newname"] ]]; then
nameary["$newname"]=$((${nameary["$newname"]} + 1))
else
nameary["$newname"]=0
fi
# Set the new name accordint to its bump value in the nameary.
# If it's greater than 0, then append the bump value.
(( ${nameary["$newname"]} != 0)) && newname="$newname-${nameary["$newname"]}"
# Add the file extension to the new name.
newname="$newname.$ext"
if $b_execute; then
mv -n -u -v "$mp4jpg" "$newname"
else
echo "$mp4jpg --> $newname"
fi
done
$b_execute || echo -e \
"\n${MNU}You must use the -x option to actually rename the files.\n$OFF"
}
main "$@"