-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathyt_utils.sh
More file actions
190 lines (168 loc) · 4.45 KB
/
Copy pathyt_utils.sh
File metadata and controls
190 lines (168 loc) · 4.45 KB
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
#!/usr/bin/env bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/lib/bashlib.sh"
source "${SCRIPT_DIR}/lib/cli.sh"
# ── yt.srt option metadata ────────────────────────────────────────────────────
YT_SRT_OPTS_SHORT=(-h -l)
YT_SRT_OPTS_LONG=(--help --lang)
YT_SRT_OPTS_ARG=("" "LANG")
YT_SRT_OPTS_DESC=("Display this help message" "Subtitle language code (default: en)")
# ── yt.mp3 option metadata ────────────────────────────────────────────────────
YT_MP3_OPTS_SHORT=(-h)
YT_MP3_OPTS_LONG=(--help)
YT_MP3_OPTS_ARG=("")
YT_MP3_OPTS_DESC=("Display this help message")
# ── yt.mp4 option metadata ────────────────────────────────────────────────────
YT_MP4_OPTS_SHORT=(-h -q)
YT_MP4_OPTS_LONG=(--help --quality)
YT_MP4_OPTS_ARG=("" "FORMAT")
YT_MP4_OPTS_DESC=("Display this help message" "yt-dlp format string")
# ── yt.chapters option metadata ───────────────────────────────────────────────
YT_CHAPTERS_OPTS_SHORT=(-h -n)
YT_CHAPTERS_OPTS_LONG=(--help --no-keep)
YT_CHAPTERS_OPTS_ARG=("" "")
YT_CHAPTERS_OPTS_DESC=("Display this help message" "Delete full file after splitting chapters")
yt.srt() {
dep_check yt-dlp
local show_help=0 lang="en"
while [[ $# -gt 0 ]]; do
case "$1" in
-h | --help)
show_help=1
shift
;;
-l | --lang)
lang="$2"
shift 2
;;
-*)
echo "Unknown option: $1" >&2
return 1
;;
*) break ;;
esac
done
eval "$(build_usage "YT_SRT" "${FUNCNAME[0]}" "URL [URL...]" "Download auto-generated subtitles as SRT.")"
((show_help)) && {
printf '%s\n' "$usage"
return 0
}
[[ -z "${1:-}" ]] && {
echo "Error: URL required" >&2
return 1
}
yt-dlp --skip-download --write-auto-subs --write-subs --sub-lang "$lang" --convert-subs srt "$@"
}
yt.mp3() {
dep_check yt-dlp
local show_help=0
while [[ $# -gt 0 ]]; do
case "$1" in
-h | --help)
show_help=1
shift
;;
-*)
echo "Unknown option: $1" >&2
return 1
;;
*) break ;;
esac
done
eval "$(build_usage "YT_MP3" "${FUNCNAME[0]}" "URL [URL...]" "Download audio as MP3.")"
((show_help)) && {
printf '%s\n' "$usage"
return 0
}
[[ -z "${1:-}" ]] && {
echo "Error: URL required" >&2
return 1
}
yt-dlp -x --audio-format mp3 "$@"
}
yt.mp4() {
dep_check yt-dlp
local show_help=0 quality=""
while [[ $# -gt 0 ]]; do
case "$1" in
-h | --help)
show_help=1
shift
;;
-q | --quality)
quality="$2"
shift 2
;;
-*)
echo "Unknown option: $1" >&2
return 1
;;
*) break ;;
esac
done
eval "$(build_usage "YT_MP4" "${FUNCNAME[0]}" "URL [URL...]" "Download best video+audio as MP4.")"
((show_help)) && {
printf '%s\n' "$usage"
return 0
}
[[ -z "${1:-}" ]] && {
echo "Error: URL required" >&2
return 1
}
yt-dlp -f "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best" ${quality:+-f "$quality"} "$@"
}
yt.chapters() {
dep_check yt-dlp
local show_help=0 no_keep=0
while [[ $# -gt 0 ]]; do
case "$1" in
-h | --help)
show_help=1
shift
;;
-n | --no-keep)
no_keep=1
shift
;;
-*)
echo "Unknown option: $1" >&2
return 1
;;
*) break ;;
esac
done
eval "$(build_usage "YT_CHAPTERS" "${FUNCNAME[0]}" "URL [YTDLP_ARGS...]" "Download audio split by chapters as MP3 into titled subdir.")"
((show_help)) && {
printf '%s\n' "$usage"
return 0
}
[[ -z "${1:-}" ]] && {
echo "Error: URL required" >&2
return 1
}
local url="$1"
shift
local dir_name extra_args=()
((no_keep)) && extra_args+=(--no-keep-video)
dir_name=$(yt-dlp --print "%(title)s" "$url") || return 1
echo "Output: \"$dir_name\""
mkdir -p "$dir_name" || return 1
yt-dlp \
--format bestaudio \
--audio-quality 0 \
--extract-audio \
--audio-format mp3 \
--download-sections "*0:00-inf" \
--split-chapters \
--output "chapter:%(section_number)02d %(section_title)s.%(ext)s" \
--paths "$dir_name" \
--no-mtime \
--no-playlist \
"${extra_args[@]}" \
"$@" "$url"
}
export -f yt.srt yt.mp3 yt.mp4 yt.chapters
register_completion "yt.srt" "YT_SRT"
register_completion "yt.mp3" "YT_MP3"
register_completion "yt.mp4" "YT_MP4"
register_completion "yt.chapters" "YT_CHAPTERS"