-
Notifications
You must be signed in to change notification settings - Fork 1
/
ytdl_play.sh
executable file
·128 lines (97 loc) · 2.1 KB
/
ytdl_play.sh
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
echo "ytdl_play.sh [url|query]"
echo
echo "You can use this to play youtube on stupidly low-powered computers"
echo
if [[ -z $1 ]]
then
exit 1
fi
function dlp {
# arguments = arguments to ytdlp
cmd="/usr/bin/python3.8 -m yt_dlp $@ --no-warnings"
>&2 echo "$ $cmd"
/usr/bin/python3.8 -m yt_dlp "$@" --no-warnings
}
function get_dlp_url {
# args:
# 1 = url/query
# 2 = search count (if query)
#
# output:
# dlp url (normal url if 1 is url, ytsearch$2:$1 if is query
#
# return code:
# 1 = query
# 0 = url
>&2 echo "get_dlp_url $1 $2"
if [[ ! $1 = https://* ]]; then
if [[ $1 = ytsearch* ]]; then
# already ytsearch
echo $1
exit 1
fi
echo ytsearch$2:$1
exit 1
fi
echo $1
exit 0
}
function get_vid_title {
# 1 = url/query
# 2 = count
>&2 echo "get_vid_title $1 $2"
count=$2
if [[ -z $2 ]]; then
count=1
fi
dlp "$(get_dlp_url "$1" $count)" --get-title
}
function get_vid_url {
# 1 = title
>&2 echo "get_vid_url $1"
vidid=$(dlp "$(get_dlp_url "$1")" --get-id)
if [ ! $? -eq 0 ]; then
code=$?
echo
exit $code
fi
echo "https://youtube.com/watch?v=$vidid"
}
function dlplay {
# 1 = url
# 2 = title
>&2 echo "dlplay $1 $2"
vidtitle="$2"
if [[ -z $2 ]]; then
vidtitle=$(get_vid_title "$1")
fi
outpath="$HOME/ytdl_play_vids/$(echo ${vidtitle// /_}).mp4"
dlp "$1" -o "$outpath" -S res:480p -f mp4
if [ ! $? -eq 0 ]; then
exit $?
fi
xdg-open "$outpath" > /dev/null &
}
url=$(get_dlp_url "$1" 9)
if [ $? -eq 0 ]; then
echo "URL detected. Downloading & playing."
dlplay "$1"
exit 0
fi
echo "Searching..."
vidsdlp=$(get_vid_title "$url")
if [ ! $? -eq 0 ]; then
exit $?
fi
readarray -t vids <<< $vidsdlp
echo "Found:"
for i in "${!vids[@]}"; do
echo "$(($i+1)) | ${vids[$i]}"
done
echo
echo "Select:"
read -n 1 selection
echo
vid=$(($selection-1))
vidtitle="${vids[$vid]}"
dlplay "$(get_vid_url "$vidtitle")" "$vidtitle"