Add script

This commit is contained in:
Andros Fenollosa 2023-05-05 14:52:26 +02:00
parent 1dc1d2b410
commit 3dcce1b0d3

26
bash-folders-image-to-webp.sh Normal file → Executable file
View File

@ -1,4 +1,3 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# -- # --
@ -33,23 +32,21 @@ EOF
} }
start() { start() {
# Output
cd "$FOLDER_ORIGIN"
# Monitors the selected folder # Monitors the selected folder
inotifywait -m -e create,moved_to --format '%f' "$FOLDER_ORIGIN" | inotifywait -m -e create,moved_to --format '%f' "$FOLDER_ORIGIN" |
while read -r filename; do while read -r filename; do
# Gets the file extension # Gets the file extension
extension="${filename##*.}" extension="${filename##*.}"
filepath=$(readlink -f "$FOLDER_ORIGIN/$filename")
# Checks if the extension is in the extension list # Checks if the extension is in the extension list
for ext in "${EXTENSIONS_TO_WATCH[@]}"; do for ext in "${EXTENSIONS_TO_WATCH[@]}"; do
if [[ "$ext" = "$extension" ]]; then if [[ "$ext" = "$extension" ]]; then
filename_output="optimized_${filename%.*}.mp4" filename_output="${filename%.*}.webp"
# Decompresses the file # Displays a flat file of information
filetype=$(file -b "$filepath" | awk '{print $1}') touch "$FOLDER_ORIGIN/$MESSAGE_WAITING"
cwebp -q 90 example.jpeg -o example.webp # Converts the image to WebP
# Notifies that it has been terminated cwebp -q "$QUALITY" "$FOLDER_ORIGIN/$filename" -o "$FOLDER_ORIGIN/$filename_output"
send-notification "Descompressing $filename finished." # Remove a flat file of information
rm "$FOLDER_ORIGIN/$MESSAGE_WAITING"
fi fi
done done
done done
@ -81,19 +78,22 @@ done
# VARIABLES # VARIABLES
PROGNAME=$(basename "$0") PROGNAME=$(basename "$0")
FOLDER_ORIGIN="$2" QUALITY="90"
MESSAGE_WAITING="converting_please_wait"
EXTENSIONS_TO_WATCH=("jpg" "jpeg" "png") EXTENSIONS_TO_WATCH=("jpg" "jpeg" "png")
# CHECKS # CHECKS
# Check if exists cwebp # Check if exists cwebp
if ! which -q cwebp; then if ! which cwebp > /dev/null; then
echo "Error: You must install the WebP terminal tools." echo "Error: You must install the WebP terminal tools."
exit 1 exit 1
fi
# Check if the required --folder flag is provided # Check if the required --folder flag is provided
if [ -z "$FOLDER_ORIGIN" ]; then if [ -z "$FOLDER_ORIGIN" ]; then
echo "Error: The --folder flag is required." echo "Error: The --folder flag is required."
exit 1 exit 1
fi
start start