aboutsummaryrefslogtreecommitdiffstats
path: root/ffmpeg/resize-and-transcode-recursively
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2024-05-06 07:38:20 +0200
committerMattias Andrée <maandree@kth.se>2024-05-06 07:38:20 +0200
commitd7a43337a05b086533430d1a749fd177439ced66 (patch)
treec9340132c241b5cb630472790762c18fad521d9c /ffmpeg/resize-and-transcode-recursively
parentmisc updates (diff)
downloaddotfiles-d7a43337a05b086533430d1a749fd177439ced66.tar.gz
dotfiles-d7a43337a05b086533430d1a749fd177439ced66.tar.bz2
dotfiles-d7a43337a05b086533430d1a749fd177439ced66.tar.xz
m
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rwxr-xr-xffmpeg/resize-and-transcode-recursively65
1 files changed, 65 insertions, 0 deletions
diff --git a/ffmpeg/resize-and-transcode-recursively b/ffmpeg/resize-and-transcode-recursively
new file mode 100755
index 0000000..8a7a207
--- /dev/null
+++ b/ffmpeg/resize-and-transcode-recursively
@@ -0,0 +1,65 @@
+#!/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 '\x1b['"${colour}m${format}"'\x1b[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" "$@"