-
Notifications
You must be signed in to change notification settings - Fork 0
/
easydl
executable file
·391 lines (368 loc) · 10.9 KB
/
easydl
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
#!/bin/bash
#make pushd silent
pushd() {
command pushd "$@" >/dev/null
}
#make popd silent
popd() {
command popd "$@" >/dev/null
}
YOUTUBECOMMAND=""
if command -v "yt-dlp" &>/dev/null; then
YOUTUBECOMMAND="yt-dlp"
elif command -v "youtube-dl" &>/dev/null; then
YOUTUBECOMMAND="youtube-dl"
else
echo 'youtube-dl or yt-dlp is required, for more support please reference the README'
exit
fi
LINKS=0
#set the install directory as the current directory as soon as the program starts
homedir=~/.config/easydl/
#Variables for coloring text
CYAN='\033[0;36m'
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color
execute_flags() {
if [ "$1" = '-i' ]; then
alias_initialize
elif [ "$1" = '-a' ]; then
alias_add
elif [ "$1" = '-d' ]; then
alias_display
elif [ "$1" = '-e' ]; then
alias_edit
elif [ "$1" = '-r' ]; then
alias_remove
elif [ "$1" = '-c' ]; then
alias_clear
elif [ "$1" = '-y' ]; then
echo "opening nano so you can edit the youtube commands"
sleep 3
nano ~/.config/easydl/youtube_commands.sh
elif [ "$1" = '-h' ]; then
echo '-i initliazes the alias.sh file to allow alias creation. Will delete previous aliases if present'
echo '-a add a new alias'
echo '-d display all of your aliases'
echo "-e edit an alias"
echo '-r remove an alias'
echo '-c clear all aliases'
echo '-y edit the youtube-dl commands'
echo '--readme Open the README in nano (~/.config/easydl/README.md)'
echo '--cut open cutter'
elif [ "$1" = '--readme' ]; then
showREADME
elif [ "$1" = '--cut' ]; then
cutter
else
main
fi
}
main() {
YORN='y'
source $homedir/alias.sh
while [ "${YORN}" = 'y' ]; do
echo 'video (mp4) or audio'
read -r -p 'video or audio> ' format
echo ''
if [ "$format" = "audio" ]; then
echo 'what audio format would you like? ("best", "aac", "flac", "mp3", "m4a", "opus", "vorbis", or "wav" with best giving the best possible format)'
read -r -p 'audio format> ' audioFormat
summaryFormat="$audioFormat"
echo ''
else
summaryFormat="Video"
fi
echo 'what url?'
read -r -p 'paste url here (ctrl shift v)> ' url
echo ''
echo 'would you like to download a playlist?'
read -r -p 'yes or no> ' wantsPlaylist
echo ''
if [[ "$wantsPlaylist" != "yes" ]] && [[ "$wantsPlaylist" != "no" ]]; then
echo 'error: invalid answer'
echo 'please answer with "yes" or "no" only'
exit
fi
if [ $LINKS -gt 0 ]; then
echo 'same directory as before?'
read -r -p 'y or n> ' yesorno
echo ''
if [ "${yesorno}" = 'n' ]; then
echo 'where do you want it? ex ~/Videos/ or /media/'
read -r -p 'directory> ' down_dir
echo ''
check_alias "$down_dir"
fi
elif [ $LINKS -eq 0 ]; then
echo 'where do you want it? ex ~/Videos/ or /media/'
read -r -p 'directory> ' down_dir
echo ''
check_alias "$down_dir"
fi
isPlaylist=$($YOUTUBECOMMAND -o "%(playlist_title)s" --get-filename --playlist-items 1 "$url")
if [ "$isPlaylist" = "NA" ]; then # link is not a playlist
#get video title
downloadTitle=$($YOUTUBECOMMAND --get-title "$url")
else
if [ "$wantsPlaylist" = "no" ]; then
#get video title of single youtube video form playlist
downloadTitle=$($YOUTUBECOMMAND --no-playlist --get-title "$url")
else
#set video title to the url (playlist)
downloadTitle=$($YOUTUBECOMMAND -o "%(playlist_title)s" --get-filename --playlist-items 1 "$url")
fi
fi
echo '
'
#give the user a summary of what is about to happen (it gives them the url, the format, and the actual directory that it will be downloaded to)
echo 'Downloading "'"$downloadTitle"'" as a(n) '"$summaryFormat"' file in' "$finaldir"
sleep 5
echo '
'
source $homedir/youtube_commands.sh
#check which format the user defined
if [ "$wantsPlaylist" = "no" ]; then
if [ "${format}" = 'audio' ]; then
pushd "$finaldir"
YoutubeAudio $url $audioFormat
popd
elif [ "${format}" = 'video' ]; then
pushd "$finaldir"
YoutubeVideo $url
popd
else
echo 'that is not understood, please type video or audio only'
fi
else
if [ "${format}" = 'audio' ]; then
pushd "$finaldir"
YoutubeAudioPlaylist $url $audioFormat
popd
elif [ "${format}" = 'video' ]; then
pushd "$finaldir"
YoutubeVideoPlaylist $url
popd
else
echo 'that is not understood, please type video or audio only'
fi
fi
echo '
'
echo 'done, do you have more?'
read -r -p 'y or n> ' YORN
if [ "${YORN}" = 'y' ]; then
((LINKS = LINKS + 1))
fi
done
echo ""
echo 'have a nice day :)'
}
check_alias() {
foundalias="false"
source $homedir/alias.sh
arg=$1
for index in "${!aliasnames[@]}"; do
name=${aliasnames[$index]}
if [ "$name" = "$arg" ]; then
regularDirectory="${aliasvalues[$index]}"
foundalias="true"
fi
done
if [ "$foundalias" != "true" ]; then
regularDirectory="$arg"
fi
# finaldir is the name of the output variable
expandPath "$regularDirectory" "finaldir"
}
alias_initialize() {
touch $homedir/alias.sh
chmod a+x $homedir/alias.sh
echo "aliasnames=()" >$homedir/alias.sh
echo "aliasvalues=()" >>$homedir/alias.sh
}
alias_add() {
echo "Adding an Alias"
echo "What would you like to name your alias"
read -r -p "name > " name
echo "And where would you like the actual directory to be located ex. ~/Videos/Movies/"
read -r -p "directory > " dir
#append the name to the name array
echo "aliasnames+=("$name")" >>$homedir/alias.sh
#append the directory to the value array
echo "aliasvalues+=("$dir")" >>$homedir/alias.sh
echo "All done!"
echo "Why not give it a try :)"
}
alias_display() {
source $homedir/alias.sh
for index in "${!aliasnames[@]}"; do
echo -e "Name: \c"
echo ${aliasnames[$index]}
echo -e "Directory: \c"
echo ${aliasvalues[$index]}
echo ''
done
}
alias_edit() {
source $homedir/alias.sh
foundalias="false"
echo "Editing an existing Alias"
echo "What is the name of the alias you would like to edit?"
read -r -p "name > " edit_name
for index in "${!aliasnames[@]}"; do
value=${aliasnames[$index]}
if [ "$value" = "$edit_name" ]; then
foundalias="true"
edit_index=$index
fi
done
if [ "$foundalias" != "true" ]; then
echo "Cannot find that alias, try again and make sure that you type it in exactly"
exit
fi
echo "What would you like to change the NAME to (enter '"'skip'"' to skip)"
read -r -p "Name > " Name_ans
if [ "$Name_ans" != "skip" ]; then
newname=$Name_ans
else
newname=${aliasnames[$edit_index]}
fi
echo "What would you like to change the DIRECTORY to (enter '"'skip'"' to skip)"
read -r -p "Directory > " Dir_ans
if [ "$Dir_ans" != "skip" ]; then
newdir=$Dir_ans
else
newdir=${aliasvalues[$edit_index]}
fi
alias_change $newname $newdir $edit_name $edit_index
}
alias_change() {
newname=$1
newdir=$2
edit_name=$3
edit_index=$4
source $homedir/alias.sh
rm $homedir/alias.sh
alias_initialize
for loopindex in "${!aliasnames[@]}"; do
currentname=${aliasnames[$loopindex]}
if [ "$currentname" != "$edit_name" ]; then
keepname=${aliasnames[$loopindex]}
keepdir=${aliasvalues[$loopindex]}
echo "aliasnames+=("$keepname")" >>$homedir/alias.sh
echo "aliasvalues+=("$keepdir")" >>$homedir/alias.sh
else
echo "aliasnames+=("$newname")" >>$homedir/alias.sh
echo "aliasvalues+=("$newdir")" >>$homedir/alias.sh
fi
done
}
alias_remove() {
source $homedir/alias.sh
foundalias="false"
echo "Removing Alias"
echo "What is the name of the Alias you would like to remove?"
read -r -p "Name > " remove_name
for index in "${!aliasnames[@]}"; do
value=${aliasnames[$index]}
if [ "$value" = "$remove_name" ]; then
foundalias="true"
edit_index=$index
fi
done
if [ "$foundalias" != "true" ]; then
echo "Cannot find that alias, try again and make sure that you type it in exactly"
exit
fi
rm $homedir/alias.sh
alias_initialize
for loopindex in "${!aliasnames[@]}"; do
currentname=${aliasnames[$loopindex]}
if [ "$currentname" != "$remove_name" ]; then
keepname=${aliasnames[$loopindex]}
keepdir=${aliasvalues[$loopindex]}
echo "aliasnames+=("$keepname")" >>$homedir/alias.sh
echo "aliasvalues+=("$keepdir")" >>$homedir/alias.sh
fi
done
}
alias_clear() {
rm $homedir/alias.sh
alias_initialize
}
expandPath() {
x=$1
local __resultvar=$2
case $x in
*"~/"*)
contains="found"
x="${HOME}/${x#"~/"}"
local __finalx=$x
;;
esac
if [ "$contains" != "found" ]; then
local __finalx=$x
fi
eval $__resultvar="'$__finalx'"
}
showREADME() {
nano --softwrap --view ~/.config/easydl/README.md
}
cutter() {
filenames=()
start_times=()
end_times=()
echo -e "${CYAN}Would you like the cuts to favor speed or accuracy?${NC}"
read -r -p '"speed" or "accuracy">' pref
echo -e "${CYAN}Enter full path to orignal file (including filename and extension)${NC}"
read -r -p ">" full_path
echo ""
original_path=$(dirname "$full_path")
original=$(basename "$full_path")
get_times
cut
}
get_times() {
sraw="initial"
#while the user doesn't input "done" for the filename
while [ "$sraw" != "done" ]; do
echo -e "${CYAN}Enter filename (including extension) or \"done\" to start cutting${NC}"
read -r -p "filename > " sraw
if [ "$sraw" != "done" ]; then
filenames+=("$sraw")
echo -e "${CYAN}Enter ${GREEN}start${CYAN} time${NC}"
read -r -p "HH:MM:SS > " raw
#turn the HH:MM:SS formated time to seconds (it can also accept M:SS and S)
start=$(echo $raw | awk -F\: '{ for(k=NF;k>0;k--) sum+=($k*(60^(NF-k))); print sum }')
start_times+=("$start")
echo -e "${CYAN}Enter ${RED}end${CYAN} time${NC}"
read -r -p "HH:MM:SS > " raw
end=$(echo $raw | awk -F\: '{ for(k=NF;k>0;k--) sum+=($k*(60^(NF-k))); print sum }')
end_times+=("$end")
echo
fi
done
}
cut() {
# original_path is the name of the output variable
expandPath "$original_path" "original_path"
pushd "$original_path"
for index in "${!filenames[@]}"; do
filename=${filenames[$index]}
final_start=${start_times[$index]}
final_end=${end_times[$index]}
echo
#execute ffmpeg $original = original file name, $final_start = starting time in seconds, $final_end = ending time in seconds, $filename = filename for cut file
if [ $pref == "speed" ]; then
ffmpeg -hide_banner -i "$original" -ss "$final_start" -to "$final_end" -c copy "$filename"
else
ffmpeg -hide_banner -i "$original" -ss "$final_start" -to "$final_end" -async 1 "$filename"
fi
done
#reset directory
popd
}
#start the program by first analyzing the command line options
execute_flags "$1"