diff --git a/README.md b/README.md index 2839f5e..31fa021 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,13 @@ -# Dynamic folders +# Bash folders -Collection of Bash scripts that execute functionalities in folders. +Collection of Bash scripts to execute functionalities in folders, such as optimizing videos, unzipping files, converting images, etc. -## Video optmizer +- [Video optimizer](#video-optimizer) +- [Decompress files](#decompress-files) + +--- + +## Video optimizer Folder that watches when new videos are added and optimizes them. @@ -21,26 +26,26 @@ sudo apt install inotify-tools ffmpeg ``` sh -curl -o dynamic-folders-video-optimizer https://raw.githubusercontent.com/tanrax/dynamic-folders/main/dynamic-folders-video-optimizer.sh && chmod +x dynamic-folders-video-optimizer && sudo mv dynamic-folders-video-optimizer /usr/local/bin && echo "🎉 Successfully installed! 🎉" +curl -o bash-folders-video-optimizer https://raw.githubusercontent.com/tanrax/bash-folders/main/bash-folders-video-optimizer.sh && chmod +x bash-folders-video-optimizer && sudo mv bash-folders-video-optimizer /usr/local/bin && echo "🎉 Successfully installed! 🎉" ``` Test ``` sh -dynamic-folders-video-optimizer --help +bash-folders-video-optimizer --help ``` ### Run ``` sh -dynamic-folders-video-optimizer --folder [folder to watch] +bash-folders-video-optimizer --folder [folder to watch] ``` Example. ``` sh mkdir optimizer -dynamic-folders-video-optimizer --folder optimizer +bash-folders-video-optimizer --folder optimizer ``` And leave a video that you want to optimize in the folder `optimizer`. @@ -49,7 +54,7 @@ And leave a video that you want to optimize in the folder `optimizer`. #### Option 1: Service -Create a file in `/etc/systemd/system/dynamic-folders-video-optimizer.service` with the following content. +Create a file in `/etc/systemd/system/bash-folders-video-optimizer.service` with the following content. ```ini @@ -60,7 +65,7 @@ Description=Folder that watches when new videos are added and optimizes them. Restart=always RestartSec=5 User=[user] -ExecStart=dynamic-folders-video-optimizer --folder [folder to watch] +ExecStart=bash-folders-video-optimizer --folder [folder to watch] [Install] WantedBy=multi-user.target @@ -77,8 +82,8 @@ sudo systemctl daemon-reload And activate it. ``` sh -sudo systemctl enable dynamic-folders-video-optimizer -sudo systemctl start dynamic-folders-video-optimizer +sudo systemctl enable bash-folders-video-optimizer +sudo systemctl start bash-folders-video-optimizer ``` #### Option 2: Cron @@ -92,9 +97,11 @@ crontab -e Add to document. ``` sh -@reboot dynamic-folders-video-optimizer --folder [folder to watch] >/dev/null 2>&1 & +@reboot bash-folders-video-optimizer --folder [folder to watch] >/dev/null 2>&1 & ``` +--- + ## Development ### Check syntax diff --git a/bash-folder-decompress.sh b/bash-folder-decompress.sh new file mode 100644 index 0000000..151d9d4 --- /dev/null +++ b/bash-folder-decompress.sh @@ -0,0 +1,131 @@ +#!/usr/bin/env bash + +# -- +# Description: Script that watches when new videos are added to a folder and optimizes them. +# -- +# Requirements: Install inotify-tools and ffmpeg +# Example Debian: $sudo apt install inotify-tools ffmpeg +# -- +# Cron: @reboot dynamic-folders-video-optimizer.sh >/dev/null 2>&1 & +# -- + +# START +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=("mkv" "mp4" "avi" "mov") + +# FUNCTIONS + +usage() { + if [ "$*" != "" ] ; then + echo "Error: $*" + fi + + cat << EOF +Usage: $PROGNAME [OPTION] +Watches when new videos are added to a folder and optimizes them. +Options: +--folder [path] Folder path where new video will be monitored and optimized +--help Display this usage message and exit +EOF + + 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() { + # 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 + # Check if the file name starts with "optimized" + if [[ "$filename" != optimized* ]]; then + # Notifies that the conversion is to be started + send-notification "Optimizing $filename ..." + # Convert the file to MP4 format using ffmpeg + + # sudo apt-get install gzip bzip2 xz-utils unzip p7zip-full unrar + +filename="$1" +filetype=$(file -b "$filename" | awk '{print $1}') + +case "$filetype" in + "gzip") + gzip -d "$filename" + ;; + "bzip2") + bzip2 -d "$filename" + ;; + "XZ") + xz -d "$filename" + ;; + "Zip") + unzip "$filename" + ;; + "7-zip") + 7z x "$filename" + ;; + "RAR") + unrar x "$filename" + ;; + *) + echo "Tipo de archivo desconocido: $filetype" + exit 1 + ;; +esac + + + + # Notifies that it has been terminated + send-notification "Completed! Output: optimized_${filename%.*}.mp4" + fi + fi + done + done +} + +# CONTROLE ARGUMENTS +isArg="" + +while [ $# -gt 0 ] ; do + case "$1" in + --help) + usage + ;; + --folder) + isArg="1" + if [ $# -eq 2 ]; then + start + else + usage "You need to specify the path of the folder to watch." + fi + ;; + *) + esac + shift +done + +if [ -z $isArg ] ; then + usage "Not enough arguments" +fi diff --git a/dynamic-folders-decompress.sh b/bash-folders-decompress.sh similarity index 100% rename from dynamic-folders-decompress.sh rename to bash-folders-decompress.sh diff --git a/dynamic-folders-image-to-webp.sh b/bash-folders-image-to-webp.sh similarity index 100% rename from dynamic-folders-image-to-webp.sh rename to bash-folders-image-to-webp.sh diff --git a/dynamic-folders-random-image.sh b/bash-folders-random-image.sh similarity index 100% rename from dynamic-folders-random-image.sh rename to bash-folders-random-image.sh diff --git a/dynamic-folders-share-files.sh b/bash-folders-share-files.sh similarity index 100% rename from dynamic-folders-share-files.sh rename to bash-folders-share-files.sh diff --git a/dynamic-folders-video-optimizer.sh b/bash-folders-video-optimizer.sh similarity index 100% rename from dynamic-folders-video-optimizer.sh rename to bash-folders-video-optimizer.sh