-
Notifications
You must be signed in to change notification settings - Fork 2
/
ff_download.sh
executable file
·306 lines (227 loc) · 11.2 KB
/
ff_download.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
305
306
#!/bin/bash
# ╭──────────────────────────────────────────────────────────────────────────────╮
# │ │
# │ Download a video or file to use in the scriptflow │
# │ │
# ╰──────────────────────────────────────────────────────────────────────────────╯
# ╭──────────────────────────────────────────────────────────╮
# │ 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_URL=""
OUTPUT_FILENAME="ff_download.mp4"
STRATEGY="1"
LOGLEVEL="error"
TMP_FILE="/tmp/tmp_ff_download_list"
FILELIST="./filelist.txt"
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_URL> [-s <STRATEGY>] [-o <OUTPUT_FILE>] [-l loglevel]\n\n" >&2
printf "Summary:\n"
printf "Download videos.\n\n"
printf "Flags:\n"
printf " -i | --input <INPUT_URL>\n"
printf "\tThe input url to download.\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 " -u | --urlsource <FILE_WITH_LIST>\n"
printf "\tA URL of a txt file with a list of all files to use as inputs. Separated one per line.\n\n"
printf " -s | --strategy <STRATEGY>\n"
printf "\t5\t A number. First 5 videos from inputs. Prefix number on output filename. Default 1.\n"
printf "\t~5\t Tilde(~) followed by a number. Random 5 videos from inputs. Prefix number on output filename.\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"
exit 1
fi
}
# ╭──────────────────────────────────────────────────────────╮
# │ Take the arguments from the command line │
# ╰──────────────────────────────────────────────────────────╯
function arguments()
{
POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
-i|--input|--input?|--input??|--input???)
write_to_temp "$2"
shift
shift
;;
-u|--urlsource)
URL_SOURCE="$2"
shift
shift
;;
-o|--output)
OUTPUT_FILENAME="$2"
shift
shift
;;
-s|--strategy)
STRATEGY="$2"
shift
shift
;;
-l|--loglevel)
LOGLEVEL="$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)
# Send to the arguments function again to override.
arguments $LIST_OF_INPUTS
}
# ╭───────────────────────────────────────────────────────────────────────────╮
# │ Reads the URL txt file and creates --inputs for each line │
# ╰───────────────────────────────────────────────────────────────────────────╯
function read_url_input_list()
{
# Check if a url source has been set.
if [ -z ${URL_SOURCE+x} ]; then return 0; fi
# download txt file
curl --insecure --silent --show-error --url "$URL_SOURCE" --output ${FILELIST} 2>/dev/null
# Check if file exists.
if [ ! -f ${FILELIST} ]; then return 0; fi
# Get the directory URL
DIR_NAME=$(dirname ${URL_SOURCE})
# append INPUTS to the LIST_OF_INPUTS
while read LINE; do
LIST_OF_INPUTS="${LIST_OF_INPUTS} --input ${DIR_NAME}/${LINE}"
done < ${FILELIST}
# Send to the arguments function again to override.
arguments $LIST_OF_INPUTS
}
# ╭──────────────────────────────────────────────────────────╮
# │ Write the absolute path into the temporary file │
# ╰──────────────────────────────────────────────────────────╯
function write_to_temp()
{
FILE=$1
# print line into temp file.
printf "%s\n" "${FILE}" >> ${TMP_FILE}
}
# ╭──────────────────────────────────────────────────────────╮
# │ 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
}
function configure_strategy()
{
INPUT_FILE=$1
# If its a number, take top X
REGEX='^[0-9]+$'
if [[ "${STRATEGY}" =~ $REGEX ]] ; then
cat ${TMP_FILE} | head -n ${STRATEGY} > ${TMP_FILE}.random
mv ${TMP_FILE}.random ${TMP_FILE}
fi
# If its a tilde ~ followed by a number, randomise and take top X
REGEX='^~[0-9]+$'
if [[ "${STRATEGY}" =~ $REGEX ]] ; then
cat ${TMP_FILE} | sort -R | head -n ${STRATEGY:1} > ${TMP_FILE}.random
mv ${TMP_FILE}.random ${TMP_FILE}
fi
}
# ╭──────────────────────────────────────────────────────────╮
# │ Cleanup │
# ╰──────────────────────────────────────────────────────────╯
function cleanup()
{
rm -f ${TMP_FILE}
rm -f ${FILELIST}
}
function print_flags()
{
printf "1️⃣ ${TEXT_GREEN_400}%-10s :${TEXT_RESET} %s\n" "Strategy" "$STRATEGY"
printf "2️⃣ ${TEXT_GREEN_400}%-10s :${TEXT_RESET} %s\n" "URL Src" "$URL_SOURCE"
}
# ╭──────────────────────────────────────────────────────────╮
# │ │
# │ Main Function │
# │ │
# ╰──────────────────────────────────────────────────────────╯
function main()
{
print_flags
configure_strategy
LOOP=1
while read FILE; do
# Filename
OUTPUT_FILE=${LOOP}_${OUTPUT_FILENAME}
# printf
# print to screen
printf "📥 ${TEXT_GREEN_400}%-10s :${TEXT_RESET} %s\n" "Input" "$FILE"
# download
curl --insecure --silent --show-error --url "$FILE" --output ${OUTPUT_FILE} 2>/dev/null
# Iterate.
LOOP=$(( $LOOP + 1 ))
done < ${TMP_FILE}
printf "✅ ${TEXT_PURPLE_500}%-10s :${TEXT_RESET} %s\n" "Output" "$OUTPUT_FILENAME"
}
cleanup
usage $@
arguments $@
read_config "$@"
read_url_input_list "$@"
main $@
cleanup