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

This commit is contained in:
Andros Fenollosa 2023-05-05 14:34:44 +02:00 committed by GitHub
parent b27ef31bdb
commit 1dc1d2b410
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,25 +2,17 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# -- # --
# Description: Script that decompresses all the files that are left in the folder. # Description: Script that watches when new image (PNG or JPEG) are added and transform to WebP format.
# --nnnnnnn
# Requirements: Install inotify-tools, gzip bzip2 xz-utils unzip p7zip-full and unrar
# Example Debian: $sudo apt install inotify-tools gzip bzip2 xz-utils unzip p7zip-full unrar
# -- # --
# Cron: @reboot bash-folders-descompress.sh >/dev/null 2>&1 & # Requirements: Install webp
# Example Debian: $sudo apt install webp
# --
# Cron: @reboot bash-folders-image-to-webp.sh >/dev/null 2>&1 &
# -- # --
# START # START
set -e set -e
# EXPORTS
# Fix: notify-send command doesn't launch the notification through systemd service
export DBUS_SESSION_BUS_ADDRESS="${DBUS_SESSION_BUS_ADDRESS:-unix:path=/run/user/${UID}/bus}"
# VARIABLES
PROGNAME=$(basename "$0")
FOLDER_ORIGIN="$2"
EXTENSIONS_TO_WATCH=("gzip" "bzip2" "xz" "zip" "7z" "rar")
# FUNCTIONS # FUNCTIONS
@ -31,7 +23,7 @@ usage() {
cat << EOF cat << EOF
Usage: $PROGNAME [OPTION] Usage: $PROGNAME [OPTION]
Watches new compress files and decompresses all the files. Watches when new image (PNG or JPEG) are added and transform to WebP format.
Options: Options:
--folder [path] Folder path where will be monitored. --folder [path] Folder path where will be monitored.
--help Display this usage message and exit --help Display this usage message and exit
@ -40,17 +32,6 @@ EOF
exit 1 exit 1
} }
send-notification() {
if command -v notify-send >/dev/null 2>&1; then
# Send a native notification
notify-send "$1"
else
# If the above command is not available, print by console
echo "$1"
fi
}
start() { start() {
# Output # Output
cd "$FOLDER_ORIGIN" cd "$FOLDER_ORIGIN"
@ -63,33 +44,10 @@ start() {
# 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
# Notifies that the conversion is to be started filename_output="optimized_${filename%.*}.mp4"
send-notification "Descompressing '$filename', please wait."
# Decompresses the file # Decompresses the file
filetype=$(file -b "$filepath" | awk '{print $1}') filetype=$(file -b "$filepath" | awk '{print $1}')
case "$filetype" in cwebp -q 90 example.jpeg -o example.webp
"gzip")
gzip -d "$filepath"
;;
"bzip2")
bzip2 -d "$filepath"
;;
"XZ")
xz -d "$filepath"
;;
"Zip")
unzip "$filepath"
;;
"7-zip")
7z x "$filepath"
;;
"RAR")
unrar x "$filepath"
;;
*)
send-notification "Error: Unknown file type $filetype"
;;
esac
# Notifies that it has been terminated # Notifies that it has been terminated
send-notification "Descompressing $filename finished." send-notification "Descompressing $filename finished."
fi fi
@ -97,27 +55,45 @@ start() {
done done
} }
# CONTROLE ARGUMENTS
isArg=""
while [ $# -gt 0 ] ; do # CONTROLE ARGUMENTS
case "$1" in
--help) # Parse command line arguments
usage while [[ $# -gt 0 ]]
;; do
key="$1"
case $key in
--folder) --folder)
isArg="1" FOLDER_ORIGIN="$2"
if [ $# -eq 2 ]; then shift # past argument
start shift # past value
else ;;
usage "You need to specify the path of the folder to watch." --quality)
fi QUALITY="$2"
shift # past argument
shift # past value
;; ;;
*) *)
usage "Unknown option: $1"
;;
esac esac
shift
done done
if [ -z $isArg ] ; then # VARIABLES
usage "Not enough arguments" PROGNAME=$(basename "$0")
fi 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