blob: c420de32c741c993d8403e16133681d8dd31963f (
plain) (
tree)
|
|
#!/bin/sh
#### rmdir test rm mv mkdir printf sed grep rev cut stat(-c "%s") ffprobe ffmpeg
set -u
if test $# -lt 2 || test $# -gt 3 || test -z "$1" || test -z "$2"; then
printf "usage: %s input-dir output-dir [max-min-dim]\n" "$0" >&2
exit 1
fi
err () {
colour="$1"
format="$2"
shift 2
printf '\033['"${colour}m${format}"'\033[m\n' "$@" >&2
}
alert () { err "1;31" "$@"; }
header () { err "1;34" "$@"; }
subheader () { err "34" "$@"; }
red () { err "31" "$@"; }
green () { err "32" "$@"; }
yellow () { err "33" "$@"; }
magenta () { err "35" "$@"; }
cyan () { err "36" "$@"; }
input="$(printf '%s\n' "$1" | sed '$s/\/*$/\//')"
output="$(printf '%s\n' "$2" | sed '$s/\/*$/\//')"
shift 2
process() {
d="$1"
shift 1
for f in "$d"*; do
if test -L "$f"; then
continue
elif test -f "$f"; then
if printf '%s\n' "$f" | sed -n '$p' | grep '\.tmp\.mkv$' >/dev/null; then
alert 'Old temporary file found: %s' "$f"
continue
fi
file="${f#$input}"
keep="$output$file"
tmp="$(printf '%s\n' "$f" | rev | cut -d . -f 2- | rev).tmp.mkv"
out="$(printf '%s\n' "$keep" | rev | cut -d . -f 2- | rev).mkv"
mkdir -p -- "$(dirname -- "$out")"
resize-and-transcode "$f" "$tmp" "$@"
r=$?
if test $r = 0; then
if ! mv -- "$tmp" "$out" ; then
alert 'Failed to move %s to %s' "$tmp" "$out"
elif ! rm -- "$f"; then
alert 'Failed to remove old file: %s' "$f"
fi
elif test $r = 1; then
mv -- "$f" "$keep"
fi
elif test -d "$f"; then
process "$f/" "$@"
fi
done
rmdir -- "$d" 2>/dev/null || :
}
process "$input" "$@"
|