-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmedia-optimize-local.sh
More file actions
executable file
·176 lines (168 loc) · 5.4 KB
/
media-optimize-local.sh
File metadata and controls
executable file
·176 lines (168 loc) · 5.4 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
#!/bin/bash
IFS="
"
#media-optimize
folder=${1:-"."}
cd "${folder}" || exit
#jpeg
p_jpeg_loop() {
quality=$(identify -verbose "${p}" | grep "Quality" | sed -e "s/.*Quality: //g")
if [[ ! -s ${quality} ]]; then
quality="76"
fi
if [[ -n ${quality} ]]; then
keep_compressing_picture=1
while [[ ${keep_compressing_picture} -gt 0 ]]; do
j=$(nice -n 15 jpegoptim -m "${quality}" "${p}")
echo "${j}"
jr=$(echo "${j}" | grep -e "[OK]" | grep -e ", optimized.")
if [[ -z ${jr} ]]; then
keep_compressing_picture=0
fi
done
fi
#until grep -q -e "[OK]" -e ", skipped." <<<"${j}" || [[ -z "${j}" ]]; do
#j=$(nice -n 15 jpegoptim -m "${quality}" "${p}")
#echo "${j}" #&>/dev/null
#done
}
while read -r p; do
p_jpeg_loop "${p}" & #&>/dev/null &
if [[ $(jobs -r -p | wc -l) -ge $(getconf _NPROCESSORS_ONLN) ]]; then
wait -n
fi
done < <(find . -type f -iname "*.jp*" \( -size +10k \))
wait
#gif
q_gif_loop() {
nice -n 15 gifsicle --batch -O3 --lossy=80 --colors=255 "${q}" #&>/dev/null
}
while read -r q; do
q_gif_loop "${q}" & #&>/dev/null &
if [[ $(jobs -r -p | wc -l) -ge $(getconf _NPROCESSORS_ONLN) ]]; then
wait -n
fi
done < <(find . -type f -iname "*.gif" \( -size +100k \))
wait
q_gif_recompress_loop() {
while [[ $(stat -c%s "${p}" || 0) -ge 512000 ]]; do
frameamount=$(($(exiftool -b -FrameCount "${p}" || 1) - 1))
nice -n 15 gifsicle "${p}" $(seq -f "#%g" 0 2 "${frameamount}") -O3 --lossy=80 --colors=255 -o "${p}" #&>/dev/null
done
}
while read -r p; do
q_gif_recompress_loop "${p}" &
if [[ $(jobs -r -p | wc -l) -ge $(getconf _NPROCESSORS_ONLN) ]]; then
wait -n
fi
done < <(find . -type f -size +500k \( -iname "*-320.gif" -or -iname "*-80.gif" -or -iname "*-48.gif" \))
wait
#png
r_png_loop() {
nice -n 15 oxipng -o max "${r}" #&>/dev/null
}
while read -r r; do
r_png_loop "${r}" & #&>/dev/null &
if [[ $(jobs -r -p | wc -l) -ge $(getconf _NPROCESSORS_ONLN) ]]; then
wait -n
fi
done < <(find . -type f -iname "*.png" -size +100k)
wait
#webp
s_webp_loop() {
keep_compressing_picture=1
while [[ ${keep_compressing_picture} -gt 0 ]]; do
#If file is not animated
if [[ -f ${s} ]]; then
#if [[ -z $(grep -o -a -e "ANIM" -e "ANMF" "${s}") ]]; then
if ! grep -q -o -a -e "ANIM" -e "ANMF" "${s}"; then
cwebp -mt -af -quiet "${s}" -o /tmp/"${s##.*\/}"_temp.webp #&>/dev/null
if [[ -f /tmp/"${s##.*\/}"_temp.webp ]]; then
size_new=$(stat -c%s /tmp/"${s##.*\/}"_temp.webp || 0)
size_original=$(stat -c%s "${s}" || 0)
if [[ -n ${size_original} && -n ${size_new} && ${size_original} -gt ${size_new} && ${size_new} -gt "0" ]]; then
size_diff=$((size_original - size_new))
echo "${s}: Saved ${size_diff} bytes" #&> /dev/null
mv /tmp/"${s##.*\/}"_temp.webp "${s}" #&> /dev/null
else
echo "${s}: Minimum size is ${size_original} bytes" #&> /dev/null
rm /tmp/"${s##.*\/}"_temp.webp #&> /dev/null
keep_compressing_picture=0
fi
else
keep_compressing_picture=0
fi
else
keep_compressing_picture=0
fi
else
keep_compressing_picture=0
fi
done
}
while read -r s; do
s_webp_loop "${s}" & #&>/dev/null &
if [[ $(jobs -r -p | wc -l) -ge $(getconf _NPROCESSORS_ONLN) ]]; then
wait -n
fi
done < <(find . -type f -iname "*.webp" \( -size +10k \))
wait
#Fix extensions
while read -r i; do
if file -b "${i}" | grep -vq "PNG" | grep -vq "empty" | grep -vq "symbolic link" | grep -vq "directory" | grep -vq "text"; then
if file -b "${i}" | grep -q "JPEG"; then
mv "${i}" "${i%.*}".jpg
#p="${i%.*}.jpg"
#p_jpeg_loop "${p}"
#jpegoptim -m76 "${i%.*}".jpg #&>/dev/null
elif file "${i}" | grep -q "GIF"; then
mv "${i}" "${i%.*}".gif
#gifsicle --batch -O3 --lossy=80 --colors=255 "${i%.*}".gif #&>/dev/null
elif file "${i}" | grep -q "Web/P"; then
mv "${i}" "${i%.*}".webp
#cwebp -mt -af -quiet $i
fi
fi
done < <(find . -iname "*.png")
while read -r j; do
if file -b "${j}" | grep -v -q -e "JPEG" -e "empty" -e "symbolic link" -e "directory" -e "text"; then
if file -b "${j}" | grep -q "PNG"; then
mv "${j}" "${j%.*}".png
#oxipng -o max "${j%.*}".png #&>/dev/null
elif file -b "${j}" | grep -q "GIF"; then
mv "${j}" "${j%.*}".gif
#gifsicle --batch -O3 --lossy=80 --colors=255 "${j%.*}".gif #&>/dev/null
elif file -b "${j}" | grep -q "Web/P"; then
mv "${j}" "${j%.*}".webp
#cwebp -mt -af -quiet $i
fi
fi
done < <(find . \( -iname "*.jpg" -or -iname "*.jpeg" \))
while read -r k; do
if file -b "${k}" | grep -v -q -e "GIF" -e "empty" -e "symbolic link" -e "directory" -e "text"; then
if file -b "${k}" | grep -q "JPEG"; then
mv "${k}" "${k%.*}".jpg
#jpegoptim -m76 "${k%.*}".jpg #&>/dev/null
elif file -b "${k}" | grep -q "PNG"; then
mv "${k}" "${k%.*}".png
#oxipng -o max "${k%.*}".png #&>/dev/null
elif file -b "${k}" | grep -q "Web/P"; then
mv "${k}" "${k%.*}".webp
#cwebp -mt -af -quiet $i
fi
fi
done < <(find . -iname "*.gif")
while read -r l; do
if file -b "${l}" | grep -v -q -e "Web/P" -e "empty" -e "symbolic link" -e "directory" -e "text"; then
if file -b "${l}" | grep -q "JPEG"; then
mv "${l}" "${l%.*}".jpg
#jpegoptim -m76 "${l%.*}".jpg #&>/dev/null
elif file -b "${l}" | grep -q "PNG"; then
mv "${l}" "${l%.*}".png
#oxipng -o max "${l%.*}".png #&>/dev/null
elif file -b "${l}" | grep -q "GIF"; then
mv "${l}" "${l%.*}".gif
#gifsicle --batch -O3 --lossy=80 --colors=255 "${l%.*}".gif #&>/dev/null
fi
fi
done < <(find . -iname "*.webp")