-
Notifications
You must be signed in to change notification settings - Fork 2
/
ff_flip.sh
executable file
·270 lines (204 loc) · 9.79 KB
/
ff_flip.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
#!/bin/bash
# ╭──────────────────────────────────────────────────────────────────────────────╮
# │ │
# │ Flip video horizontally or vertically │
# │ │
# ╰──────────────────────────────────────────────────────────────────────────────╯
# ╭──────────────────────────────────────────────────────────╮
# │ Set Defaults │
# ╰──────────────────────────────────────────────────────────╯
# set -o errexit # If a command fails bash exits.
# set -o pipefail # pipeline fails on one command.
if [[ "${DEBUG-0}" == "1" ]]; then set -o xtrace; fi # DEBUG=1 will show debugging.
# ╭──────────────────────────────────────────────────────────╮
# │ VARIABLES │
# ╰──────────────────────────────────────────────────────────╯
INPUT_FILENAME="input.mp4"
OUTPUT_FILENAME="ff_flip.mp4"
HORIZONTAL=""
VERTICAL=""
LOGLEVEL="error"
GREP=""
function stylesheet()
{
TEXT_GREEN_400="\e[38;2;74;222;128m"
TEXT_ORANGE_500="\e[38;2;249;115;22m"
TEXT_RED_400="\e[38;2;248;113;113m"
TEXT_BLUE_600="\e[38;2;37;99;235m"
TEXT_YELLOW_500="\e[38;2;234;179;8m"
TEXT_PURPLE_500="\e[38;2;168;85;247m"
TEXT_RESET="\e[39m"
}
stylesheet
# ╭──────────────────────────────────────────────────────────╮
# │ Usage. │
# ╰──────────────────────────────────────────────────────────╯
usage()
{
if [ "$#" -lt 2 ]; then
printf "ℹ️ Usage:\n $0 -i <INPUT_FILE> [-v] [-h] [-o <OUTPUT_FILE>] [-l loglevel]\n\n" >&2
printf "Summary:\n"
printf "Flip video horizontally and/or vertically.\n\n"
printf "Flags:\n"
printf " -i | --input <INPUT_FILE>\n"
printf "\tThe name of an input file.\n\n"
printf " -o | --output <OUTPUT_FILE>\n"
printf "\tDefault is %s\n" "${OUTPUT_FILENAME}"
printf "\tThe name of the output file.\n\n"
printf " -h | --horizontal\n"
printf "\tFlip video horizontally.\n\n"
printf " -v | --vertical\n"
printf "\tFlip video vertically.\n\n"
printf " -g | --grep <STRING>\n"
printf "\tSupply a grep string for filtering the inputs if a folder is specified.\n\n"
printf " -C | --config <CONFIG_FILE>\n"
printf "\tSupply a config.json file with settings instead of command-line. Requires JQ installed.\n\n"
printf " -l | --loglevel <LOGLEVEL>\n"
printf "\tThe FFMPEG loglevel to use. Default is 'error' only.\n"
printf "\tOptions: quiet,panic,fatal,error,warning,info,verbose,debug,trace\n\n"
exit 1
fi
}
# ╭──────────────────────────────────────────────────────────╮
# │ Take the arguments from the command line │
# ╰──────────────────────────────────────────────────────────╯
function arguments()
{
POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
-i|--input)
INPUT_FILENAME=$(realpath "$2")
shift
shift
;;
-o|--output)
OUTPUT_FILENAME="$2"
shift
shift
;;
-h|--horizontal)
HORIZONTAL=" -vf hflip"
shift
;;
-v|--vertical)
VERTICAL=" -vf vflip"
shift
;;
-l|--loglevel)
LOGLEVEL="$2"
shift
shift
;;
-g|--grep)
GREP="$2"
shift
shift
;;
-C|--config)
CONFIG_FILE="$2"
shift
shift
;;
--description) # IGNORED. used for descriptions in JSON
shift
shift
;;
-*|--*)
echo "Unknown option $1"
exit 1
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg back onto variable
shift # remove argument and shift past it.
;;
esac
done
}
# ╭──────────────────────────────────────────────────────────╮
# │ Read config-file if supplied. Requires JQ │
# ╰──────────────────────────────────────────────────────────╯
function read_config()
{
# Check if config has been set.
if [ -z ${CONFIG_FILE+x} ]; then return 0; fi
# Check dependencies
if ! command -v jq &> /dev/null; then
printf "JQ is a dependency and could not be found. Please install JQ for JSON parsing. Exiting.\n"
exit
fi
# Read file
LIST_OF_INPUTS=$(cat ${CONFIG_FILE} | jq -r 'to_entries[] | ["--" + .key, .value] | @sh' | xargs)
# Sen to the arguments function again to override.
arguments $LIST_OF_INPUTS
}
# ╭──────────────────────────────────────────────────────────╮
# │ Exit the app by just skipping the ffmpeg processing. │
# │ Then copy the input to the output. │
# ╰──────────────────────────────────────────────────────────╯
function exit_gracefully()
{
cp -f ${INPUT_FILENAME} ${OUTPUT_FILENAME}
exit 0
}
# ╭──────────────────────────────────────────────────────────╮
# │ Run these checks before you run the main script │
# ╰──────────────────────────────────────────────────────────╯
function pre_flight_checks()
{
INPUT_FILE=$1
# Check input filename has been set.
if [[ -z "${INPUT_FILE+x}" ]]; then
printf "\t❌ No input file specified. Exiting.\n"
exit_gracefully
fi
# Check input file exists.
if [ ! -f "$INPUT_FILE" ]; then
printf "\t❌ Input file not found. Exiting.\n"
exit_gracefully
fi
# Check input filename is a movie file.
if ffprobe "${INPUT_FILE}" > /dev/null 2>&1; then
printf ""
else
printf "\t❌ Input file: '%s' not a movie file. Exiting.\n" "${INPUT_FILE}"
ffprobe "${INPUT_FILE}"
exit_gracefully
fi
}
function print_flags()
{
printf "👯 ${TEXT_GREEN_400}%-10s :${TEXT_RESET} %s\n" "Horizontal" "$HORIZONTAL"
printf "🙃 ${TEXT_GREEN_400}%-10s :${TEXT_RESET} %s\n" "Vertical" "$VERTICAL"
}
# ╭──────────────────────────────────────────────────────────╮
# │ │
# │ Main Function │
# │ │
# ╰──────────────────────────────────────────────────────────╯
function main()
{
print_flags
# If this is a file
if [ -f "$INPUT_FILENAME" ]; then
pre_flight_checks $INPUT_FILENAME
ffmpeg -v ${LOGLEVEL} -i ${INPUT_FILENAME} ${HORIZONTAL} ${VERTICAL} ${OUTPUT_FILENAME}
printf "✅ ${TEXT_PURPLE_500}%-10s :${TEXT_RESET} %s\n" "Output" "$OUTPUT_FILENAME"
fi
# If this is a drectory
if [ -d "$INPUT_FILENAME" ]; then
LOOP=0
LIST_OF_FILES=$(find $INPUT_FILENAME -maxdepth 1 \( -iname '*.mp4' -o -iname '*.mov' \) | grep "$GREP")
for INPUT_FILENAME in $LIST_OF_FILES
do
pre_flight_checks $INPUT_FILENAME
ffmpeg -v ${LOGLEVEL} -i ${INPUT_FILENAME} ${HORIZONTAL} ${VERTICAL} ${LOOP}_${OUTPUT_FILENAME}
printf "✅ ${TEXT_PURPLE_500}%-10s :${TEXT_RESET} %s\n" "Output" "${LOOP}_${OUTPUT_FILENAME}"
LOOP=$(expr $LOOP + 1)
done
fi
}
usage $@
arguments $@
read_config "$@"
main $@