entr
is a "utility for running arbitrary commands when files change", it doesn't specifically monitor for new files (it can pass one new file to the command, but I couldn't get this to work reliably). You can work around this (as you did) by having entr
monitor the directory itself, but you still need to take care of identifying which files actually are new and avoid re-processing.
cd ~/Downloads
mkdir -p videos
while :; do
ls . | entr -dz find . -depth 1 -type f -iname "*.webm" -exec sh -c '
f="$1"
mv -- "$f" videos/
handbrakecli --encoder x264 --width 700 --format av_mp4 --optimize --non-anamorphic --rate 30 -i "videos/$f" -o "videos/${f%.webm}.mp4"
# rm "videos/$f"
' _ {} \;
done
This uses find
to identify new WebM files, moves them to a different directory (to avoid reprocessing) and runs Handbrake on them. Uncomment the last line to automatically remove WebM files after processing. To terminate, press Ctrl-Z and then run kill -9 %1
.
PS: As @gilby already mentioned in the comments: Get Hazel, works like a charm.
PPS: Also, the answers on Monitor a folder for changes, and run a command when a change is detected provide some additional ways to trigger shell scripts after the content of a directory changes.