From 873838d0125e205615d4a6334fef6c00c8ca46fd Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Tue, 4 Apr 2023 09:13:39 +0200 Subject: [PATCH] Finish first script --- README.md | 16 +++++++ dynamic-video-to-mp4.sh | 98 +++++++++++++++++++++++++++++++++++++++++ video-to-mp4.sh | 29 ------------ 3 files changed, 114 insertions(+), 29 deletions(-) create mode 100755 dynamic-video-to-mp4.sh delete mode 100644 video-to-mp4.sh diff --git a/README.md b/README.md index c25b618..e3dd4f8 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,19 @@ # Dynamic folders Collection of Bash scripts that execute functionalities in folders. + +## Video to mp4 + +### Install + +### Run + +### Service + +## Development + +### Check syntax + +```sh +shellcheck [script] +``` diff --git a/dynamic-video-to-mp4.sh b/dynamic-video-to-mp4.sh new file mode 100755 index 0000000..07eb30b --- /dev/null +++ b/dynamic-video-to-mp4.sh @@ -0,0 +1,98 @@ +#!/usr/bin/env bash + +# -- +# Description: Script that watches when new videos are added to a folder and optimizes them to mp4 for Web. +# -- +# Requirements: Install inotify-tools and ffmpeg +# Example Debian: $sudo apt install inotify-tools ffmpeg +# -- +# Activate a service +# Cron: @reboot dynamic-video-to-mp4.sh >/dev/null 2>&1 & +# or create a service: +# $sudo nano /etc/systemd/system/dynamic-video-to-mp4.service +# -- + +# START +set -e + +# 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 to mp4 for Web. +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 --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 + ffmpeg -i "$FOLDER_ORIGIN/$filename" -c:v libx264 -tune stillimage -c:a aac -b:a 192k -pix_fmt yuv420p -nostdin -shortest "$FOLDER_ORIGIN/optimized_${filename%.*}.mp4" + # 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/video-to-mp4.sh b/video-to-mp4.sh deleted file mode 100644 index ae7fcf2..0000000 --- a/video-to-mp4.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/bash - -# Requirements: sudo apt install inotify-tools ffmpeg -# Enable, cron: @reboot /path/script.sh >/dev/null 2>&1 & -# Carpeta de destino -WATCH_FOLDER=$argv[1] - -# Extensiones a monitorear -EXTENSIONS=("mkv" "mp4" "avi" "mov") - -# Configura inotify para monitorear la carpeta de destino -inotifywait -m -e create --format '%f' "$WATCH_FOLDER" | -while read filename; do - # Obtiene la extensión del archivo - extension="${filename##*.}" - - # Verifica si la extensión está en la lista de extensiones - if [[ " ${EXTENSIONS[@]} " =~ " ${extension} " ]]; then - # Verifica si el nombre del archivo comienza con "optimized" - if [[ "$filename" != optimized* ]]; then - notify-send "Archivo detectado: $filename. Iniciando conversión..." - # Convierte el archivo a formato MP4 - ffmpeg -i "$WATCH_FOLDER/$filename" -c:v libx264 -tune stillimage -c:a aac -b:a 192k -pix_fmt yuv420p -shortest "$WATCH_FOLDER/optimized_${filename%.*}.mp4" - notify-send "Archivo convertido: optimized_${filename%.*}.mp4" - else - echo "Archivo ignorado: $filename" - fi - fi -done