Is it possible to generate a brief notification every time mpv starts a playback? Maybe through
mpv can run lua user scripts, some of which are listed here. One of these, notify will generate a sophisticated
notify-send. It has a few dependencies, and I wasn't able to get it to work in my setup, but the followed greatly simplified code worked for me. Place this file in
~/.config/mpv/scripts/mynotify.lua (create the directory if needed), and run
mpv as usual. You should see a notification when the artist or title changes.
-- based on https://github.com/rohieb/mpv-notify -- https://unix.stackexchange.com/a/455198/119298 lastcommand = nil function string.shellescape(str) return "'"..string.gsub(str, "'", "'\"'\"'").."'" end function do_notify(a,b) local command = ("notify-send -a mpv -- %s %s"):format(a:shellescape(), b:shellescape()) if command ~= lastcommand then os.execute(command) lastcommand = command end end function notify_current_track() data = mp.get_property_native("metadata") if data then local artist = (data["ARTIST"] or data["artist"] or " ") local title = (data["TITLE"] or data["title"] or " ") if artist..title~=" " then do_notify(artist, title) return end end local data = mp.get_property("path") if data then local file = data:gsub("^.-([^/]+)$","%1") file = file:gsub("%....$","") -- delete 3 char suffix local dir = data:gsub("^.-([^/]+)/[^/]*$","%1") do_notify(dir, file) end end mp.register_event("file-loaded", notify_current_track)
This updated version waits for events that are sent when a new file is ready to be played. It tries to find the metadata and extract the artist and title from it. If this is empty, it then gets the current filename (
"path") and splits out the last part after
/ to get a filename, from which it removes any trailing 3 character suffix. It tries to find the last directory part of the filename, and uses these 2 items in the notification. If your directories are structured with say, artist/albumname/tracktitle.aac, you might like to change this with a more appropriate pattern match and extraction. See the lua section on patterns.
mpv has an event hook library,
libmpv. There's a issue asking for essentially the same thing here titled: libmpv event hooking in C# #3810.
There's a comment on that issue with a screenshot:
From looking this thread it looks like
mpv is at least somewhat capable of doing what you want. I believe you'll have to write some code though to implement what you want.
The other way to approach this on the off chance that
mpv is already sending message via D-Bus is to use the cli tool
dbus-monitor. With this tool you'll be able to catch all the events being logged, and perhaps you'll be able to catch ones coming from
$ sudo dbus-monitor --system
mpv is a fork of
mplayer2 and I know
mplayer2 does utilize D-Bus so I would expect
mpv to do the same.