This repository was archived by the owner on Nov 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_thumbnails
More file actions
455 lines (375 loc) Β· 18.4 KB
/
generate_thumbnails
File metadata and controls
455 lines (375 loc) Β· 18.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#!/bin/bash
# Website: https://github.com/antonionarcilio
# Author: Antonio Narcilio
# Maintenance: Antonio Narcilio
# Version: 2.0.1
#
# --------------------------------------------------------------------------- #
# π‘ DEPENDENCIES REQUIRED
# 1. ffmpeg
# 2. ImageMagick
# 3. kdialog
#
# --------------------------------------------------------------------------- #
# π‘ WHAT DOES THIS SCRIPT DO ?
# This script installs the necessary dependencies for the contact sheet
# creation script to work correctly
#
# --------------------------------------------------------------------------- #
#
# π‘ HOW TO USE IT?
#
# 1. Run in the terminal via the command line:
# 1.1 $ generate_thumbnails "video_name.mp4"
#
# Or
#
# 2. Run in the dolphin manager:
# 2.1 Right-click on the video you want to generate the thumbnail, in the
# context menu that opens select "GENERATE THUMBNAILS" and wait for the
# thumbnail creation process
#
# --------------------------------------------------------------------------- #
#
# π‘ FOR MORE INFORMATION, READ README.MD
#
#
# config shellcheck
# shellcheck disable=SC2116
#
# - - - - - - - - - - - - - - GLOBAL VARIABLES - - - - - - - - - - - - - - #
# You may change
KD_ICON="an-effects";
KD_ICON_ERROR="an-error";
KD_ICON_WARNING="an-alert";
VIDEO_FORMAT=("avi" "mp4" "m4v" "mov" "mpg" "mpeg" "wmv" "mkv" "ts");
IS_IT_A_VALID_FORMAT=1;
# NOT CHANGE
CURRENT_DIR=$(dirname "$1");
FILE=$(realpath "$1");
FILE_NAME=$(basename -- "$FILE");
EXTENSION="${FILE_NAME##*.}";
FILE_NAME="${FILE_NAME%.*}";
SUFFIX_EXTENSION="webp";
TMP_DIR=/tmp/thumbnails-$FILE_NAME-${RANDOM}/;
TMP_TILE_FILE="$TMP_DIR/.grid";
# FONT=/usr/local/share/fonts/r/Roboto_Regular.ttf;
FONT=Roboto-Regular.ttf;
LOG=/tmp/log_generate_thumbnails;
# Getting display size
AXIS_X=$(xrandr -q | grep Screen | awk '{ print $8 }');
AXIS_Y=$(xrandr -q | grep Screen | awk '{ print $10 }' | awk -F"," '{ print $1 }');
# ASK=/tmp/.kdialog-dontagain;
# touch ${ASK};
mkdir -p "$TMP_DIR";
# - - - - - - - - - - - - - - KDIALOG FUNCTIONS - - - - - - - - - - - - - - #
# Expect to pass 3 parameters "TITLE" "MESSAGE" "TIME"
function kdialogPopUp() {
kdialog \
--title "$1" \
--icon $KD_ICON \
--passivepopup "<h5>$2</h5>" "$3";
}
# Expect to pass 2 parameters "TITLE" "MESSAGE"
function kdialogPopUpWarning() {
kdialog \
--title "$1" \
--icon $KD_ICON_WARNING \
--passivepopup "<h5>$2</h5>" 10;
}
# No parameters needed
function kdialogError() {
kdialog \
--title "Error β" \
--icon $KD_ICON_ERROR \
--error "Something unexpected happened π’";
exit 0 > /dev/null 2>&1;
}
# Expect to pass 2 parameters "TITLE" "MESSAGE"
# function kdialogYesNo() {
# kdialog \
# --icon "$KD_ICON" \
# --title "$1" \
# --yesno "<h4>$2</h4>" \
# --dontagain ${ASK}:do-not-show;
# KD_YESNO_STATUS=$?;
# }
# Expect to pass 2 parameters "TITLE" "MESSAGE"
function kdialogWarning() {
kdialog \
--title "$1" \
--icon $KD_ICON_WARNING \
--warningcontinuecancel "$2";
}
# EXPECTED 1 ARGUMENTS β "FILE.txt"
function kdialogTextBox() {
kdialog \
--title "β Errors found β " \
--textbox "$1" \
--geometry 500x300+"${AXIS_X}"+"${AXIS_Y}";
exit 0 > /dev/null 2>&1;
}
# - - - - - - - - - - - - - - - - FUNCTIONS - - - - - - - - - - - - - - - - #
function generatingThumbnail() {
kdialogPopUp "Please wait..." "Thumbnail generation process started !" 5;
function kdialogComboBox() {
kd_option=('6x6' '5x5' '4x4' '3x3');
CHOOSE_USER=$(kdialog --title "$1" \
--icon $KD_ICON \
--combobox "$2" "${kd_option[0]}" "${kd_option[1]}" "${kd_option[2]}" "${kd_option[3]}" \
--geometry 300x50+"$AXIS_X"+"$AXIS_Y" \
--default "${kd_option[1]}");
KD_COMBOXBOX_STATUS=$?;
echo "$CHOOSE_USER" > "$TMP_TILE_FILE";
sed -i 's/x/\n/g' "$TMP_TILE_FILE" 2>> $LOG;
index=0;
while IFS= read -r line; do
TILE[$index]=$(echo "$line" 2>> $LOG);
index=$((index + 1));
done < "$TMP_TILE_FILE";
}
kdialogComboBox "Choose an option" "<h4><b>The thumbnail will have a grid of how much ?</b></h4>";
if [[ $KD_COMBOXBOX_STATUS -eq 0 ]]; then
file_output="${CURRENT_DIR}/${FILE_NAME}_${CHOOSE_USER}-preview.jpeg";
tmp_file_output="$TMP_DIR/$FILE_NAME"-tmp_preview.${SUFFIX_EXTENSION};
add_zero="${TMP_DIR}/.add-zero.txt";
nframes=$(echo "scale=0;${TILE[0]}*${TILE[1]}" | bc);
duration=$(ffmpeg -i "$FILE" 2>&1 | grep "Duration"| cut -d ' ' -f 4 | sed s/,// | sed 's@\..*@@g' | awk '{ split($1, A, ":"); split(A[3], B, "."); print 3600*A[1] + 60*A[2] + B[1] }');
# -------------------------- ADITIONAL FRAMES -------------------------- #
# Adding 2 frames, which will be removed at the end of the process.
# They are added to prevent the first frame from going blank or a frame
# from appearing where the video itself 'hasn't been initialized yet'.
nframes=$((nframes +2));
for (( variable=0; variable < nframes; variable++ )) do
offset=$(echo "scale=2;${variable}*${duration}/${nframes}+${duration}/${nframes}/2" | bc);
# |-> Time of the video where a print will be taken
# Solution if the returned value is similar to .18 (millisecond), ffmpeg returns an error
# stating that it doesn't recognize this value, so add a zero before the
# value, making the problem no longer happen
echo "$offset" > "$add_zero";
sed -i 's/^/0/' "$add_zero";
while IFS= read -r line; do
offset=$(echo "$line" 2>> $LOG);
done < "$add_zero";
if [ "$variable" -gt 9 ];then
zeros="00";
elif [ "$variable" -gt 99 ];then
zeros="0";
elif [ "$variable" -gt 999 ];then
zeros="";
else
zeros="000";
fi;
ffmpeg -start_at_zero -copyts -ss "$offset" -i "$FILE" \
-vf "scale=-2:1080,setsar=1:1,\
drawbox=x=1660:\y=1020:w=in_w:h=65:color=black@0.5:t=fill, \
drawtext=fontfile=${FONT}:\
fontsize=60:\
fontcolor=white:\
shadowcolor=black@0.5:\
shadowx=2:\
shadowy=2:\
box=1:\
boxcolor=black@0:\
x=(W-tw)/1.0050:y=H-th-7:\
text='%{pts\:gmtime\:0\:%H\\\\\:%M\\\\\:%S}'" \
-vframes 1 "${TMP_DIR}${zeros}${variable}.${SUFFIX_EXTENSION}" 2>> $LOG;
[[ $? -eq 1 ]] && kdialogTextBox "$LOG" && return 1 > /dev/null 2>&1;
done;
# ----------------------- REMOVE ADITIONAL FRAMES ----------------------- #
# remove the first and the ultimate thumbnail
rm -rf "${TMP_DIR}/0000.${SUFFIX_EXTENSION}" "${TMP_DIR}/${zeros}$((variable -1)).${SUFFIX_EXTENSION}" 2>> $LOG;
function montageThumbnail() {
# going back to the original value
nframes=$((nframes - 2));
function thumbnailThreeByThree() {
montage "${TMP_DIR}"/{0001..0003}."${SUFFIX_EXTENSION}" -background white -geometry +"${TILE[0]}"+1 -tile "${TILE[0]}x1" "${TMP_DIR}/line_1.${SUFFIX_EXTENSION}" && \
montage "${TMP_DIR}"/{0004..0006}."${SUFFIX_EXTENSION}" -background white -geometry +"${TILE[0]}"+1 -tile "${TILE[0]}x1" "${TMP_DIR}/line_2.${SUFFIX_EXTENSION}" && \
montage "${TMP_DIR}"/{0007..009}."${SUFFIX_EXTENSION}" -background white -geometry +"${TILE[0]}"+1 -tile "${TILE[0]}x1" "${TMP_DIR}/line_3.${SUFFIX_EXTENSION}" && \
montage "${TMP_DIR}"/line_{1..2}."${SUFFIX_EXTENSION}" -background white -geometry +1+2 -tile "1x2" "${TMP_DIR}/montage-1.${SUFFIX_EXTENSION}" 2> $LOG && \
montage "${TMP_DIR}"/line_3."${SUFFIX_EXTENSION}" -background white -geometry +1+2 -tile "1x2" "${TMP_DIR}/montage-2.${SUFFIX_EXTENSION}" 2> $LOG && \
creatingFinalFile() {
montage "${TMP_DIR}"/montage*."${SUFFIX_EXTENSION}" -background white -geometry +1+2 -tile "1x2" "$tmp_file_output" 2> $LOG;
}
processorUsage creatingFinalFile;
# expect to pass 1 argument - "function"
[[ $? -eq 1 ]] && kdialogTextBox "$LOG";
} # end of thumbnailThreeByThree function
function thumbnailFourByFour() {
montage "${TMP_DIR}"/{0001..0004}."${SUFFIX_EXTENSION}" -background white -geometry +"${TILE[0]}"+1 -tile "${TILE[0]}x1" "${TMP_DIR}/line_1.${SUFFIX_EXTENSION}" && \
montage "${TMP_DIR}"/{0005..0008}."${SUFFIX_EXTENSION}" -background white -geometry +"${TILE[0]}"+1 -tile "${TILE[0]}x1" "${TMP_DIR}/line_2.${SUFFIX_EXTENSION}" && \
montage "${TMP_DIR}"/{0009..0012}."${SUFFIX_EXTENSION}" -background white -geometry +"${TILE[0]}"+1 -tile "${TILE[0]}x1" "${TMP_DIR}/line_3.${SUFFIX_EXTENSION}" && \
montage "${TMP_DIR}"/{0013..0016}."${SUFFIX_EXTENSION}" -background white -geometry +"${TILE[0]}"+1 -tile "${TILE[0]}x1" "${TMP_DIR}/line_4.${SUFFIX_EXTENSION}";
montage "${TMP_DIR}"/line_{1..2}."${SUFFIX_EXTENSION}" -background white -geometry +1+2 -tile "1x2" "${TMP_DIR}/montage-1.${SUFFIX_EXTENSION}" 2> $LOG && \
montage "${TMP_DIR}"/line_{3..4}."${SUFFIX_EXTENSION}" -background white -geometry +1+2 -tile "1x2" "${TMP_DIR}/montage-2.${SUFFIX_EXTENSION}" 2> $LOG && \
creatingFinalFile() {
montage "${TMP_DIR}"/montage*."${SUFFIX_EXTENSION}" -background white -geometry +1+2 -tile "1x2" "$tmp_file_output" 2> $LOG;
}
processorUsage creatingFinalFile;
# expect to pass 1 argument - "function"
[[ $? -eq 1 ]] && kdialogTextBox "$LOG";
} # end of thumbnailFourByFour function
function thumbnailFiveByFive() {
montage "${TMP_DIR}"/{0001..0005}."${SUFFIX_EXTENSION}" -background white -geometry +"${TILE[0]}"+1 -tile "${TILE[0]}x1" "${TMP_DIR}/line_1.${SUFFIX_EXTENSION}" && \
montage "${TMP_DIR}"/{0006..0010}."${SUFFIX_EXTENSION}" -background white -geometry +"${TILE[0]}"+1 -tile "${TILE[0]}x1" "${TMP_DIR}/line_2.${SUFFIX_EXTENSION}" && \
montage "${TMP_DIR}"/{0011..0015}."${SUFFIX_EXTENSION}" -background white -geometry +"${TILE[0]}"+1 -tile "${TILE[0]}x1" "${TMP_DIR}/line_3.${SUFFIX_EXTENSION}" && \
montage "${TMP_DIR}"/{0016..0020}."${SUFFIX_EXTENSION}" -background white -geometry +"${TILE[0]}"+1 -tile "${TILE[0]}x1" "${TMP_DIR}/line_4.${SUFFIX_EXTENSION}" && \
montage "${TMP_DIR}"/{0021..0025}."${SUFFIX_EXTENSION}" -background white -geometry +"${TILE[0]}"+1 -tile "${TILE[0]}x1" "${TMP_DIR}/line_5.${SUFFIX_EXTENSION}";
montage "${TMP_DIR}"/line_{1..2}."${SUFFIX_EXTENSION}" -background white -geometry +1+2 -tile "1x2" "${TMP_DIR}/montage-1.${SUFFIX_EXTENSION}" 2> $LOG && \
montage "${TMP_DIR}"/line_{3..4}."${SUFFIX_EXTENSION}" -background white -geometry +1+2 -tile "1x2" "${TMP_DIR}/montage-2.${SUFFIX_EXTENSION}" 2> $LOG && \
creatingFinalFile() {
montage "${TMP_DIR}"/montage*."${SUFFIX_EXTENSION}" "${TMP_DIR}"/line_5*."${SUFFIX_EXTENSION}" -background white -geometry +1+3 -tile "1x3" "$tmp_file_output" 2> $LOG;
}
processorUsage creatingFinalFile;
# expect to pass 1 argument - "function"
[[ $? -eq 1 ]] && kdialogTextBox "$LOG";
} # end of thumbnailFiveByFive function
function thumbnailSixBySix() {
montage "${TMP_DIR}"/{0001..0006}."${SUFFIX_EXTENSION}" -background white -geometry +"${TILE[0]}"+1 -tile "${TILE[0]}x1" "${TMP_DIR}/line_1.${SUFFIX_EXTENSION}" && \
montage "${TMP_DIR}"/{0007..0012}."${SUFFIX_EXTENSION}" -background white -geometry +"${TILE[0]}"+1 -tile "${TILE[0]}x1" "${TMP_DIR}/line_2.${SUFFIX_EXTENSION}" && \
montage "${TMP_DIR}"/{0013..0018}."${SUFFIX_EXTENSION}" -background white -geometry +"${TILE[0]}"+1 -tile "${TILE[0]}x1" "${TMP_DIR}/line_3.${SUFFIX_EXTENSION}" && \
montage "${TMP_DIR}"/{0019..0024}."${SUFFIX_EXTENSION}" -background white -geometry +"${TILE[0]}"+1 -tile "${TILE[0]}x1" "${TMP_DIR}/line_4.${SUFFIX_EXTENSION}" && \
montage "${TMP_DIR}"/{0025..0030}."${SUFFIX_EXTENSION}" -background white -geometry +"${TILE[0]}"+1 -tile "${TILE[0]}x1" "${TMP_DIR}/line_5.${SUFFIX_EXTENSION}" && \
montage "${TMP_DIR}"/{0031..0036}."${SUFFIX_EXTENSION}" -background white -geometry +"${TILE[0]}"+1 -tile "${TILE[0]}x1" "${TMP_DIR}/line_6.${SUFFIX_EXTENSION}";
montage "${TMP_DIR}"/line_{1..2}."${SUFFIX_EXTENSION}" -background white -geometry +1+2 -tile "1x2" "${TMP_DIR}/montage-1.${SUFFIX_EXTENSION}" 2> $LOG && \
montage "${TMP_DIR}"/line_{3..4}."${SUFFIX_EXTENSION}" -background white -geometry +1+2 -tile "1x2" "${TMP_DIR}/montage-2.${SUFFIX_EXTENSION}" 2> $LOG && \
montage "${TMP_DIR}"/line_{5..6}."${SUFFIX_EXTENSION}" -background white -geometry +1+2 -tile "1x2" "${TMP_DIR}/montage-3.${SUFFIX_EXTENSION}" 2> $LOG && \
creatingFinalFile() {
montage "${TMP_DIR}"/montage*."${SUFFIX_EXTENSION}" -background white -geometry +1+3 -tile "1x3" "$tmp_file_output" 2> $LOG;
}
processorUsage creatingFinalFile;
# expect to pass 1 argument - "function"
[[ $? -eq 1 ]] && kdialogTextBox "$LOG";
} # end of thumbnailSixBySix function
case ${nframes} in
9) processorUsage thumbnailThreeByThree;;
16) processorUsage thumbnailFourByFour;;
25) processorUsage thumbnailFiveByFive;;
36) processorUsage thumbnailSixBySix;;
*) processorUsage thumbnailFiveByFive;;
esac;
rm -rf "${TMP_DIR}"/line_*."${SUFFIX_EXTENSION}" "${TMP_DIR}"/montage*."${SUFFIX_EXTENSION}" ;
}
processorUsage montageThumbnail;
# expect to pass 1 argument - "function"
convert "${tmp_file_output}" -resize 1920x1080 "${file_output}" 2>> $LOG;
status=$?;
[[ $status -eq 1 ]] && kdialogTextBox "$LOG";
if [[ "$status" -eq 0 ]]; then
kdialogPopUp "Finish π" "Successfully β
generated thumbnails π₯³<br>Saved to: π<br><a href='${file_output}'>${file_output}</a>" 5;
# |-> Expect to pass 3 parameters "TITLE" "MESSAGE" "TIME"
else
kdialogPopUpWarning "Ops ! π" "An error occurred while generating the thumbnail<br>Thumbnail cannot be generated! π’";
# |-> Expect to pass 2 parameters "TITLE" "MESSAGE"
fi
else
kdialogPopUp "Canceled !" "Thumbnail will not be generated !" 5;
# |-> Expect to pass 3 parameters "TITLE" "MESSAGE" "TIME"
fi;
}
function checkingFileFormat() {
index=0;
for index in "${!VIDEO_FORMAT[@]}"; do
[[ $EXTENSION == "${VIDEO_FORMAT[index]}" ]] && IS_IT_A_VALID_FORMAT=0;
done;
if [[ $IS_IT_A_VALID_FORMAT -eq 0 ]]; then
processorUsage generatingThumbnail;
# expect to pass 1 argument - "function"
else
kdialogPopUpWarning "Nothing found !" "No files of type <b>${VIDEO_FORMAT[*]}</b> were found!";
# |-> Expect to pass 2 parameters "TITLE" "MESSAGE"
fi;
}
function main() {
echo "" > $LOG;
# Parameter exist ?
if [[ $FILE != '' ]]; then
# kdialogYesNo "Choose an option" "Do you want to create a thumbnail of the video ? π<br><u>$FILE_NAME</u>";
# |-> Expect to pass 2 parameters "TITLE" "MESSAGE"
# β οΈ Prevention in case the user runs the script on dolphin 'unintentionally'
# if [[ $KD_YESNO_STATUS -eq 0 ]]; then
checkingFileFormat;
# elif [[ $KD_YESNO_STATUS -eq 1 ]]; then
# kdialogPopUp "Canceled !" "Thumbnail will not be generated !" 5;
# # |-> Expect to pass 3 parameters "TITLE" "MESSAGE" "TIME"
# else
# kdialogError;
# fi
# Parameter does not exist ?
elif [[ $FILE == '' ]]; then
kdialogPopUpWarning "Nothing found !" "No files of type <b>${VIDEO_FORMAT[*]}</b> were found!";
# |-> Expect to pass 2 parameters "TITLE" "MESSAGE"
else
kdialogError;
fi
}
# - - - - - - - - - - - - CHECK IF DEPENDENCIES EXIST - - - - - - - - - - - - #
function verifyDependencies() {
# imagemagick is isntall ?
function imagemagickVerify() {
convert --version > /dev/null 2>&1;
is_exist_imagemagick=$?;
if [[ $is_exist_imagemagick -ne 0 ]]; then
msg=("ImageMagic is required !" "Access the site: <a href='https://imagemagick.org/script/download.php'>ImageMagick</a> for more,<br>or run the command<br><b>sudo \"apt/pacman -S\" imagemagick</b>");
kdialogPopUpWarning "${msg[0]}" "${msg[1]}";
# |-> Expect to pass 2 parameters "TITLE" "MESSAGE"
echo -e "\e[1;33m${msg[0]}\e[0m";
fi;
}
# ffmpeg is isntall ?
function ffmpegVerify() {
ffmpeg -version > /dev/null 2>&1;
is_exist_ffmpeg=$?;
if [[ $is_exist_ffmpeg -ne 0 ]]; then
msg=("FFMPEG is required !" "Access the site: <a href='https://ffmpeg.org/download.html'>FFMPEG</a> for more,<br>or run the command<br><b>sudo \"apt/pacman -S\" ffmpeg</b>");
kdialogPopUpWarning "${msg[0]}" "${msg[1]}";
# |-> Expect to pass 2 parameters "TITLE" "MESSAGE"
echo -e "\e[1;33m${msg[0]}\e[0m";
fi;
}
# kdialog is isntall ?
function kdialogVerify() {
kdialog --version > /dev/null 2>&1;
is_exist_kdialog=$?;
if [[ $is_exist_kdialog -eq 0 ]]; then
imagemagickVerify;
ffmpegVerify;
else
echo -e "Kdialog is required!\nRun the command:\n\e[1;33msudo \"apt/pacman -S\" ffmpeg\e[0m";
exit 0 > /dev/null 2>&1;
fi;
}
kdialogVerify;
if [[ $is_exist_kdialog -eq 0 && is_exist_ffmpeg -eq 0 && $is_exist_imagemagick -eq 0 ]]; then
processorUsage main;
else
exit 0 > /dev/null 2>&1;
fi
}
# - - - - - - - - - - - - - PC CRASH PREVENTION - - - - - - - - - - - - - #
# expect to pass 1 argument - "function"
function processorUsage() {
checkProcessUsage () {
PROCESSOR_USAGE=$(awk -v a="$(awk '/cpu /{print $2+$4,$2+$4+$5}' /proc/stat; sleep 1)" '/cpu /{split(a,b," "); print 100*($2+$4-b[1])/($2+$4+$5-b[2])}' /proc/stat);
PROCESSOR_USAGE=$(echo "${PROCESSOR_USAGE}" | awk '{printf("%d\n",$1)}');
# echo $PROCESSOR_USAGE;
PERCENTAGE=90;
}
checkProcessUsage;
if [[ ${PROCESSOR_USAGE} -gt ${PERCENTAGE} ]]; then
cont_final_process=0;
while [[ ${PROCESSOR_USAGE} -gt ${PERCENTAGE} ]]; do
echo -e "\e[2;33mProcessor consuming more than ${PERCENTAGE}%, await ...\e[0m";
[[ ${cont_final_process} == 20 ]] && kdialogPopUpWarning 'Process terminated' 'Process terminated due to excessive CPU consumption!';
sleep 20;
cont_final_process=$((cont_final_process + 1));
checkProcessUsage;
done;
$1;
else
$1;
fi;
}
# - - - - - - - - - - - - - - - - EXECUTION - - - - - - - - - - - - - - - - #
processorUsage verifyDependencies;
# expect to pass 1 argument - "function"