mirror of
https://github.com/tanrax/bash-folders.git
synced 2025-10-10 18:25:50 +02:00
Moved everything into funcitons for scoping; push optimized videos to an "optimized" subfolder
This commit is contained in:
@@ -1,64 +1,81 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -e
|
||||
|
||||
FOLDER_ORIGIN="$2"
|
||||
EXTENSIONS_TO_WATCH=("mkv" "mp4" "avi" "mov")
|
||||
MESSAGE_WAITING="optimizing_please_wait"
|
||||
|
||||
usage() {
|
||||
[[ -n "${*}" ]] && printf '%s\n' "Error: ${*}" >&2
|
||||
|
||||
cat << EOF
|
||||
USAGE: ${0##*/} [OPTION] --folder PATH
|
||||
USAGE: ${0##*/} [OPTION] PATH
|
||||
|
||||
Watches when new videos are added to a folder and optimizes them.
|
||||
|
||||
OPTIONS:
|
||||
--help Display this usage message and exit
|
||||
--folder PATH Folder path where new video will be monitored and optimized
|
||||
EOF
|
||||
|
||||
exit 1
|
||||
}
|
||||
|
||||
start() {
|
||||
inotifywait -m -e create,moved_to --format '%f' "$FOLDER_ORIGIN" | while read -r filename; do
|
||||
extension="${filename##*.}"
|
||||
for ext in "${EXTENSIONS_TO_WATCH[@]}"; do
|
||||
if [[ "$ext" = "$extension" ]]; then
|
||||
if [[ "$filename" != optimized* ]]; then
|
||||
filename_output="optimized_${filename%.*}.mp4"
|
||||
touch "$FOLDER_ORIGIN/$MESSAGE_WAITING"
|
||||
ffmpeg -i "$FOLDER_ORIGIN/$filename" -c:v libx264 -tune stillimage -c:a aac -b:a 192k -pix_fmt yuv420p -nostdin -shortest "/tmp/$filename_output"
|
||||
mv "/tmp/$filename_output" "$FOLDER_ORIGIN/$filename_output"
|
||||
rm "$FOLDER_ORIGIN/$MESSAGE_WAITING"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
done
|
||||
optimize() {
|
||||
touch -a "${2}"
|
||||
ffmpeg \
|
||||
-i "${1}" \
|
||||
-c:v "libx264" \
|
||||
-tune stillimage \
|
||||
-c:a "aac" \
|
||||
-b:a "192k" \
|
||||
-pix_fmt "yuv420p" \
|
||||
-nostdin \
|
||||
-shortest \
|
||||
"${2}"
|
||||
}
|
||||
|
||||
isArg=""
|
||||
run() {
|
||||
local file extension optimized
|
||||
optimized="${1}/optimized"
|
||||
|
||||
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."
|
||||
set -e
|
||||
|
||||
mkdir --parents "${optimized}"
|
||||
|
||||
while read -r file; do
|
||||
extension="${file##*.}"
|
||||
[[ "${extension,,}" =~ foo ]] || continue
|
||||
|
||||
printf '%s\n' "Optimizing file '${file}'..."
|
||||
|
||||
if ! optimize "${file}" "${optimized}/${file%.*}.mp4"; then
|
||||
printf '%s\n' "Failed to optimize file: '${file}'" >&2
|
||||
fi
|
||||
;;
|
||||
done < <(inotifywait --monitor --event "create" --event "moved_to" --format '%f' "${1}")
|
||||
}
|
||||
|
||||
main() {
|
||||
local opts
|
||||
|
||||
opts="$(getopt \
|
||||
--options h \
|
||||
--longoptions help \
|
||||
--name "${0##*/}" \
|
||||
-- "${@}" \
|
||||
)"
|
||||
|
||||
eval set -- "${opts}"
|
||||
while true; do
|
||||
case "${1}" in
|
||||
-h | --help ) usage; return 0;;
|
||||
-- ) shift; break;;
|
||||
* )
|
||||
printf '%s\n' "Unknown option: '${1}'" >&2
|
||||
usage
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
|
||||
if [[ -z "${isArg}" ]]; then
|
||||
usage "Not enough arguments"
|
||||
if [[ -z "${1}" ]]; then
|
||||
printf '%s\n' "No folder specified" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
mkdir --parents "${1}"
|
||||
run "${1}"
|
||||
}
|
||||
|
||||
main "${@}"
|
||||
|
Reference in New Issue
Block a user