-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcleanup-exif-date.sh
executable file
·187 lines (154 loc) · 3.48 KB
/
cleanup-exif-date.sh
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
#!/usr/bin/env bash
__DEBUG=1
source include arrays filesize
function decho
{
(( __DEBUG )) && echo "$@"
}
count=0
function get_exif_ctime
{
# 0x9003|2003:11:23 21:54:20
exif -m --tag="Date and Time (Original)" "$1" 2>/dev/null
return
}
# See: http://fvue.nl/wiki/Bash:_Passing_variables_by_reference
# Usage: local "$1" && upvar $1 "value(s)"
upvar() {
if unset -v "$1"; then # Unset & validate varname
if (( $# == 2 )); then
eval $1=\"\$2\" # Return single value
else
eval $1=\(\"\${@:2}\"\) # Return array
fi
fi
}
# array_shift <array_var_name> into <var_name>
function array_shift
{
(( $# > 1 )) && if [[ $2 != "into" ]]
then
echo "Invalid arguments passed to $FUNCNAME: $@" >&2
return 1
fi
local __array_var_name=$1
local __var_name=$3
local __first=
local __rest=
e="$( declare -p "$__array_var_name" )";
e=${e#*=}
e=${e#\'}
e=${e%\'}
eval "declare E=$e"
(( ${#E[@]} < 1 )) && return 1
set -- "${E[@]}"
# echo "Positional parameters are 1:$1 2:$2 3:$3 4:$4 etc..."
test -n "$__var_name" && local "$__var_name" && upvar $__var_name "$1"
shift
A=( "$@" )
eval $__array_var_name='("${A[@]}")'
# local "$__array_var_name" && upvar $__array_var_name "$@"
# upvar didn't work so well when the array got down to a single member (tried to set it to a flat variable)
#
# # alias get_array_by_ref='e="$( declare -p ${1} )"; eval "declare -A E=${e#*=}"'
# # KEYS=( "${!E[@]}" )
}
unset EXPLODED
declare -a EXPLODED
function explode
{
local c=$#
(( c < 2 )) &&
{
echo function "$0" is missing parameters
return 1
}
local delimiter="$1"
local string="$2"
local limit=${3-99}
local tmp_delim=$'\x07'
local delin=${string//$delimiter/$tmp_delim}
local oldifs="$IFS"
IFS="$tmp_delim"
EXPLODED=($delin)
IFS="$oldifs"
}
function basename
{
local __rv="${1##*/}"
local "$3" && upvar $3 "$__rv"
}
function dirname
{
local __rv="${1%/*}"
local "$3" && upvar $3 "$__rv"
}
function extension
{
local __rv="${1##*.}"
local "$3" && upvar $3 "$__rv"
}
function noextension
{
local __rv="${1%.*}"
local "$3" && upvar $3 "$__rv"
}
function is_jpeg_ext
{
local __rv=1
shopt -s nocasematch
[[ $1 == jpg ]] && __rv=0
shopt -u nocasematch
return $__rv
}
function process
{
local __sfn=$1
local __tfn="Cleaned/$2"
local __tdir=
dirname "$__tfn" into __tdir
[ ! -d "$__tdir" ] && mkdir -p "$__tdir"
local __ssize=
local __tsize=0
filesize "$__sfn" into __ssize
[ -e "$__tfn" ] && filesize "$__tfn" into __tsize
echo "ssize: $__ssize tsize: $__tsize"
(( __ssize > __tsize )) && cp "$__sfn" "$__tfn"
}
find . -iname '*.jpg' |
while read -r fn
do
# decho $fn
dirname "$fn" into dn
basename "$fn" into bn
extension "$bn" into ext
noextension "$bn" into noext
# decho $dn/$bn "($ext)"
[[ $dirname =~ .*AppleDouble.* ]] && continue
[[ $fn =~ DS_Store ]] && continue
# [[ $fn == $__target ]] && continue
# declare -p BASH_REMATCH
# ./Xmas 2005/FEB252E4781948F18479E7567CB803E5.jpg: JPEG image data, EXIF standard
test -e "$fn" || continue
test -s "$fn" ||
{
decho "Empty: $fn";
continue
}
date="$( get_exif_ctime "$fn" )" || {
(( count++ ))
echo ln -f "$fn" "nodate/$count.jpg"
ln -f "$fn" "nodate/$count.jpg"
continue
}
# 2003:11:23 21:54:20
date="${date/ /-}"
date="${date//:/.}"
__target="bydate/$date.$bn"
if [ ! -e "$__target" ]
then
echo ln "$fn" "$__target"
ln "$fn" "$__target"
fi
done
# check for short arg within long arg