2023-05-05 13:35:13 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# --
|
2023-05-05 14:34:44 +02:00
|
|
|
# Description: Script that watches when new image (PNG or JPEG) are added and transform to WebP format.
|
2023-05-05 13:35:13 +02:00
|
|
|
# --
|
2023-05-05 14:34:44 +02:00
|
|
|
# Requirements: Install webp
|
|
|
|
# Example Debian: $sudo apt install webp
|
|
|
|
# --
|
|
|
|
# Cron: @reboot bash-folders-image-to-webp.sh >/dev/null 2>&1 &
|
2023-05-05 13:35:13 +02:00
|
|
|
# --
|
|
|
|
|
|
|
|
# START
|
|
|
|
set -e
|
|
|
|
|
|
|
|
|
|
|
|
# FUNCTIONS
|
|
|
|
|
|
|
|
usage() {
|
|
|
|
if [ "$*" != "" ] ; then
|
|
|
|
echo "Error: $*"
|
|
|
|
fi
|
|
|
|
|
|
|
|
cat << EOF
|
|
|
|
Usage: $PROGNAME [OPTION]
|
2023-05-05 14:34:44 +02:00
|
|
|
Watches when new image (PNG or JPEG) are added and transform to WebP format.
|
2023-05-05 13:35:13 +02:00
|
|
|
Options:
|
|
|
|
--folder [path] Folder path where will be monitored.
|
|
|
|
--help Display this usage message and exit
|
|
|
|
EOF
|
|
|
|
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
start() {
|
|
|
|
# Monitors the selected folder
|
|
|
|
inotifywait -m -e create,moved_to --format '%f' "$FOLDER_ORIGIN" |
|
|
|
|
while read -r filename; do
|
|
|
|
# Gets the file extension
|
|
|
|
extension="${filename##*.}"
|
|
|
|
# Checks if the extension is in the extension list
|
|
|
|
for ext in "${EXTENSIONS_TO_WATCH[@]}"; do
|
|
|
|
if [[ "$ext" = "$extension" ]]; then
|
2023-05-05 14:52:26 +02:00
|
|
|
filename_output="${filename%.*}.webp"
|
|
|
|
# Displays a flat file of information
|
|
|
|
touch "$FOLDER_ORIGIN/$MESSAGE_WAITING"
|
|
|
|
# Converts the image to WebP
|
|
|
|
cwebp -q "$QUALITY" "$FOLDER_ORIGIN/$filename" -o "$FOLDER_ORIGIN/$filename_output"
|
|
|
|
# Remove a flat file of information
|
|
|
|
rm "$FOLDER_ORIGIN/$MESSAGE_WAITING"
|
2023-05-05 13:35:13 +02:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2023-05-05 14:34:44 +02:00
|
|
|
|
2023-05-05 13:35:13 +02:00
|
|
|
# CONTROLE ARGUMENTS
|
2023-05-05 14:34:44 +02:00
|
|
|
|
|
|
|
# Parse command line arguments
|
|
|
|
while [[ $# -gt 0 ]]
|
|
|
|
do
|
|
|
|
key="$1"
|
|
|
|
case $key in
|
|
|
|
--folder)
|
|
|
|
FOLDER_ORIGIN="$2"
|
|
|
|
shift # past argument
|
|
|
|
shift # past value
|
|
|
|
;;
|
|
|
|
--quality)
|
|
|
|
QUALITY="$2"
|
|
|
|
shift # past argument
|
|
|
|
shift # past value
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
usage "Unknown option: $1"
|
|
|
|
;;
|
2023-05-05 13:35:13 +02:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2023-05-05 14:34:44 +02:00
|
|
|
# VARIABLES
|
|
|
|
PROGNAME=$(basename "$0")
|
2023-05-05 14:52:26 +02:00
|
|
|
QUALITY="90"
|
|
|
|
MESSAGE_WAITING="converting_please_wait"
|
2023-05-05 14:34:44 +02:00
|
|
|
EXTENSIONS_TO_WATCH=("jpg" "jpeg" "png")
|
|
|
|
|
|
|
|
# CHECKS
|
|
|
|
|
|
|
|
# Check if exists cwebp
|
2023-05-05 14:52:26 +02:00
|
|
|
if ! which cwebp > /dev/null; then
|
2023-05-05 14:34:44 +02:00
|
|
|
echo "Error: You must install the WebP terminal tools."
|
|
|
|
exit 1
|
2023-05-05 14:52:26 +02:00
|
|
|
fi
|
2023-05-05 14:34:44 +02:00
|
|
|
|
|
|
|
# Check if the required --folder flag is provided
|
|
|
|
if [ -z "$FOLDER_ORIGIN" ]; then
|
|
|
|
echo "Error: The --folder flag is required."
|
|
|
|
exit 1
|
2023-05-05 14:52:26 +02:00
|
|
|
fi
|
|
|
|
|
2023-05-05 14:34:44 +02:00
|
|
|
start
|