Is it possible to generate a brief notification every time mpv starts a playback? Maybe through notify-send?


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, "'", "'\"'\"'").."'"
function do_notify(a,b)
   local command = ("notify-send -a mpv -- %s %s"):format(a:shellescape(), 
   if command ~= lastcommand then
      lastcommand = command
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)
   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)

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.

  • yeah, i could't make notify to work either, that's why i'm asking. I tried your script but i got this error: [mynotify] Lua error: /home/user/.config/mpv/scripts/mynotify.lua:5: bad argument #1 to 'gsub' (string expected, got nil) – kurokirasama Jul 13 '18 at 23:49
  • As I said, it is greatly simplified. For example, your metadata may not have an artist or title, so you will get an error. I've changed the answer to not crash in this case, or if the keyword is in uppercase, but you may then get notifications with no text. You need to add whatever metadata keys are of interest to you, and exist in your files. I just had some simple id3 tags for mp3 audio files. – meuh Jul 14 '18 at 9:48
  • thanks!, I don't think my files have metadata, because they are all aac files. is it possible to modify your script to show only the filenames? – kurokirasama Jul 14 '18 at 23:05
  • I've updated the answer to cope with no metadata and some simple filename parsing. You should probably accept this answer and move on to a new question for help on lua. – meuh Jul 15 '18 at 15:01

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:

            ss #1

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.

Exploring D-Bus

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 mpv.

$ sudo dbus-monitor --system

NOTE: mpv is a fork of mplayer2 and I know mplayer2 does utilize D-Bus so I would expect mpv to do the same.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.