-
Notifications
You must be signed in to change notification settings - Fork 0
/
yt_download
executable file
·132 lines (103 loc) · 2.97 KB
/
yt_download
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
#!/usr/bin/env bash
#============================================|
# Downloads youtube audio
# created by: Joao Mauricio
# github: github.com/jean0t
# version: 1.4
#============================================|
# Changelog
#
# 1.0:
# - Downloads and extracts audios
# from youtube videos
#
# 1.1:
# - Test if there are links
# to download
#
# 1.2:
# - Test if the link passed is
# a valid youtube link
#
# 1.3:
# - More organized code with
# functions and a menu to
# interact with the user.
#
# - Now provides info about
# the app with parameters
#
# fixed:
# - download of multiple
# songs in a playlist
# without explicit allowing.
#
# 1.4:
# - Added support to download audio
# from a playlist.
#============================================|
# VERIFICATIONS
# Do not allow root to run
[[ "$UID" -eq '0' ]] && { echo 'Do not run as root' ; exit 1 ; }
# test if the necessary module exist
[[ $( type -p yt-dlp ) ]] || { echo 'yt-dlp not found.' ; exit 1 ; }
# Test internet conection
[[ $( ping -c1 8.8.8.8 ) ]] || { echo 'You need internet conection to run the script' ; exit 1 ; }
# test if the directory exists
[[ ! -e "$HOME/Downloads/Youtube_Download" ]] \
&& { echo 'created directory ~Downloads/Youtube_Download' ; mkdir -p "$HOME/Downloads/Youtube_Download" ; }
#============================================| GLOBAL VARIABLES
version='1.4'
output_directory="$HOME/Downloads/Youtube_Download/%(title)s.%(ext)s"
#============================================| FUNCTIONS
function version() {
echo "version ${version}"
}
function help() {
cat <<'EOF'
usage: ./yt_download [OPTIONS] LINK
OPTIONS:
-a|--audio Downloads the audio of the video
-pa|--playlist-audio Downloads the audio of all videos in a playlist
-h|--help Gets info about the software
-v|--version Gets the current version
REASON OF COMMON ISSUES:
- Absence of ffmpeg on the system
- Absence of the module mutagen
EOF
}
# accepts 2 parameters, first is the link, second is the confirmation if it will
# download the playlist or not
function download_audio() {
# test if there are parameters
[[ $# -eq 0 ]] && { echo 'you need to pass an youtube link to download' ; exit 1 ; }
# test if the link is valid
local youtube_regex='^(https?\:\/\/)?(www\.youtube\.com|youtu\.?be)\/.+$'
[[ $1 =~ $youtube_regex ]] || { echo 'It is not a valid link, please provide a https://youtube.com/...' ; exit 1 ; }
echo 'Starting the download...'
if yt-dlp -x -f bestaudio -o "$output_directory" --embed-thumbnail --"${2}"-playlist "$1" >&- 2>&- ; then
echo 'Download was successful, saved in:'
echo "$HOME/Downloads/Youtube_Download"
exit 0
else
echo $'\E[31;1mAn error has occurred.\E[0m\nUse -h or --help to know more.'
exit 1
fi
}
#============================================|START THE PROGRAM
case "$1" in
-pa|--playlist-audio)
shift
download_audio "$1" "yes"
;;
-a|--audio)
shift
download_audio "$1" "no"
;;
-v|--version)
version
;;
-h|--help|*)
help
;;
esac