-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcac_all.sh
executable file
·180 lines (158 loc) · 3.87 KB
/
cac_all.sh
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/bin/bash
CAC=$(which cac)
if [[ "x" = "x${CAC}" ]]; then
echo "Unable to find cac!"
exit 42
fi
# Let's make sure this is our cac...
if [[ ! "$(${CAC} --version 2>&1)" =~ 'EWX cac V' ]]; then
echo "Wrong cac!"
echo -n "Got: "
${CAC} --version 2>&1
echo "Expected: EWX cac"
exit 43
fi
SHORT="a:,h,p:,s,t:,T:,U"
LONG="archive:,help,prefix:,splitaudio,target:,tempdir:,upgrade"
OPTS=$(getopt --name cac_all --options $SHORT --longoptions $LONG -- "$@")
eval set -- "$OPTS"
xPREFIX=""
xTARGET=""
xARCHIVE=""
xSHOWUSAGE=""
xSPLITAUDIO=""
xDOUPGRADE=""
xTEMPDIR=""
xEXIT=0
xHaveArchive="no"
while :
do
case "$1" in
-a | --archive )
xARCHIVE="$2"
xHaveArchive="yes"
shift 2
;;
-h | --help )
xSHOWUSAGE="yes"
shift 1
;;
-p | --prefix )
xPREFIX="$2"
shift 2
;;
-s | --splitaudio)
xSPLITAUDIO="--splitaudio"
shift 1
;;
-t | --target )
xTARGET="$2"
shift 2
;;
-T | --tempdir )
xTEMPDIR="--tempdir $2"
shift 2
;;
-U | --upgrade )
xDOUPGRADE="--upgrade"
shift 1
;;
--)
shift;
break
;;
*)
echo "Unexpected option \"$1\" encountered"
xSHOWUSAGE="yes"
xEXIT=1
shift 1
;;
esac
done
if [[ -z "${xPREFIX}" && "yes" != "${xSHOWUSAGE}" ]]; then
echo "ERROR: Prefix was not set!"
xSHOWUSAGE="yes"
xEXIT=2
fi
if [[ -z "${xTARGET}" && "yes" != "${xSHOWUSAGE}" ]]; then
echo "ERROR: Target was not set!"
xSHOWUSAGE="yes"
xEXIT=3
fi
if [[ "yes" = "${xSHOWUSAGE}" ]]; then
echo "Usage: $0 <-h|--help>"
echo "Usage: $0 [OPTIONS] <-p|--prefix prefix> <-t|--target target> [-a|--archive archive]"
echo
echo "[c]leanup [a]nd [c]onvert all <prefix>*.[avi|mkv|mp4|mpg|mpeg|webm] into <target> and"
echo "move all that succeeded to [archive] if set"
echo
echo "Checks <target> for existence of each video and works with lock files to ensure"
echo "to not produce any double conversions."
echo
echo "OPTIONS:"
echo " -a --archive <archive> : Videos are moved there after processing"
echo " -h --help : Show this help and exit."
echo " -s --splitaudio : Split the second channel, if it exists, into its own wav"
echo " -T --tempdir <path> : Declare an alternative temporary directory for the processing"
echo " -U --upgrade : Force 60 FPS when 30 FPS would be the target (source < 50 FPS)"
exit ${xEXIT}
fi
if [[ ! -d "${xTARGET}" ]]; then
mkdir -p "${xTARGET}"
res=$?
if [[ 0 -ne $res ]]; then
echo "ERROR: mkdir \"${xTARGET}\" failed: $res"
exit $res
fi
fi
if [[ "yes" = "${xHaveArchive}" && ! -d "${xARCHIVE}" ]]; then
mkdir -p "${xARCHIVE}"
res=$?
if [[ 0 -ne $res ]]; then
echo "ERROR: mkdir \"${xARCHIVE}\" failed: $res"
exit $res
fi
fi
declare -a xInputFiles=()
mapfile -t xInputFiles < <( \
find ./ -mindepth 1 -maxdepth 1 \( \
-name "${xPREFIX}*.mkv" -or \
-name "${xPREFIX}*.mp4" -or \
-name "${xPREFIX}*.mpg" -or \
-name "${xPREFIX}*.mpeg" -or \
-name "${xPREFIX}*.webm" -or \
-name "${xPREFIX}*.avi" \
\) -printf "%f\n" 2>/dev/null | sort -h )
for mkv in "${xInputFiles[@]}"; do
if [[ -z "$mkv" ]]; then
continue 1
fi
dest="${xTARGET}/${mkv%.*}.mkv"
lock="${xTARGET}/${mkv%.*}.lck"
hasl=0
if [[ ! -f "${dest}" ]]; then
lockfile -r0 "$lock" 1>/dev/null 2>&1 && hasl=1
fi
if [[ $hasl -eq 1 ]]; then
${CAC} --input "${mkv}" --output "${dest}" ${xSPLITAUDIO} ${xDOUPGRADE} ${xTEMPDIR}
res=$?
rm -f "${lock}"
if [[ 0 -eq $res ]]; then
if [[ "yes" = "${xHaveArchive}" ]]; then
echo -n "Moving \"$mkv\" ..."
mv "${mkv}" "${xARCHIVE}/"
echo " done"
else
echo "Not moving \"$mkv\" as no archive was set"
fi
elif [[ 42 -eq $res ]]; then
# User interruption via CTRL+C (SIGINT) or SIGTERM
echo "User interruption caught. Exiting..."
exit $res
elif [[ 255 -ne $res ]]; then
echo "${CAC} FAILED: $res"
fi
else
echo "${dest} already in progress. Skipping..."
fi
done