summaryrefslogtreecommitdiff
path: root/scripts/musiccmd
blob: ecaf437a186a208e45f5c5b287928d36246e656d (plain)
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
#!/bin/sh
# helper for the music script
# dependencies: music, mpv, socat, yt-dlp, ffmpeg, imagemagick
# optional dependencies: dwmblocks, libnotify, herbe

FAVDIR="${XDG_MUSIC_DIR:-$HOME/music}/favorite"
DLDIR="${XDG_MUSIC_DIR:-$HOME/music}/download"
CACHE="${XDG_CACHE_HOME:-$HOME/.cache}"
IMG="nsxiv"

getpath() {
	tmp="$(printf '{ "command": ["get_property", "path"] }\n'\
		| socat - /tmp/mpvsocket)"
	tmp="${tmp#{\"data\":\"}"
	path="${tmp%\",\"request_id\":0,\"error\":\"success\"\}}"
}

getvol() {
	tmp="$(printf '{ "command": ["get_property", "volume"] }\n'\
		| socat - /tmp/mpvsocket)"
	tmp="${tmp#{\"data\":}"
	printf '%s\n' "${tmp%.000000,\"request_id\":0,\"error\":\"success\"\}}"
}

download() {
	mkdir -p "$1"
	notify-send "Downloading '$path' to '$1'"
	if yt-dlp -f "ba" -x --embed-thumbnail --audio-format mp3\
			-o"%(title)s [%(id)s].%(ext)s" -P "$1" "$path"; then
		notify-send "'$path' successfuly downloaded to '$2'"
		return 0
	else
		notify-send "Error: '$path' couldn't be downloaded"
		return 1
	fi
}

addtofav() {
	case $path in
	*youtube.com*)
		download "$FAVDIR" ;;
	*)
		mkdir -p "$FAVDIR"
		if cp "$path" "$FAVDIR"; then
			notify-send "'$path' was copied to '$FAVDIR'"
			return 0
		else
			notify-send "'$path' couldn't be copied to '$FAVDIR'"
			return 1
		fi ;;
	esac
}

getthumbnail() {
	mkdir -p "$CACHE"
	case $path in
	*youtube.com*)
		yt-dlp --skip-download --force-overwrites --write-thumbnail\
			-o "$CACHE/thumbnail" "$path" > /dev/null 2>&1 &&\
			magick "$CACHE/thumbnail.webp" "$CACHE/thumbnail.jpg" ;;
	*)
		ffmpeg -y -i "$path" "$CACHE/thumbnail.jpg" 2> /dev/null ;;
	esac
}


main() {
	if ! pgrep -x music >/dev/null; then
		kill -35 "$(pidof dwmblocks)"
		herbe "Error: There is no music playing"\
			"Do you want to play some ?" &&
			music
		return 1
	fi

	cmd="${1:-$(printf '⏯️pause/play\n⏭️next\n⏮️prev\n📢volume\n🔳stop
⭐favorite\n⬇️download\n🖼️thumbnail' | dmenu -i -p "musiccmd")}"

	case "$cmd" in
	cycle|*pause|*play)
		printf 'cycle pause\n' | socat - /tmp/mpvsocket
		kill -35 "$(pidof dwmblocks)" ;;
	*next)
		printf 'playlist-next\n' | socat - /tmp/mpvsocket ;;
	*prev)
		printf 'playlist-prev\n' | socat - /tmp/mpvsocket ;;
	*volume)
		printf '{ "command": ["set_property", "volume", %s] }\n'\
			"${2:-$(true | dmenu -p "Current volume: $(getvol)")}"\
			| socat - /tmp/mpvsocket 1> /dev/null ;;
	*stop)
		printf 'stop\n' | socat - /tmp/mpvsocket
		kill -35 "$(pidof dwmblocks)" ;;
	*favorite|fav)
		getpath
		addtofav ;;
	*download)
		getpath
		download "$DLDIR" ;;
	*thumbnail)
		getpath
		if getthumbnail; then
			$IMG "$CACHE/thumbnail.jpg"
		else
			notify-send "Error: can't get thumbnail"
			return 1
		fi ;;
	path)
		getpath
		printf '%s\n' "$path" ;;
	vol)
		getvol ;;
	esac
}

main "$@"