-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathyoutube-play
executable file
·142 lines (121 loc) · 3.5 KB
/
youtube-play
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
#!/bin/bash
#############################################
ARCHIVE=/media/music
cd $ARCHIVE
# examples:
# https://blabla +cloud (download and save in cloud playlist)
# https://youtube.com/playlist? (download playlist)
# +cloud (play cloud playlist)
# always alright (play music)
#
##############################################
STOPFLAG=/tmp/.stopmusic
logme() {
echo $*
logger -t youtube-play "$*"
}
play_stop() {
ALL=$1
[ -n "$ALL" ] && touch $STOPFLAG
killall gst-play-1.0 2>/dev/null
}
play_file() {
FILENAME="$1"
#logger -t youtube-play "playing $1"
play_stop
send_message "${FILENAME}"
gst-play-1.0 "$FILENAME"
}
send_message() {
MESSAGE="$1"
KEYBOARD="$2"
MESSAGE=${MESSAGE//_/} # remove _
[ -z "$KEYBOARD" ] && KEYBOARD='[".stop,.list,.next",".vol+,.help,.vol-"]'
PASS=$(grep http_password ~/.homeassistant/secrets.yaml | awk '{ print $2 }')
for service in 'telegram' 'telegram_lorenzo'; do
curl -X POST -H "x-ha-access: $PASS" -H "Content-Type: application/json" \
-d "{\"message\": \"$MESSAGE\", \"data\":{\"keyboard\":$KEYBOARD}}" \
http://localhost:8123/api/services/notify/${service}
done
}
download() {
URL="$1"
OUT="$2"
youtube-dl -f bestaudio \
--metadata-from-title "%(artist)s - %(title)s" --add-metadata \
--download-archive ${ARCHIVE}/.archive.txt \
--youtube-skip-dash-manifest \
-o "$OUT" "$URL" --exec "ffmpeg -i {} -codec:a libmp3lame -qscale:a 0 {}.mp3 && rm {} "
}
IFS=' ' read -r -a WORDS <<< "$*" # lower and split into array
#LAST="${WORDS##* }"
COMMAND=${WORDS[-1]:0:1} # first char of last word
exec 3>&1 1>> /tmp/youtube.log 2>&1
set -x
if [ "$COMMAND" = "+" ]; then # playlist
PLAYLIST=${WORDS[-1]:1:99}
PLAYLIST=${PLAYLIST,,} # lowercase
if [ -n "$PLAYLIST" ]; then
unset WORDS[-1]
MATCHED=$(find -maxdepth 1 -iname "*$PLAYLIST*" -type d | head -n 1)
[ -n "$MATCHED" ] && PLAYLIST="$MATCHED"
mkdir -p "$PLAYLIST"
cd "$PLAYLIST"
fi
fi
if [ "$COMMAND" = "." ]; then
case ${WORDS[-1]:1:99} in
stop)
play_stop 'all'
;;
next)
play_stop
;;
vol-)
amixer set PCM 20%-
;;
vol+)
amixer set PCM 20%+
;;
list)
PLAYLISTS="$(find . -maxdepth 1 -type d -name "??*" | sort | xargs | sed 's|^..||' | sed 's| \.\/|\\n|g')"
send_message "$PLAYLISTS"
;;
help)
send_message "<string>: play song\n+<playlist>: play playlist\n<url> +<playlist>: download from url and put in playlist"
;;
*)
send_message "command not known"
esac
exit 0
fi
# download playlist from youtube
if [[ "${WORDS[*]}" =~ '/playlist?' ]]; then
URL=${WORDS[0]}
logme "detected youtube playlist"
download "$URL" '%(playlist)s/%(playlist_index)s - %(title)s.%(ext)s'
PLAYLIST=$(find . -maxdepth 1 -type d -printf "%T+|%p\n" | sort -nr | head -n1 | cut -d '|' -f2 | cut -b 3-)
SIZE=$(du "$PLAYLIST" -hs | cut -f1)
send_message "downloaded playlist '$PLAYLIST' $SIZE"
# download single video from youtube
elif [[ "${WORDS[*]}" =~ "https://" ]]; then
URL=${WORDS[0]}
logme "detected youtube url=$URL"
#FILENAME=$(youtube-dl --get-filename -f m4a "$URL") # find music title
download "$URL" '%(title)s.%(ext)s'
FILENAME=$(find . -maxdepth 1 -type f -printf "%T+|%p\n" | sort -nr | head -n1 | cut -d '|' -f2 | cut -b 3-)
send_message "downloaded ${FILENAME/.m4a/}"
play_stop 'all'
play_file "$FILENAME"
# pattern matching with $WORDS
else
SEARCH=${WORDS[*]}
play_stop 'all'
rm -f $STOPFLAG
find -iname "*$SEARCH*" ! -name ".*" -type f | sort | while read file; do
[ -f $STOPFLAG ] && break
play_file "${file#./}"
done
fi
rm -f $STOPFLAG
exit 0