bash-folders/bash-folders-image-to-webp.sh

100 lines
2.1 KiB
Bash
Raw Normal View History

2023-03-22 00:33:13 +01:00
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() {
# Output
cd "$FOLDER_ORIGIN"
# 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##*.}"
filepath=$(readlink -f "$FOLDER_ORIGIN/$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:34:44 +02:00
filename_output="optimized_${filename%.*}.mp4"
2023-05-05 13:35:13 +02:00
# Decompresses the file
filetype=$(file -b "$filepath" | awk '{print $1}')
2023-05-05 14:34:44 +02:00
cwebp -q 90 example.jpeg -o example.webp
2023-05-05 13:35:13 +02:00
# Notifies that it has been terminated
send-notification "Descompressing $filename finished."
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")
FOLDER_ORIGIN="$2"
EXTENSIONS_TO_WATCH=("jpg" "jpeg" "png")
# CHECKS
# Check if exists cwebp
if ! which -q cwebp; then
echo "Error: You must install the WebP terminal tools."
exit 1
# Check if the required --folder flag is provided
if [ -z "$FOLDER_ORIGIN" ]; then
echo "Error: The --folder flag is required."
exit 1
start