-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyoutube-download.sh
More file actions
executable file
·109 lines (96 loc) · 2.35 KB
/
Copy pathyoutube-download.sh
File metadata and controls
executable file
·109 lines (96 loc) · 2.35 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
#!/usr/bin/env bash
set -euo pipefail
readonly RED='\033[0;31m'
readonly NC='\033[0m'
log() {
echo "[$(date -u)] $*"
}
die() {
echo -e "${RED}[$(date -u)] ERROR: $*${NC}" >&2
exit 1
}
usage() {
cat <<EOF
Script to download YouTube media (playlists or single videos) as MP3 (default) or video.
Usage:
$(basename "$0") [-v|--video] <url_or_id> [dest_folder]
Options:
-v, --video Download best-quality video (mkv) instead of audio-only MP3
Arguments:
url_or_id - A full YouTube URL, a playlist ID, or a single video ID
dest_folder - Where to store downloaded files (default: current directory)
EOF
exit 1
}
MODE="audio"
TARGET=""
DEST=""
while [[ $# -gt 0 ]]; do
case "$1" in
-v | --video)
MODE="video"
shift
;;
-h | --help)
usage
;;
-*)
die "Unknown option $1"
;;
*)
if [[ -z "$TARGET" ]]; then
TARGET="$1"
elif [[ -z "$DEST" ]]; then
DEST="$1"
else
die "Too many arguments provided"
fi
shift
;;
esac
done
[[ -z "$TARGET" ]] && usage
DEST="${DEST:-$PWD}"
mkdir -p "$DEST" || die "Cannot create directory '$DEST'"
cd "$DEST" || die "Cannot switch to directory '$DEST'"
# Tool selection. Prefer yt-dlp over the largely unmaintained youtube-dl
if command -v yt-dlp &>/dev/null; then
YOUTUBE_DL="yt-dlp"
EXTRA_FLAGS=(--embed-metadata --concurrent-fragments 4)
elif command -v youtube-dl &>/dev/null; then
YOUTUBE_DL="youtube-dl"
EXTRA_FLAGS=(--add-metadata)
else
die "Neither yt-dlp nor youtube-dl is installed"
fi
COMMON_FLAGS=(
-4
--retries 3
--restrict-filenames
--continue
--no-progress
--ignore-errors
--embed-thumbnail
--sleep-interval 5
--download-archive "download-archive.txt"
"${EXTRA_FLAGS[@]}"
)
log "Start downloading in $MODE mode"
if [[ "$MODE" == "audio" ]]; then
"$YOUTUBE_DL" \
"${COMMON_FLAGS[@]}" \
--extract-audio \
--audio-format mp3 \
--audio-quality 0 \
-o "%(artist,uploader|Unknown)s-%(track,title)s.%(ext)s" \
"$TARGET"
else
"$YOUTUBE_DL" \
"${COMMON_FLAGS[@]}" \
-f "bestvideo+bestaudio/best" \
--merge-output-format mkv \
--embed-subs \
-o "%(title)s.%(ext)s" \
"$TARGET"
fi
log "Download completed"