summaryrefslogtreecommitdiff
path: root/mpc-notify
blob: 9c74329e6e2213d05889fbb29009f0783fccc84e (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
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
#!/bin/sh -e
# Based on:
# 	https://wiki.archlinux.org/title/Ncmpcpp#With_album_art

# Sends notification of currently playing MPD music with album cover art.
# Dependencies:
# 	mpc
# 	ffmpeg
# 	notify-send
# 	and my other script, notify-replace.
# 	recommended: a home directory setup with xdg-user-dirs (for finding your
# 	music directory
# For notification on song change:
# 	Assign execute_on_song_change to this script in ncmpcpp's config file.
# For notification on media keys:
# 	Bind media key to a call to mpc with the approprite command.

# exit early if we're in tty
if [ -z "$WAYLAND_DISPLAY" ] && [ -z "$DISPLAY" ]; then
	exit 2
fi
command="$1"

# set defaults
# FEATURE: make width and height configurable through flags
preview_width=64
preview_height=64
default_song_icon="media-playback-start"

# find xdg music directory
. "${XDG_CONFIG_HOME:-$HOME/.config}/user-dirs.dirs" 2>/dev/null || true
xdg_music_dir="${XDG_MUSIC_DIR:-$HOME/Music}"

# find mpc config directory
mpc_config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/mpc"
[ -d "$mpc_config_dir" ] || mkdir -p "$mpc_config_dir"

# issue an mpc command based on arguments and assemble notification message
use_preview=0
case "$command" in
	"")
		# use defaults
		song_head="Now Playing"
		use_preview=1
		;;
	"toggle")
		mpc -q toggle
		state="$(mpc status %state%)"
		if [ "$state" = "playing" ]; then
			song_head="Now Playing"
			use_preview=1
		elif [ "$state" = "paused" ]; then
			song_head="Paused"
			song_icon="media-playback-pause"
		fi
		;;
	"shuffle")
		mpc -q shuffle
		song_head="Shuffled Playlist"
		song_icon="media-playlist-shuffle"
		;;
	"single")
		mpc -q single
		state="$(mpc status %single%)"
		if [ "$state" = "on" ]; then
			song_head="Single Mode: On"
		elif [ "$state" = "once" ]; then
			song_head="Single Mode: Once"
		elif [ "$state" = "off" ]; then
			song_head="Single Mode: Off"
		fi
		song_icon="multimedia-player"
		;;
	"random")
		mpc -q random
		state="$(mpc status %random%)"
		if [ "$state" = "on" ]; then
			song_head="Random Mode: On"
		elif [ "$state" = "off" ]; then
			song_head="Random Mode: Off"
		fi
		song_icon="media-eject"
		;;
	"repeat")
		mpc -q repeat
		state="$(mpc status %repeat%)"
		if [ "$state" = "on" ]; then
			song_head="Repeat Mode: On"
		elif [ "$state" = "off" ]; then
			song_head="Repeat Mode: Off"
		fi
		song_icon="media-playlist-repeat"
		;;
	*)
		mpc -q "$command"
		# Don't notify if mpc changed song because ncmpcpp would notify on
		# song change.
		[ "$command" = "next" ] || [ "$command" = "prev" ] && exit
		;;
esac
song_body="$(mpc --format '[%title%[ \n%artist% - %album%]]|[%file%]' current)"

# use album preview for song icon
if [ "$use_preview" -eq 1 ]; then
	preview_dir="$mpc_config_dir/previews"
	[ -d "$preview_dir" ] || mkdir -p "$preview_dir"

	# get preview filename
	song_filename="$(mpc --format "${xdg_music_dir}/%file%" current)"
	song_album="$(mpc --format %album% current)"
	if [ -z "$song_album" ]; then
		# fallback to default for empty album name
		song_icon="$default_song_icon"
	else
		preview_path="${preview_dir}/$(echo "$song_album" | base64).png"
		if [ -f "$preview_path" ]; then
			# use cached preview
			song_icon="$preview_path"
		elif ffmpeg -y -i "$song_filename" -an -vf \
			scale="${preview_width}:${preview_height}" \
			"$preview_path" >/dev/null 2>&1;
		then
			# or use generated preview
			song_icon="$preview_path"
		else
			song_dirname=$(dirname "$song_filename")
			art_filename="$(find "$song_dirname" -type f |
				grep -Esm 1 '.*\.(jpg|png)')"
			if [ $? -eq 0 ]; then
				# or use scaled cover art from directory
				ffmpeg -y -i "$art_filename" -an -vf \
					scale="${preview_width}:${preview_height}" \
					"$preview_path" >/dev/null 2>&1;
				song_icon="$preview_path"
			else
				# or fallback to default icon
				song_icon="$default_song_icon"
			fi
		fi
	fi
fi

notify-replace 'mpc-notify' "$song_head" "$song_body" -i "$song_icon" -u low