-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathimg_date
More file actions
executable file
·221 lines (183 loc) · 4.44 KB
/
img_date
File metadata and controls
executable file
·221 lines (183 loc) · 4.44 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
#!/bin/zsh
# Angel Olivera <aolivera@gmail.com>
# 2009
# License: GPL-2
# TODO:
# implement time shifting
setopt nonomatch
prog=$0:t
which exif &>/dev/null || { print "This program requires exif(1)." && exit 1; }
usage()
{
print \
"Usage: $prog [OPTION] EXPRESSION
Where EXPRESSION is a list of filenames separated by spaces.
Options
-r Reverse the operation: remove the timestamp from the file names
-s shift Shift timestamp by \`\`shift'' hours
-o Organize: sort the images nito their own directories named by date
"
exit 1
}
while getopts rs:o option; do
case $option in
(s) shift=$OPTARG;;
(r) op=revert;;
(o) op=organize;;
(*) usage;;
esac
done
((OPTIND > 1)) && shift $((OPTIND - 1))
if [[ -z $1 ]]; then
usage
fi
get_ts()
{
# arg: file
# output: timestamp: YYYY-MM-DDTHH-MM-SS
local ts thumb
if [[ ! -f $1 ]]; then
print "# $prog:t: $1: No such file or directory" >&2
return
fi
# from exif
ts=$(exif $1 2>/dev/null | awk '/Date and Time *\|/{sub(/\|/, ""); print $4 "T" $5}')
# print "$0: TS:** $ts **"
# ts=$(exif -t 'Date and Time' -m $1)
# ts=${ts/ /}
if [[ -z $ts ]]; then
thumb=$(print (#i)$f:r.thm)
if [[ -e $thumb ]]; then
ts=$(get_ts $thumb)
else
# from inode
ts=$(stat --format="%y" $1 2>/dev/null)
ts=$ts[1,10]T$ts[12,19]
fi
fi
print ${ts//[:-]/}
}
change_filename()
{
# arg: operation (prepend or append)
# arg: filename
# arg: string
# arg: separator
# arg: transformation (optional), can be one of:
# - l: lowercase
# - u: uppercase
# - ln: lowercase just the name (not the extension)
# - un: uppercase just the namey
# - le: lowercase just the extension (not the name)
# - ue: uppercase just the extension
if (($#@<4)); then
print "# $0:t: insufficient arguments" >&2
return
fi
local op=$1 f=$2 string=$3 nf sep=$4 transform=$5
if [[ ! -f $f ]]; then
print "# $0:t: $1: No such file or directory" >&2
return
fi
case $op in
(prepend)
nf=${string}${sep}${f:t:r}
;;
(append)
nf=${f:t:r}${sep}${string}
;;
(*)
print "# $0:t: $1: Unsupported operation. Valid operations are: prepend, append." >&2
return
;;
esac
case $transform in
(l)
nf=${(L)nf}.${(L)f:e}
;;
(u)
nf=${(U)nf}.${(U)f:e}
;;
(ln)
nf=${(L)nf}.${f:e}
;;
(un)
nf=${(U)nf}.${f:e}
;;
(le)
nf=${nf}.${(L)f:e}
;;
(ue)
nf=${nf}.${(U)f:e}
;;
(*)
print "# $0:t: $1: Unsupported transformation. Valid transformations are: l, u, ln, un, le, ue." >&2
return
;;
esac
# very messy way of printing the path if the file is not in the current
# directory, but couldn't find a cleaner, shorter one
path=${${${f:h}#.}:+${${f:h}#.}/}
nf=$path$nf
print $nf
}
shift_timestamp()
{
# arg: timestamp in format: %Y%m%d--%H%M%S
# arg: shift (positive/negative integer)
if (($#@!=2)); then
print "# $0:t: insufficient arguments" >&2
fi
local ts=$1 shift=$2
# TZ=UTC date --rfc-3339=seconds -d '2005-07-10 11:38:44-00:00'
# TZ=UTC date --rfc-3339=seconds -d "$ts[1,4]-$ts[5,6]-$ts[7,8] $ts[10,11]:$ts[12,13]:$ts[14,15]-00:00"
if ((shift>0)); then
TZ=UTC-$((shift))
else
TZ=UTC+$((-shift))
fi
TZ=$TZ date --rfc-3339=seconds -d "$ts[1,4]-$ts[5,6]-$ts[7,8] $ts[10,11]:$ts[12,13]:$ts[14,15]-00:00"
}
timestamp_file()
{
# arg: file
local f=$1 ts ts_exif
print $'\n# processing: '"$f" >&2
ts=$(get_ts $f)
[[ -z $ts ]] && return
if [[ -n $shift ]]; then
ts=$(shift_timestamp $ts $shift)
ts_exif="$ts[1,4]:$ts[6,7]:$ts[9,10] $ts[12,13]:$ts[15,16]:$ts[18,19]"
print exif -t \'Date and Time\' --set-value \"$ts_exif\" --ifd=0 -o \"$f\" \"$f\" '>/dev/null'
ts=$ts[1,4]$ts[6,7]$ts[9,10]T$ts[12,13]$ts[15,16]$ts[18,19]
fi
nf=$(change_filename prepend $f $ts - le)
[[ -z $nf ]] && return
print mv -v \"$f\" \"$nf\"
print touch -acmt $ts[3,8]$ts[10,13] \"$nf\"
}
organize()
{
# arg: file
local f=$1 ts dest
ts=$(get_ts $f)
[[ -z $ts ]] && return
dst="$f:h/$ts[1,4]-$ts[5,6]-$ts[7,8]"
print mkdir -p \"$dst\"
print mv -v \"$f\" \"$dst\"
}
for f in $@; do
case $op in
(revert)
n=${${f:t}##*-}
print mv -v \"$f\" \"$f:h/$n\"
;;
(organize)
organize $f
;;
(*)
timestamp_file $f
;;
esac
done
if [ -n "$1" ]; then print "# This is a preview only. Run with $0:t|sh to apply the changes."; fi