-
Notifications
You must be signed in to change notification settings - Fork 0
/
yt-dlp-lemon
executable file
·482 lines (438 loc) · 11.4 KB
/
yt-dlp-lemon
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#!/usr/bin/env bash
app_name='yt-dlp-lemon'
app_version='0.5'
# python3 yt-dlp python-mutagen ffmpeg
# Default file for option -i FILE .
ignore_file="${HOME}/.local/share/${app_name}/ignore.lst"
# Used by option -S or -B
sponsorblock_all_marks_list=(
sponsor
intro
outro
selfpromo
preview
filler
interaction
)
# Used by option -D
write_all_extras_list=(
--write-playlist-metafiles
--write-description
--write-info-json
--write-sub
--write-desktop-link
--write-thumbnail
)
# Used by option -E
embed_all_extras_list=(
--xattrs
--embed-metadata
--embed-sub
--embed-thumbnail
--embed-chapters
--embed-info-json
)
help_header() {
cat <<EOF
${app_name} [options] [url...]
Simple wrapper to yt-dlp with only a subset of options.
EOF
}
help_options() {
cat <<EOF
options:
-h show help and exit
-H show all options, notes and exit
-m HEIGHT max height
-f FORMAT repack format
-I no ignore file
-s add chapter marks + recognize sponsors
-b remove sponsored segments
-c split file by chapters
-p playlist mode
-a audio mode
-d download description files
-e embed meta and chapters
-q show filepath only
-x skip download
EOF
}
help_options_all() {
cat <<EOF
options:
-h show help and exit
-H show all options, notes and exit
-v show version and exit
-m HEIGHT allowed max height of video size, example "480"
-l MB download speed limit in MB/s, example "4.2"
-r LEN restrict and shorten filenames to ASCII chars
-n NAME set first base part of output filename
-i FILE set alternate ignore file [1]
-I do not use any ignore file, force download [1]
-f FORMAT repackage container to format, example "mp4" [2]
-F FORMAT same as -f, but will force re-encode if needed [2]
-w worst available video and audio quality
-s add chapter marks + recognize sponsor by SponsorBlock [3]
-S same as -s, but add all type of segments [3]
-b remove sponsor segments from file by SponsorBlock [3]
-B same as -b, but remove all type of segments [3]
-c split video creation by separate chapter files
-C same as -c, but don't write index number into name
-p playlist mode, download entire list with index names
-P same as -p, but don't write index number into name
-R same as -p, but with reverse index order
-a audio mode, extract and keep audio file only
-k keep all intermediate and uncut video files
-d save description and info-json as separate text files
-D same as -d, but download all type of extra files
-e embed metadata and chapter marks into video file
-E same as -e, but embed all type of extras
-q hide info, show progress bar and filepath only
-x skip download, print information only
EOF
}
help_notes() {
if [[ -f "${ignore_file}" ]]; then
ignore_count="$(grep -cE '[0-9a-zA-Z]' -- "${ignore_file}")"
else
ignore_count="0"
fi
cat <<EOF
[1] -i -I
URLs are only downloaded if they are missing from ignore file. Successful
downloads are added to it automatically. -I will not use any ignore file and
force download existing files. Set alternative file with -i or default to:
${ignore_count} "${ignore_file}"
[2] -f -F
-f to repackage output format without re-encoding file. -F will instead force
re-encoding to specified format when needed and encoder is available. If
container does not support the video/audio codec, then operation fails. Simple
examples are "mp4" or "mp3". Available formats:
avi, flv, gif, mkv, mov, mp4, webm, aac, aiff, alac, flac, m4a, mka, mp3,
ogg, opus, vorbis, wav
[3] -s -S -b -B
-s will use SponsorBlock engine to add chapter marks for your video player. In
addition, it will also recognize sponsored segments. -b will remove those
sponsored segments from final video output. The uppercase variants -S and -B do
the same respectively, but recognizes all type of segments:
${sponsorblock_all_marks_list[@]}
EOF
}
help_footer() {
cat <<EOF
Copyright © 2024 Tuncay D. <https://github.com/thingsiplay/${app_name}>
EOF
}
OPTIND=1
while getopts ':hHvm:l:r:n:i:If:F:wsSbBcCpPRakdDeEqx' OPTION; do
case "$OPTION" in
h)
help_header
help_options
help_footer
exit 0
;;
H)
help_header
help_options_all
help_notes
help_footer
exit 0
;;
v)
echo "${app_name} v${app_version}"
exit 0
;;
n)
if ! [[ ${OPTARG} =~ ^[0-9a-z\ A-Z]+$ ]]; then
echo "Only alphanumerical characters allowed for -n." >&2
exit 1
fi
name="${OPTARG}"
;;
i)
ignore_file="${OPTARG}"
;;
m)
if ! [[ ${OPTARG} =~ ^[0-9]+$ ]]; then
echo "Only digits allowed for -m." >&2
exit 1
fi
max_height="${OPTARG}"
;;
l)
if ! [[ ${OPTARG} =~ ^[\.0-9]+$ ]]; then
echo "Only digits and dot allowed for -l." >&2
exit 1
fi
limit="${OPTARG}"
;;
r)
if ! [[ ${OPTARG} =~ ^[0-9]+$ ]]; then
echo "Only digits allowed for -r." >&2
exit 1
fi
restrict_filenames="${OPTARG}"
;;
I)
no_ignore_file=true
;;
f)
if ! [[ ${OPTARG} =~ ^[a-z0-9/\>]+$ ]]; then
echo "Only digits, lowercase, '/', '>' allowed for -f" >&2
exit 1
fi
format_remux="${OPTARG}"
;;
F)
if ! [[ ${OPTARG} =~ ^[a-z0-9/\>]+$ ]]; then
echo "Only digits, lowercase, '/', '>' allowed for -F." >&2
exit 1
fi
format_recode="${OPTARG}"
;;
w)
worst=true
;;
c)
split_chapters=true
;;
C)
split_chapters=true
without_chapter_index=true
;;
p)
playlist=true
;;
P)
playlist=true
without_playlist_index=true
;;
R)
playlist=true
reverse_playlist_index=true
;;
a)
extract_audio=true
;;
s)
add_marks=(sponsor)
;;
S)
add_marks=("${sponsorblock_all_marks_list[@]}")
;;
b)
block_marks=(sponsor)
;;
B)
block_marks=("${sponsorblock_all_marks_list[@]}")
;;
k)
keep=true
;;
d)
write_extras=(--write-description --write-info-json)
;;
D)
write_extras=("${write_all_extras_list[@]}")
;;
e)
embed_extras=(--embed-metadata --embed-chapters)
;;
E)
embed_extras=("${embed_all_extras_list[@]}")
;;
x)
skip_download=true
;;
q)
quiet=true
;;
?)
echo "Error parsing options: '${OPTARG}'" >&2
exit 1
;;
esac
done
shift "$((OPTIND - 1))"
cmd=(
yt-dlp
--ignore-errors
--extractor-retries
10
)
if [ -n "${format_recode}" ] && ! [ "${format_recode}" == "" ]; then
cmd+=(
--recode-video
"${format_recode}"
)
elif [ -n "${format_remux}" ] && ! [ "${format_remux}" == "" ]; then
cmd+=(
--remux-video
"${format_remux}"
)
fi
if [ -n "${worst}" ] && [ "${worst}" == true ]; then
quality_type="worst"
else
quality_type="best"
fi
if [ -n "${quiet}" ] && [ "${quiet}" == true ]; then
cmd+=(
--quiet
--no-warnings
--progress
--print after_move:filepath
--no-simulate
)
fi
if [ -n "${skip_download}" ] && [ "${skip_download}" == true ]; then
if [ -n "${quiet}" ] && [ "${quiet}" == true ]; then
fmt='%(duration>%H:%M:%S)s | %(uploader)s | %(title)s | %(filesize_approx)s bytes'
else
fmt='%(duration>%H:%M:%S.%s)s %(uploader)s - %(title)s\n%(filesize_approx)s bytes\n\n%(description)s'
fi
cmd+=(
--skip-download
--print "${fmt}"
--no-download-archive
--no-overwrites
)
elif [ -n "${no_ignore_file}" ] && [ "${no_ignore_file}" == true ]; then
cmd+=(
--no-download-archive
--force-overwrites
)
elif [ -n "${ignore_file}" ]; then
ignore_file="$(readlink -m -- "${ignore_file}")"
mkdir -p -- "${ignore_file%/*}"
touch -- "${ignore_file}" || exit 1
cmd+=(
--download-archive "${ignore_file}"
--no-overwrites
)
fi
if [ -n "${restrict_filenames}" ] && [ "${restrict_filenames}" -gt 0 ]; then
cmd+=(--restrict-filenames)
cmd+=(--trim-filenames "${restrict_filenames}")
else
cmd+=(--windows-filenames)
cmd+=(--replace-in-metadata "title,channel,playlist" "[/|]" " ")
cmd+=(--replace-in-metadata "title,channel,playlist" "[–•]" "-")
cmd+=(--replace-in-metadata "title,channel,playlist" " ?: ?" " - ")
cmd+=(--replace-in-metadata "title,channel,playlist" "<" "(")
cmd+=(--replace-in-metadata "title,channel,playlist" ">" ")")
cmd+=(--replace-in-metadata "title,channel,playlist" " +" " ")
cmd+=(--replace-in-metadata "title,channel,playlist" "[^][()!0-9a-zA-Z- _а-яА-ЯŽžÀ-ÿ\.,\"']" "")
cmd+=(--replace-in-metadata "title,channel,playlist" "\(\)" "")
cmd+=(--replace-in-metadata "title,channel,playlist" "^ +" "")
cmd+=(--replace-in-metadata "title,channel,playlist" " +$" "")
fi
if [ -z "${name}" ] || [ "${name}" == "" ]; then
if [ -n "${restrict_filenames}" ] && [ "${restrict_filenames}" -gt 0 ]; then
name="%(title)s_by_%(channel)s"
else
name="%(title)s by %(channel)s"
fi
fi
if [ -n "${playlist}" ] && [ "${playlist}" == true ]; then
cmd+=(--yes-playlist)
if [ -n "${without_playlist_index}" ] && [ "${without_playlist_index}" == true ]; then
index=""
elif [ -n "${reverse_playlist_index}" ] && [ "${reverse_playlist_index}" == true ]; then
cmd+=(--playlist-reverse)
index="%(autonumber)s - "
else
index="%(playlist_index)s - "
fi
if [ -z "${skip_download}" ] || [ "${skip_download}" == false ]; then
playlist_name="%(playlist_title)s/${index}"
cmd+=(
--output
"${playlist_name}${name}.%(ext)s"
)
fi
else
cmd+=(--no-playlist)
if [ -z "${skip_download}" ] || [ "${skip_download}" == false ]; then
cmd+=(
--output
"${name}.%(ext)s"
)
fi
fi
if [ -n "${split_chapters}" ] && [ "${split_chapters}" == true ]; then
cmd+=(--split-chapters)
if [ -z "${skip_download}" ] || [ "${skip_download}" == false ]; then
if [ -n "${without_chapter_index}" ] && [ "${without_chapter_index}" == true ]; then
index=""
else
index="%(section_number)s - "
fi
cmd+=(
--output
"chapter:${name}/${index}%(section_title)s.%(ext)s"
)
fi
else
cmd+=(--no-split-chapters)
fi
if [ -n "${keep}" ] && [ "${keep}" == true ]; then
cmd+=(--keep-video)
else
cmd+=(--no-keep-video)
fi
if [ -n "${limit}" ] && ! [ "${limit}" == '0' ]; then
cmd+=(--limit-rate "${limit}"M)
fi
if [ -n "${extract_audio}" ] && [ "${extract_audio}" == true ]; then
if [ -n "${worst}" ] && [ "${worst}" == true ]; then
cmd+=(
--extract-audio
--format 'worstaudio'
)
else
cmd+=(
--extract-audio
--audio-format best
--audio-quality 0
--format 'bestaudio'
)
fi
else
if [ -n "${max_height}" ] && [ "${max_height}" -gt 0 ]; then
quality="${quality_type}video[height<=${max_height}]+${quality_type}audio/${quality_type}[height<=${max_height}]"
else
quality="${quality_type}video+${quality_type}audio"
fi
cmd+=(--format "${quality}")
fi
if [ "${#add_marks[@]}" -gt 0 ]; then
cmd+=(--sponsorblock-mark)
for m in "${add_marks[@]}"; do
marks="${marks},${m}"
done
cmd+=("${marks:1}")
fi
if [ "${#block_marks[@]}" -gt 0 ]; then
cmd+=(--sponsorblock-remove)
for m in "${block_marks[@]}"; do
marks="${marks},${m}"
done
cmd+=("${marks:1}")
fi
if [ "${#write_extras[@]}" -gt 0 ]; then
cmd+=("${write_extras[@]}")
fi
if [ "${#embed_extras[@]}" -gt 0 ]; then
cmd+=("${embed_extras[@]}")
fi
if [ -n "${skip_download}" ] && [ "${skip_download}" == true ]; then
for url in "${@}"; do
"${cmd[@]}" "${url}" | sed -e 's/\\n/\n/g' -e '/^[[:space:]]*$/d'
done
else
for url in "${@}"; do
"${cmd[@]}" "${url}"
done
fi