I want to show my focus on execute commands in Bash using watchers and folders. I have named this trick Bash Folders.
I have a problem. I needed to run a lot of commands on files. For example, every day I need to transform videos (the extension isn’t relevant) to MP4. Ok.
Classic steps to follow:
- Open the terminal.
- Change the directory.
- Run
ffmpeg
(At the reality, I used an alias). - Wait for it to finish.
- Move output to the target folder.
Sound familiar? Now, you think repeating all steps hundreds of times a day. It’s crazy!
My solution
It’s easy. If a new video is copied in a specific folder, then run a command using this file.
inotifywait
is a unix tool that checks for updates in folders. The following code runs ffmpeg
if a new file is created or moved in my-folder-to-watch
.
inotifywait -m -e create,moved_to --format '%f' my-folder-to-watch |
while read -r filename; do
ffmpeg -i $filename ...
done
Do you want to filtering files by extension? Use --includei
flag.
EXTENSIONS_TO_WATCH="mkv|mp4|avi|mov"
inotifywait -m -e create,moved_to my-folder-to-watch \
--includei "\.($EXTENSIONS_TO_WATCH)$" --format '%f' |
while read -r filename; do
ffmpeg -i $filename ...
done
If you’re looking for a code that is more flexible, but more verbose, you can use a loop with a conditional statement to check the extension.
EXTENSIONS_TO_WATCH=("mkv" "mp4" "avi" "mov")
inotifywait -m -e create,moved_to my-folder-to-watch --format '%f' |
while read -r filename; do
extension="${filename##*.}"
for ext in "${EXTENSIONS_TO_WATCH[@]}"; do
if [[ "$ext" = "$extension" ]]; then
ffmpeg -i $filename ...
fi
done
done
Now my steps are:
- Copy, or move, file to specific folder.
- Wait for it to finish.
- Move the output to the target folder.
But… How do I know if ffmpeg
is finished? You can achieve this by sending desktop notifications (notify-send
) or by creating a temporal plain file (optimizing_please_wait
).
Example
This script implements everything we discussed.
Create a file, possibly named bash-folders-video-optimizer
, and copy the following contents:
#!/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 ffmpeg
# --
# Cron: @reboot bash-folders-video-optimizer.sh >/dev/null 2>&1 &
# --
# START
set -e
# VARIABLES
PROGNAME=$(basename "$0")
FOLDER_ORIGIN="$2"
EXTENSIONS_TO_WATCH=("mkv" "mp4" "avi" "mov")
MESSAGE_WAITING="optimizing_please_wait"
# 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
}
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
filename_output="optimized_${filename%.*}.mp4"
# Displays a flat file of information
touch "$FOLDER_ORIGIN/$MESSAGE_WAITING"
# Convert the file to MP4 format using ffmpeg in /tmp/
ffmpeg -i "$FOLDER_ORIGIN/$filename" -c:v libx264 -tune stillimage -c:a aac -b:a 192k -pix_fmt yuv420p -nostdin -shortest "/tmp/$filename_output"
# When finished move the optimized file
mv "/tmp/$filename_output" "$FOLDER_ORIGIN/$filename_output"
# Remove a flat file of information
rm "$FOLDER_ORIGIN/$MESSAGE_WAITING"
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
Now run:
bash-folders-video-optimizer --folder [folder to watch]
Persistence
Do you want execute the after script every boot your computer?
Open your cron:
crontab -e
And insert this line:
@reboot bash-folders-video-optimizer --folder [folder to watch] >/dev/null 2>&1 &
Conclusion
I hope this trick helps you to optimize your time. If you want to see more examples, I recommend you to visit the repository Bash folders: images optimizer, battery hooks, etc. I will be happy if you contribute with your own scripts.
See you in the next post!
{{ comments.length }} comentarios