From 48bf79fa77d1cbbd1ef8e9ad52482654c4bc5128 Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Sat, 4 Nov 2023 15:03:14 +0100 Subject: [PATCH] Add AVIF --- README.md | 99 ++++++++++++++++++++++++++++++++++- bash-folders-image-to-avif.sh | 93 ++++++++++++++++++++++++++++++++ 2 files changed, 191 insertions(+), 1 deletion(-) create mode 100755 bash-folders-image-to-avif.sh diff --git a/README.md b/README.md index e6d7175..420a641 100644 --- a/README.md +++ b/README.md @@ -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. @@ -311,7 +408,7 @@ Add to document. ## Collaborations & Pull Requests -You must provide the documentation, as well as the scripts present, test that it works well and the script must pass a `shellcheck` (below you will find an example of execution). +You must provide the documentation, as well as the scripts present, test that it works well and the script must pass a `shellcheck` (below you will find an example of execution). ```sh shellcheck [script] diff --git a/bash-folders-image-to-avif.sh b/bash-folders-image-to-avif.sh new file mode 100755 index 0000000..b1fc82c --- /dev/null +++ b/bash-folders-image-to-avif.sh @@ -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