-
Notifications
You must be signed in to change notification settings - Fork 34
/
ytdl-chan
executable file
·96 lines (84 loc) · 2.88 KB
/
ytdl-chan
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
#!/bin/bash
usage() {
bin=$(basename $0)
echo >&2 "Usage:"
echo >&2
echo >&2 " $bin n_first [ n_last [ delay_sec ] ]"
echo >&2 " Download video(s) for youtube channel to the current dir"
echo >&2 " (from n_first to n_last) in same order to how they're"
echo >&2 " listed in the metadata cache file (e.g. oldest-to-latest)."
echo >&2 " Run in an empty dir with any numbers to get more info on how to"
echo >&2 " get metadata cache file (list of yt json manifests, one per line)."
echo >&2 " Numbers (n_first, n_last) are inclusive, 1-indexed."
echo >&2
echo >&2 " $bin { -n | --pick } n1 [ n2 ... ]"
echo >&2 " Download specific video(s), specified by their sequence number(s)."
echo >&2
echo >&2 " $bin { -r | --reverse-sort | --tac }"
echo >&2 " Reverse order of lines in the metadata file."
echo >&2 " Useful for 'ytuser:chan-name' meta dumps,"
echo >&2 " which are usually in latest-to-oldest order."
echo >&2
echo >&2 " $bin { -l | --list }"
echo >&2 " List titles from metadata cache file."
echo >&2
echo >&2 " $bin { -i | --info | -m | --meta | -u | --url } n"
echo >&2 " Dump metadata for video, specified by its sequence number."
echo >&2 " --info dumps pretty version, --meta - pure json, --url - just video URL."
echo >&2
echo >&2 " $bin [ -h | --help ]"
exit ${1:-0}
}
[[ -z "$1" || "$1" = -h || "$1" = --help ]] && usage
set -o pipefail
trap exit INT
err=
ytdl=yt-dlp
ytdl_opts=( ) # use ~/.config/<ytdl>/config
chan_cache=_meta.json
[[ ! -e "$chan_cache" ]] && {
echo >&2 "Create '$chan_cache' file with command like this one:"
echo >&2 " $ytdl -sj 'ytuser:TheGreatWar' >$chan_cache"
exit 1
}
[[ "$1" = -l || "$1" = --list ]] && {
[[ "$#" -ne 1 ]] && usage 1
jq -r '.fulltitle' "$chan_cache" | cat -n
exit
}
[[ "$1" = -r || "$1" = --reverse-sort || "$1" = --tac ]] && {
[[ "$#" -ne 1 ]] && usage 1
tac "$chan_cache" >"$chan_cache".new && mv "$chan_cache"{.new,}
exit
}
[[ "$1" = -i || "$1" = --info || "$1" = -m ||
"$1" = --meta || "$1" = -u || "$1" = --url ]] && {
[[ "$#" -ne 2 ]] && usage 1
case "$1" in
-i|--info) filter=( jq -CS . ) ;;
-u|--url) filter=( jq -r .webpage_url ) ;;
*) filter=( cat ) ;;
esac
sed -n "$2 p" "$chan_cache" | "${filter[@]}"
exit
}
sed_exp= delay=0
if [[ "$1" = -n || "$1" = --pick ]] ; then
[[ "$#" -ge 2 ]] || usage 1
shift
for n in "$@"; do sed_exp="${sed_exp}${n}p;"; done
else
[[ "$#" -le 3 ]] || usage 1
[[ -z "$2" ]] && sed_exp="$1 p" || sed_exp="$1,$2 p"
delay=${3:-0}
fi
while read n u; do
echo " ------ Fetching video $n"
$ytdl \
--no-overwrites --restrict-filenames \
"${ytdl_opts[@]}" -o "$(printf '%03d' $n)__%(title)s.%(ext)s" "$u" \
|| err="ERROR (last one only): n=$n code=$?"
[[ ! "$delay" -gt 0 ]] || { echo " --- delay: ${delay}s"; sleep "$delay"; }
done < <(jq -r '.webpage_url' "$chan_cache" | cat -n | sed -n "$sed_exp")
err=${err:-done}
echo " ------ $err"