-
Notifications
You must be signed in to change notification settings - Fork 0
/
reorg.sh
executable file
·268 lines (233 loc) · 7.61 KB
/
reorg.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/bin/bash
. ./bin/util.sh
declare -r bashName=`basename ${0} | cut -d '.' -f 1`
declare -r tmpDir=/tmp/${bashName}
declare -r logFile=${tmpDir}/$(date +"%Y%m%d_%H%M%S").log
declare -r nowDir=`pwd`
declare cfgDryRun=false
declare cfgJustRetouch=false
declare -r threads=1
declare -r prefix="IMG_"
declare -r fileDateFmt="%Y%m%d_%H%M%S"
declare -r infoDateFmt="%m/%d/%Y %H:%M:%S"
declare -r linuxTouchFmt="%Y%m%d%H%M.%S"
declare -r nowOS=$(uname -s)
declare -r linuxOS="Linux"
declare -r macOS="Darwin"
declare targetDir=""
declare -a scanFiles
function init() {
chkDir "${tmpDir}"
if [[ "${nowOS}" != "${macOS}" ]] && [[ "${nowOS}" != "${linuxOS}" ]]; then
ErrExit "Not support ${nowOS}"
fi
cd "${sourceDir}"
}
function usage() {
ErrExit "Usage: ./${bashName} -s source/path [-t target/path] [-z \"Time/Zone\"] [-d] [-r]"
}
function loadFiles() {
echo "Scan ${sourceDir}"
oldIFS=$IFS
IFS=$'\n'
scanFiles=(`find ${sourceDir} -type f | sort`)
IFS=$oldIFS
for file in "${scanFiles[@]}"; do
analysisFile "${file}"
#while [ "$(jobs -p | wc -l)" == "${threads}" ];
#do
# sleep 5
#done
done
wait
}
function analysisFile() {
echo "---------------------------------------------------------------------------------"
echo "${1}"
fileName=`basename "$1"`
pureName=${fileName%.*}
fileExt=${fileName##*.}
filePath=`dirname "$1"`
relativePath=$(loadRelativePath "${sourceDir}" "${filePath}")
cDate=`loadCreateTime "$1"`
mDate=`loadModifyTime "$1"`
fileDate=`loadFileDate ${pureName}`
if [[ ${forceFileTime} ]]; then
minDate=${fileDate}
else
minDate=`getMinTimestamp ${cDate} ${mDate}`
minDate=`getMinTimestamp ${minDate} ${fileDate}`
fi
#echo "Compare timestamp: $(timestamp2Date ${cDate} "${infoDateFmt}"), $(timestamp2Date ${mDate} "${infoDateFmt}"), $(timestamp2Date ${fileDate} "${infoDateFmt}")"
#echo "Min timestamp: ${minDate}"
if [[ "${targetDir}" != "" ]]; then
if [[ "${relativePath}" != "" ]]; then
target="${targetDir}${relativePath}/${prefix}$(timestamp2FileDate "${minDate}").${fileExt}"
else
target="${targetDir}/${prefix}$(timestamp2FileDate "${minDate}").${fileExt}"
fi
else
target="${filePath}/${prefix}$(timestamp2FileDate "${minDate}").${fileExt}"
fi
if ${cfgJustRetouch}; then
reTouchTime "${cDate}" "${mDate}" "${minDate}" "${target}"
return
fi
fileInfo=$(file -b "${1}")
isImg=$(echo "${fileInfo}" | grep "image data," | wc -l)
if (( ${isImg} > 0 )); then
if [[ "${1}" != "${target}" ]]; then
target=$(checkNotExistsPath "$1" "${target}")
[[ "${1}" != "${target}" ]] && mvFile "${1}" "${target}"
fi
reTouchTime "${cDate}" "${mDate}" "${minDate}" "${target}"
elif [[ "${fileInfo}" =~ ^'ISO Media' ]]; then
if $(isPressed "${1}"); then
tmpPath=${filePath}/${pureName}_tmp.${fileExt}
tmpPath=$(checkNotExistsPath "$1" "${tmpPath}")
mvFile "${1}" "${tmpPath}"
target=$(checkNotExistsPath "$1" "${target}")
pressMp4 "${tmpPath}" "${target}"
reTouchTime "${cDate}" "${mDate}" "${minDate}" "${target}"
rmFile "${tmpPath}"
elif [[ "${1}" != "${target}" ]]; then
target=$(checkNotExistsPath "$1" "${target}")
[[ "${1}" != "${target}" ]] && mvFile "${1}" "${target}"
reTouchTime "${cDate}" "${mDate}" "${minDate}" "${target}"
fi
else
echo "PASS"
fi
echo ""
}
function checkNotExistsPath() {
renameCount=1
sourcePath="${1}"
sourceName=$(basename "${sourcePath}")
targetFolder=$(dirname "${2}")
targetName=$(basename "${2}")
result="${2}"
while [ -f "${result}" ]; do
result="${targetFolder}/${targetName%.*}_${renameCount}.${targetName##*.}"
[[ "${sourcePath}" == "${result}" ]] && break
((renameCount++))
done
echo "${result}"
}
function loadRelativePath() {
[[ "${1}" != "${2}" ]] && echo "${t1}" | sed "s~${t2}~~g" 2> /dev/null
}
function isPressed() {
profile=$(ffprobe -v quiet -show_streams "${1}" | grep "profile=Baseline")
[[ "${profile}" == "" ]] && return 0 || return 1
}
function pressMp4() {
execCmd "ffmpeg -i '${1}' -c:v libx264 -preset slow -profile:v high -crf 18 -coder 1 -pix_fmt yuv420p -movflags +faststart -g 30 -bf 2 -c:a aac -b:a 384k -profile:a aac_low '${2}'"
}
function loadCreateTime() {
[[ "${nowOS}" == "${macOS}" ]] && tmp=`GetFileInfo -d "${1}"`
[[ "${nowOS}" == "${linuxOS}" ]] && tmp=`stat "${1}" | grep Access | cut -c 9-`
infoDate2Timestamp "${tmp}"
}
function loadModifyTime() {
[[ "${nowOS}" == "${macOS}" ]] && tmp=`GetFileInfo -m "${1}"`
[[ "${nowOS}" == "${linuxOS}" ]] && tmp=`stat "${1}" | grep Modify | cut -c 9-`
infoDate2Timestamp "${tmp}"
}
function loadFileDate() {
tmp=`echo "${1}" | grep -Eo "[0-9]{8}_[0-9]{6}"`
fileDate2Timestamp "${tmp}"
}
function infoDate2Timestamp() {
date2Timestamp "${1}" "${infoDateFmt}"
}
function fileDate2Timestamp() {
date2Timestamp "${1}" "${fileDateFmt}"
}
function date2Timestamp() {
[[ "${nowOS}" == "${macOS}" ]] && date -j -f "${2}" "${1}" +%s 2> /dev/null
[[ "${nowOS}" == "${linuxOS}" ]] && date -d "${1}" +%s 2> /dev/null
}
function timestamp2InfoDate() {
timestamp2Date "${1}" "${infoDateFmt}"
}
function timestamp2FileDate() {
timestamp2Date "${1}" "${fileDateFmt}"
}
function timestamp2TouchDate() {
timestamp2Date "${1}" "${linuxTouchFmt}"
}
function timestamp2Date() {
if [[ "${mTZ}" != "" ]]; then
[[ "${nowOS}" == "${macOS}" ]] && TZ=${mTZ} date -r ${1} +"${2}"
[[ "${nowOS}" == "${linuxOS}" ]] && TZ=${mTZ} date --date=@${1} +"${2}"
else
[[ "${nowOS}" == "${macOS}" ]] && date -r ${1} +"${2}"
[[ "${nowOS}" == "${linuxOS}" ]] && date --date=@${1} +"${2}"
fi
}
function getMinTimestamp() {
(( $# == 1 )) && echo "${1}" && return
(( $1 >= $2 )) && echo "${2}" || echo "${1}"
}
function reTouchTime() {
#1 cDate
#2 mDate
#3 minDate
#4 target
#echoInfo "Retouch: $1_$2_$3_$4"
if [[ "${1}" != "${3}" ]] || [[ "${mTZ}" != "" ]]; then
touchCreateTime ${3} "${4}"
fi
if [[ "${2}" != "${3}" ]] || [[ "${mTZ}" != "" ]]; then
touchModifyTime ${3} "${4}"
fi
}
function touchCreateTime() {
if [[ "${nowOS}" == "${macOS}" ]]; then
time=$(timestamp2InfoDate ${1})
execCmd "SetFile -d '${time}' '${2}'"
elif [[ "${nowOS}" == "${linuxOS}" ]]; then
time=$(timestamp2TouchDate ${1})
execCmd "touch -a -t ${time} '${2}'"
fi
}
function touchModifyTime() {
if [[ "${nowOS}" == "${macOS}" ]]; then
time=$(timestamp2InfoDate ${1})
execCmd "SetFile -m '${time}' '${2}'"
elif [[ "${nowOS}" == "${linuxOS}" ]]; then
time=$(timestamp2TouchDate ${1})
execCmd "touch -m -t ${time} '${2}'"
fi
}
while getopts 'drs:t:z:f' OPT; do
case ${OPT} in
d)
cfgDryRun=true
;;
r)
cfgJustRetouch=true
;;
s)
[[ "${OPTARG}" != "" ]] && [[ ! -d ${OPTARG} ]] && ErrExit "Source ${OPTARG} is not exists."
declare -r sourceDir="$(cd "${OPTARG}" && pwd )"
;;
t)
targetDir="${OPTARG}"
;;
z)
declare mTZ="$OPTARG"
;;
f)
declare -r forceFileTime=true
;;
\?)
usage
;;
esac
done
(( $# < 1 )) && usage
init
loadFiles
cd ${nowDir}