-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.sh
executable file
·304 lines (220 loc) · 7.11 KB
/
process.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env bash
set -o nounset
# https://community.gopro.com/s/article/GoPro-Camera-File-Naming-Convention?language=en_US
#
# nullglob - If set, Bash allows filename patterns which
# match no files to expand to a null string,
# rather than themselves.
#
shopt -s nullglob
usage() {
cat << EOT 1>&2
Usage: process.sh [-h] [-d] [-f] [-n] [-p pfx] [-t type] -s w:h -o fn { -i fn | video ... }
OPTIONS
=======
-d output debug information
-f overwrite output file if it already exists
-h show help
-n don't wait for upload to YouTube
-i fn input file to use when calling ffmpeg
-o fn output combined file to fn
-p pfx use pfx as prefix for file names (default: GX)
-s w:h scale video to w width and h height pixels
-t type use type as extension for file names (default: MP4)
ARGUMENTS
=========
video ... number of one or more videos to look for
EXAMPLES
========
# build ffmpeg input file from video IDs 10, 20, and 30
$ process.sh -d -s 2560:1440 -o output.mp4 10 20 30
# read ffmpeg input file from file instead of building via command line
$ process.sh -d -s 2560:1440 -i input.txt -o output.mp4
EOT
exit
}
initGlobals() {
declare -gA GLOBALS=(
[CUSTOM_INPUT_FILE]='' # -i
[DEBUG]='false' # -d
[FILE_PREFIX]='' # -p
[FILE_TYPE]='' # -t
[INPUT_FILE]='' # -i
[NO_WAIT]='false' # -n
[OUTPUT_FILE]='' # -o
[OVERWRITE_OPTION]='' # -f
[SCALING]='' # -s
[VIDEO_IDS]='' # video ...
)
}
debug() {
if [[ ${GLOBALS[DEBUG]} == 'true' ]]; then
echo "$@"
fi
}
processOptions() {
[[ $# -eq 0 ]] && usage
while getopts ":hdfni:o:p:s:t:" FLAG; do
case "${FLAG}" in
d)
GLOBALS[DEBUG]='true'
debug "Debug mode turned on."
;;
f)
GLOBALS[OVERWRITE_OPTION]='-y'
debug "Force overwrite mode turned on."
;;
i)
GLOBALS[INPUT_FILE]=${OPTARG}
debug "Input file set to '${GLOBALS[INPUT_FILE]}'."
if [[ -s ${GLOBALS[INPUT_FILE]} ]]; then
GLOBALS[CUSTOM_INPUT_FILE]='true'
debug "Custom input file mode turned on."
fi
;;
n)
GLOBALS[NO_WAIT]='true'
debug "No wait mode turned on."
;;
o)
GLOBALS[OUTPUT_FILE]=${OPTARG}
debug "Output file set to '${GLOBALS[OUTPUT_FILE]}'."
;;
p)
GLOBALS[FILE_PREFIX]=${OPTARG}
debug "File prefix set to '${GLOBALS[FILE_PREFIX]}'."
;;
s)
GLOBALS[SCALING]=${OPTARG}
if [[ ${GLOBALS[SCALING]} =~ [0-9]+:[0-9]+ ]]; then
debug "Scaling set to '${GLOBALS[SCALING]}'."
else
debug "Invalid scaling: '${GLOBALS[SCALING]}'."
usage
fi
;;
t)
GLOBALS[FILE_TYPE]=${OPTARG}
debug "File type set to '${GLOBALS[FILE_TYPE]}'."
;;
h | *)
usage
;;
esac
done
shift $(( OPTIND - 1 ))
GLOBALS[VIDEO_IDS]=$*
[[ $# -eq 0 ]] && usage
}
validateInputs() {
if [[ -z ${GLOBALS[SCALING]} ]] || [[ -z ${GLOBALS[OUTPUT_FILE]} ]]; then
debug "Missing scaling and/or output file name."
usage
fi
}
setDefaults() {
if [[ -z ${GLOBALS[OVERWRITE_OPTION]} ]]; then
GLOBALS[OVERWRITE_OPTION]='-n'
debug "Overwrite option set to default of '${GLOBALS[OVERWRITE_OPTION]}'."
fi
if [[ -z ${GLOBALS[FILE_PREFIX]} ]]; then
GLOBALS[FILE_PREFIX]='GX'
debug "File prefix set to default of '${GLOBALS[FILE_PREFIX]}'"
fi
if [[ -z ${GLOBALS[FILE_TYPE]} ]]; then
GLOBALS[FILE_TYPE]='MP4'
debug "File type set to default of '${GLOBALS[FILE_TYPE]}'."
fi
}
checkForDependency() {
debug "Checking for dependency '$1'."
if ! command -v "$1" &> /dev/null; then
echo "Dependency '$1' is missing." > /dev/stderr
exit
fi
}
dependencyCheck() {
local DEPENDENCY
for DEPENDENCY in caffeinate cat ffmpeg mktemp realpath; do
checkForDependency "${DEPENDENCY}"
done
}
cleanup() {
if [[ ${GLOBALS[CUSTOM_INPUT_FILE]} != 'true' ]]; then
debug "Deleting temp file '${GLOBALS[INPUT_FILE]}'."
rm "${GLOBALS[INPUT_FILE]}"
fi
}
performSetup() {
initGlobals
processOptions "$@"
validateInputs
setDefaults
dependencyCheck
}
buildInputFile() {
local VIDEO_ID
local FORMATTED_VIDEO_ID
local GOPRO_FILE
local FULL_FILE_NAME
GLOBALS[INPUT_FILE]=$(mktemp)
debug "Building input file '${GLOBALS[INPUT_FILE]}'."
# for each video ID listed on command line
for VIDEO_ID in ${GLOBALS[VIDEO_IDS]}; do
# format it as a four-digit number
printf -v FORMATTED_VIDEO_ID '%04d' "${VIDEO_ID}"
debug "Checking video ID '${FORMATTED_VIDEO_ID}'."
# if any files exist for formatted video ID
for GOPRO_FILE in "${GLOBALS[FILE_PREFIX]}"??"${FORMATTED_VIDEO_ID}"."${GLOBALS[FILE_TYPE]}"; do
FULL_FILE_NAME=$(realpath "${GOPRO_FILE}")
debug "Adding file '${FULL_FILE_NAME}' to '${GLOBALS[INPUT_FILE]}'."
echo "file '${FULL_FILE_NAME}'" >> "${GLOBALS[INPUT_FILE]}"
done
done
}
showInputFile() {
if [[ ${GLOBALS[DEBUG]} == 'true' && -s ${GLOBALS[INPUT_FILE]} ]]; then
debug "=== Contents of '${GLOBALS[INPUT_FILE]}' ==="
cat "${GLOBALS[INPUT_FILE]}"
debug "=== End contents of '${GLOBALS[INPUT_FILE]}' ==="
fi
}
combineVideos() {
if [[ ! -s ${GLOBALS[INPUT_FILE]} ]]; then
echo "No files to process. Exiting." > /dev/stderr
cleanup
exit
fi
debug "Combining video into '${GLOBALS[OUTPUT_FILE]}'."
debug << EOT
Running FFmpeg (in: ${GLOBALS[INPUT_FILE]}, out: ${GLOBALS[OUTPUT_FILE]},
scaling: ${GLOBALS[SCALING]}, overwrite: ${GLOBALS[OVERWRITE_OPTION]}).
EOT
# https://ffmpeg.org/ffmpeg.html
# https://support.google.com/youtube/answer/6039860?hl=en
caffeinate ffmpeg \
-hide_banner \
"${GLOBALS[OVERWRITE_OPTION]}" \
-f concat \
-safe 0 \
-i "${GLOBALS[INPUT_FILE]}" \
-c:a flac \
-c:v h264 \
-filter:v scale=hd1080 \
"${GLOBALS[OUTPUT_FILE]}"
cleanup
echo "The videos have been combined into '${GLOBALS[OUTPUT_FILE]}'."
}
waitForUpload() {
if [[ ${GLOBALS[NO_WAIT]} != 'true' ]]; then
echo "Please upload the video to YouTube and then type CTRL+C once it has completed."
caffeinate
fi
}
performSetup "$@"
if [[ ${GLOBALS[CUSTOM_INPUT_FILE]} != 'true' ]]; then
buildInputFile "$@"
fi
showInputFile
combineVideos
waitForUpload