-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilesOptim.sh
executable file
·347 lines (302 loc) · 10.3 KB
/
FilesOptim.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/bin/bash
#########################################################################
## A shell script made to optimize JPG, PNG, videos bnd PDF files’ sizes.
## Copyright (C) Corentin Michel - All Rights Reserved
## Contact: [email protected] [https://github.com/SKOHscripts]
## With help from Maximilian Fries’s code (2016) @MokaMokiMoke[https://github.com/MokaMokiMoke]
#########################################################################
clear
rouge='\e[1;31m'
jaune='\e[1;33m'
bleu='\e[1;34m'
violet='\e[1;35m'
vert='\e[1;32m'
neutre='\e[0;m'
if [ "$UID" -eq "0" ]
then
zenity --warning --height 80 --width 400 --title "EREUR" --text "Merci de lancez le script sans sudo : \n<b>./FilesOptim.sh</b>."
exit
fi
which zenity > /dev/null
if [ $? = 1 ]
then
sudo apt install -y zenity
fi
zenity --question --width=500 --height=100 --title "FilesOptim" --text "A shell script that optimizes the size of many files. The script will optimise the size of .JPG and .PNG files without loss of quality. Next, it will recursively list video files (.mp4, .mkv, .avi, .m4v, .wmv) and optimise their size if the gain is greater than a chosen value (75% by default). Finally, it will optimise .PDF files without loss of colour quality and fixing the images at 300DPI.\n\nAre you OK with that ?"
if [ $? == 0 ]
then
zenity --info --width=300 --height=100 --text "Please select the folder from where you want to start optimization."
inputStr=$(zenity --file-selection --directory "${HOME}")
cd $inputStr || { zenity --error --width=300 --height=100 --text "The folder name must not contain spaces."; exit; }
####################################################################################
# PICTURES OPTIMISATION
####################################################################################
echo ""
echo -e -n "$vert [1/3]$rouge PICTURES OPTIMISATION "
for i in `seq 30 $COLUMNS`;
do echo -n "."
done
echo -e " $neutre"
sleep 3
which jpegoptim > /dev/null
if [ $? = 1 ]
then
sudo apt install -y jpegoptim
fi
{
count=0
success=0
successlog=./success.tmp
find . -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) >> jpg_files.txt
total=$(wc -l < "jpg_files.txt")
TOTAL=$(($total + 0))
log=./log
gainlog=./gain.tmp
gain=0
echo "0" | tee $successlog $gainlog > /dev/null
}
IFS=$'\n' # make newlines the only separator
set -f # disable globbing
if [ ! -s "./jpg_files.txt" ]
then
echo -e " $violet"
echo "No JPG File in Directory"
echo -e " $neutre"
else
for i in $(cat < "./jpg_files.txt"); do
((count++))
echo -e " $jaune"
echo "Processing JPG #$count of $total"
echo -e -n " $neutre"
sizeold=$(wc -c "$i" | cut -d' ' -f1)
jpegoptim -p -t "$i"
sizenew=$(wc -c "$i" | cut -d' ' -f1)
difference=$((sizenew-sizeold))
# Check if new filesize is smaller
if [ $difference -lt 0 ]
then
printf "Compression was successfull. New File is %'.f Bytes smaller\n" \
$((-difference)) | tee -a $log
((success++))
echo $success > $successlog
((gain-=difference))
echo $gain > $gainlog
else
printf "Compression was not necessary\n" | tee -a $log
fi
# Print Statistics
printf "Successfully compressed %'.f of %'.f files\n" $(cat $successlog) $TOTAL | tee -a $log
printf "Safed a total of %'.f Bytes\n" $(cat $gainlog) | tee -a $log
done
zenity --notification --width=300 --height=100 --text "Successfully compressed $(cat $successlog) of $TOTAL files\nSafed a total of $(cat gain.tmp) Bytes"
fi
rm ./jpg_files.txt
sleep 2
####################################################################################
which optipng > /dev/null
if [ $? = 1 ]
then
sudo apt install -y optipng
fi
{
count=0
find . -type f \( -iname "*.png" \) >> png_files.txt
total=$(wc -l < "png_files.txt")
TOTAL=$(($TOTAL + $total))
}
IFS=$'\n' # make newlines the only separator
set -f # disable globbing
if [ ! -s "./png_files.txt" ]
then
echo -e " $violet"
echo "No PNG File in Directory"
echo -e " $neutre"
else
for i in $(cat < "./png_files.txt"); do
((count++))
echo -e " $jaune"
echo "Processing PNG #$count of $total"
echo -e -n " $neutre"
sizeold=$(wc -c "$i" | cut -d' ' -f1)
optipng -o5 -preserve "$i"
sizenew=$(wc -c "$i" | cut -d' ' -f1)
difference=$((sizenew-sizeold))
# Check if new filesize is smaller
if [ $difference -lt 0 ]
then
printf "Compression was successfull. New File is %'.f Bytes smaller\n" \
$((-difference)) | tee -a $log
((success++))
echo $success > $successlog
((gain-=difference))
echo $gain > $gainlog
else
printf "Compression was not necessary\n" | tee -a $log
fi
# Print Statistics
printf "Successfully compressed %'.f of %'.f files\n" $(cat $successlog) $TOTAL | tee -a $log
printf "Safed a total of %'.f Bytes\n" $(cat $gainlog) | tee -a $log
done
zenity --notification --width=300 --height=100 --text "Successfully compressed $(cat $successlog) of $TOTAL files\nSafed a total of $(cat gain.tmp) Bytes"
fi
rm ./png_files.txt
#####################################################################################
# VIDEO OPTIMISATION
#####################################################################################
echo ""
echo -e -n "$vert [2/3]$rouge VIDEO OPTIMISATION "
for i in `seq 27 $COLUMNS`;
do echo -n "."
done
echo -e " $neutre"
sleep 3
which ffmpeg > /dev/null
if [ $? = 1 ]
then
sudo apt install -y ffmpeg
fi
{
count=0
successlog=./success.tmp
gainlog=./gain.tmp
find . -type f -iname "*.mp4" -o -iname '*.mkv' -o -iname '*.avi' -o -iname '*.m4v' -o -iname '*.wmv'>> paths_file.txt
total=$(wc -l < "paths_file.txt")
TOTAL=$(($TOTAL + $total))
log=./log
# echo "0" | tee $successlog > /dev/null
limit=75 #If video is compressed for less than 75%, the compression will be cancelled
}
IFS=$'\n' # make newlines the only separator
set -f # disable globbing
if [ ! -s "paths_file.txt" ]
then
echo -e " $violet"
echo "No VIDEO File in Directory"
echo -e " $neutre"
else
for i in $(cat < "./paths_file.txt"); do
((count++))
echo -e " $jaune"
echo "Processing vidéo file #$count of $total"
echo "Current File: $i "
echo -e -n " $neutre"
extension="${i##*.}"
new="$i.$extension"
ffmpeg -y -i "$i" -vcodec libx265 -crf 28 "$new" || rm "$new"
sizeold=$(wc -c "$i" | cut -d' ' -f1)
sizenew=$(wc -c "$i.$extension" | cut -d' ' -f1)
difference=$((sizenew-sizeold))
perc=$((-difference*100/sizeold))
echo ""
# Check if new filesize is smaller
if [ $perc -ge $limit ]
then
mv "$new" "$i"
printf "Compression was successfull. New File is %'.f Bytes smaller\n" \
$((-difference)) | tee -a $log
((success++))
echo $success > $successlog
((gain-=difference))
echo $gain > $gainlog
else
rm "$new"
printf "Compression was not necessary\n" | tee -a $log
fi
# Print Statistics
printf "Successfully compressed %'.f of %'.f files\n" $(cat $successlog) $TOTAL | tee -a $log
printf "Safed a total of %'.f Bytes\n" $(cat $gainlog) | tee -a $log
done
zenity --notification --width=300 --height=100 --text "Successfully compressed $(cat $successlog) of $TOTAL files\nSafed a total of $(cat gain.tmp) Bytes"
fi
rm ./paths_file.txt
#####################################################################################
# PDF OPTIMISATION
#####################################################################################
echo ""
echo -e -n "$vert [3/3]$rouge PDF OPTIMISATION "
for i in `seq 25 $COLUMNS`;
do echo -n "."
done
echo -e " $neutre"
sleep 3
## Script to compress PDF Files using ghostscript incl. subdirs
## Copyright (C) 2016 Maximilian Fries - All Rights Reserved
## Contact: [email protected]
## Last revised 2022-01-05 by Corentin Michel (https://github.com/SKOHscripts)
# Variables and preparation
which gs > /dev/null
if [ $? = 1 ]
then
sudo apt install -y gs
fi
{
count=0
successlog=./success.tmp
gainlog=./gain.tmp
find . -type f -name "*.pdf" >> pdf_files.txt
total=$(wc -l < "pdf_files.txt")
TOTAL=$(($TOTAL + $total))
log=./log
verbose="-dQUIET"
mode="printer"
# echo "0" | tee $successlog > /dev/null
}
#Parameter Handling & Logging
{
echo "-- Debugging for Log START --"
echo "Number of Parameters: $#"
echo "Parameters are: $*"
echo "-- Debugging for Log END --"
} >> $log
# Only compression-mode set
if [ $# -eq 1 ]; then
mode="$1"
fi
# Also Verbose Level Set
if [ $# -eq 2 ]; then
mode="$1"
verbose=""
fi
IFS=$'\n' # make newlines the only separator
set -f # disable globbing
if [ ! -s "pdf_files.txt" ]
then
echo -e " $violet"
echo "No PDF File in Directory"
echo -e " $neutre"
else
for i in $(cat < "pdf_files.txt"); do
((count++))
echo -e " $jaune"
echo "Processing PDF #$count of $total" | tee -a $log
echo "Current File: $i "| tee -a $log
echo -e -n " $neutre"
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS="/$mode" -dNOPAUSE \ -dBATCH $verbose -sOutputFile="$i-new" "$i" | tee -a $log
sizeold=$(wc -c "$i" | cut -d' ' -f1)
sizenew=$(wc -c "$i-new" | cut -d' ' -f1)
difference=$((sizenew-sizeold))
# Check if new filesize is smaller
if [ $difference -lt 0 ]
then
rm "$i"
mv "$i-new" "$i"
printf "Compression was successfull. New File is %'.f Bytes smaller\n" \
$((-difference)) | tee -a $log
((success++))
echo $success > $successlog
((gain-=difference))
echo $gain > $gainlog
else
rm "$i-new"
printf "Compression was not necessary\n" | tee -a $log
fi
# Print Statistics
printf "Successfully compressed %'.f of %'.f files\n" $(cat $successlog) $TOTAL | tee -a $log
printf "Safed a total of %'.f Bytes\n" $(cat $gainlog) | tee -a $log
done
zenity --info --width=300 --height=100 --text "Successfully compressed $(cat $successlog) of $TOTAL files\nSafed a total of $(cat gain.tmp) Bytes"
fi
rm $successlog $gainlog $log ./pdf_files.txt
else
exit
fi