This commit is contained in:
Andros Fenollosa 2023-11-04 15:03:14 +01:00
parent cdcc7cd171
commit 48bf79fa77
2 changed files with 191 additions and 1 deletions

View File

@ -6,6 +6,7 @@ Small collection of Bash scripts to launch functionalities in folders when new f
- [Video optimizer](#video-optimizer): Folder that watches when new videos are added and optimizes them.
- [Battery hook](#battery-hook): Folder with custom scripts to be launched in different battery states.
- [Image to avif](#image-to-avif): Folder that watches when new image (PNG, JPEG or WebP) are added and transform to AVIF format.
- [Image to webp](#image-to-webp): Folder that watches when new image (PNG or JPEG) are added and transform to WebP format.
---
@ -215,6 +216,102 @@ Collaborations & Pull Requests
---
## Image to AVIF
Folder that watches when new image (PNG, JPEG or WebP) are added and transform to AVIF format.
### Requirements
- `avifenc`
Example in Debian.
``` sh
sudo apt install libavif-bin
```
### Install
``` sh
curl -o bash-folders-image-to-avif https://raw.githubusercontent.com/tanrax/bash-folders/main/bash-folders-image-to-avif.sh && chmod +x bash-folders-image-to-avif && sudo rm -f /usr/local/bin/bash-folders-image-to-avif && sudo mv bash-folders-image-to-avif /usr/local/bin && echo "🎉 Successfully installed! 🎉"
```
Test
``` sh
bash-folders-image-to-avif --help
```
### Run
``` sh
bash-folders-image-to-avif --folder [folder to watch]
```
Example.
``` sh
mkdir image-to-avif-converter
bash-folders-image-to-avif --folder image-to-avif-converter
```
And leave a image that you want to optimize in the folder `image-to-avif-converter`.
---
### Start at operating system startup
#### Option 1: Service
Create a file in `/etc/systemd/system/bash-folders-image-to-avif.service` with the following content.
```ini
[Unit]
Description=Folder that watches when new image (PNG, JPEG or WebP) are added and transform to AVIF format.
[Service]
Restart=always
RestartSec=5
User=[user]
ExecStart=bash-folders-image-to-avif --folder [folder to watch]
[Install]
WantedBy=multi-user.target
```
Edit it to your needs.
Recharge services.
``` sh
sudo systemctl daemon-reload
```
And activate it.
``` sh
sudo systemctl enable bash-folders-image-to-avif
sudo systemctl start bash-folders-image-to-avif
```
#### Option 2: Cron
Open.
``` sh
crontab -e
```
Add to document.
``` sh
@reboot bash-folders-image-to-avif --folder [folder to watch] >/dev/null 2>&1 &
```
---
## Image to WebP
Folder that watches when new image (PNG or JPEG) are added and transform to WebP format.

93
bash-folders-image-to-avif.sh Executable file
View File

@ -0,0 +1,93 @@
#!/usr/bin/env bash
# --
# Description: Script that watches when new image (PNG, JPEG or Webp) are added and transform to AVIF format.
# --
# Requirements: Install libavif
# Example Debian: $sudo apt install libavif-bin
# --
# Cron: @reboot bash-folders-image-to-avif.sh >/dev/null 2>&1 &
# --
# START
set -e
# FUNCTIONS
usage() {
if [ "$*" != "" ] ; then
echo "Error: $*"
fi
cat << EOF
Usage: $PROGNAME [OPTION]
Watches when new image (PNG, JPEG or WebP) are added and transform to AVIF format.
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
filename_output="${filename%.*}.avif"
# Displays a flat file of information
touch "$FOLDER_ORIGIN/$MESSAGE_WAITING"
# Converts the image to WebP
avifenc "$FOLDER_ORIGIN/$filename" "$FOLDER_ORIGIN/$filename_output"
# Remove a flat file of information
rm "$FOLDER_ORIGIN/$MESSAGE_WAITING"
fi
done
done
}
# CONTROLE ARGUMENTS
# Parse command line arguments
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--folder)
FOLDER_ORIGIN="$2"
shift # past argument
shift # past value
;;
*)
usage "Unknown option: $1"
;;
esac
done
# VARIABLES
PROGNAME=$(basename "$0")
MESSAGE_WAITING="converting_please_wait"
EXTENSIONS_TO_WATCH=("jpg" "jpeg" "png" "webp")
# CHECKS
# Check if exists cwebp
if ! which avifenc > /dev/null; then
echo "Error: You must install the AVIF terminal tools."
exit 1
fi
# Check if the required --folder flag is provided
if [ -z "$FOLDER_ORIGIN" ]; then
echo "Error: The --folder flag is required."
exit 1
fi
start