aboutsummaryrefslogtreecommitdiffstats
path: root/ffmpeg/resize-and-transcode-recursively
blob: c420de32c741c993d8403e16133681d8dd31963f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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 '\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" "$@"