Lua script for mpv notifications in Mabox

Just want to share a modified Lua script I retouched for having fun with our mpv notifications (displayed by default xfce4-notificacions in mabox) and have them all coming up on screen whenever you use the media player (ytfzf, pyradio, stremio, or just mpv itself). There are no extra packages needed so far (all them are installed by default in mabox).
You might need to check your config file in mpv (~/home/.config/mpv/mpv.conf) so as to add an extra line saying [notify-lua] and save the changes. Also in ~/home/.config/mpv/scripts you need create a folder named as scripts if you don’t have it and place this notify.lua script in there. Then restart and play pyradio or mpv player to play any streaming or video file to check it up. Good luck.



notify.lua script is below in 177 lines to copy and add as a new file in scripts(created) folder in mpv

-- notify.lua -- Desktop notifications for mpv.
-- Just put this file into your ~/.config/mpv/scripts folder and mpv will find it.
--
-- Copyright (c) 2014 Roland Hieber
-- (Also some minor edits from Arindam Das)
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
-- I tweaked a bit this 2014 notify-mpv.lua script to use it in Maboxlinux to 
-- integrate with Pyradio app to play streaming & metadata, added some bucle stop
-- to interrupt libnotify send continuous notifications. Nick0bre Chile 2022.
-------------------------------------------------------------------------------
-- helper functions
-------------------------------------------------------------------------------

function print_debug(s)
	print("DEBUG: " .. s) -- comment out for no debug info
	return true
end

-- url-escape a string, per RFC 2396, Section 2
function string.urlescape(str)
	local s, c = string.gsub(str, "([^A-Za-z0-9_.!~*'()/-])",
		function(c)
			return ("%%%02x"):format(c:byte())
		end)
	return s;
end

-- escape string for html
function string.htmlescape(str)
	local str = string.gsub(str, "<", "&lt;")
	str = string.gsub(str, ">", "&gt;")
	str = string.gsub(str, "&", "&amp;")
	str = string.gsub(str, "\"", "&quot;")
	str = string.gsub(str, "'", "&apos;")
	return str
end

-- escape string for shell inclusion
function string.shellescape(str)
	return "'"..string.gsub(str, "'", "'\"'\"'").."'"
end

-- converts string to a valid filename on most (modern) filesystems
function string.safe_filename(str)
	local s, _ = string.gsub(str, "([^A-Za-z0-9_.-])",
		function(c)
			return ("%02x"):format(c:byte())
		end)
	return s;
end

-------------------------------------------------------------------------------
-- here we go.
-------------------------------------------------------------------------------

-- scale an image file
-- @return boolean of success
function scaled_image(src, dst)
	local convert_cmd = ("convert -scale x64 -- %s %s"):format(
		string.shellescape(src), string.shellescape(dst))
	-- print_debug("executing " .. convert_cmd)
	if os.execute(convert_cmd) then
		return true
	end
	return false
end

-- extract image from audio file
function extracted_image_from_audiofile (audiofile, imagedst)
  local ffmpeg_cmd = ("ffmpeg -loglevel -8 -vsync 2 -i %s %s > /dev/null"):format(
    string.shellescape(audiofile), string.shellescape(imagedst)
  )
  -- print_debug("executing " .. ffmpeg_cmd)
  if os.execute(ffmpeg_cmd) then
    return true
  end

  return false
end

function get_value(data, keys)
	for _,v in pairs(keys) do
		if data[v] and string.len(data[v]) > 0 then
			return data[v]
		end
	end
	return ""
end

COVER_ART_PATH = "/tmp/covert_art.jpg"
ICON_PATH = "/tmp/icon.jpg"

function notify_current_track()
  os.remove(COVER_ART_PATH)
  os.remove(ICON_PATH)

  local metadata = mp.get_property_native("metadata")
	
  -- track doesn't contain metadata
  if not metadata then
		return
	end

  -- we try to fetch metadata values using all possible keys
	local artist = get_value(metadata, {"artist", "ARTIST"})
  local album  = get_value(metadata, {"album", "ALBUM"})
	local title  = get_value(metadata, {"title", "TITLE", "icy-title"})

	-- print_debug("notify_current_track(): -> extracted metadata:")
	-- print_debug("artist: " .. artist)
	-- print_debug("album: " .. album)	
  -- print_debug("title: " .. title)

	-- absolute filename of currently playing audio file
	local abs_filename = os.getenv("PWD") .. "/" .. mp.get_property_native("path")
  -- print_debug(abs_filename)

  params = ""
  -- extract cover art: set it as icon in notification params
  if extracted_image_from_audiofile(abs_filename, COVER_ART_PATH) then
    if scaled_image(COVER_ART_PATH, ICON_PATH) then
      params = "-i " .. ICON_PATH
    end
  end

  -- form notification summary
  summary_str ="Now playing:"
  if (string.len(artist) > 0) then
    summary_str = string.htmlescape(artist)
  end
  summary = string.shellescape(summary_str)

  body_str = mp.get_property_native("filename")
  if (string.len(title) > 0) then
    if (string.len(album) > 0) then
      body_str = ("%s<br /><i>%s</i>"):format(
        string.htmlescape(title), string.htmlescape(album))
    else
      body_str = string.htmlescape(title)
    end
  end

  body = string.shellescape(body_str)

	local command = ("notify-send -a mpv -t 5000 %s -- %s %s"):format(params, summary, body)
	-- print_debug("command: " .. command)
	os.execute(command)

  
end

function notify_metadata_updated(name, data)
	notify_current_track()
end


-- insert main() here

mp.register_event("file-loaded", notify_current_track)
mp.observe_property("metadata", nil, notify_metadata_updated)
4 Likes

Hello, Ben Chile!
It’s a good idea! I like it.
Guillaume

1 Like

Glad that you add some fun into your system, or some extra ricing as they call it :face_with_hand_over_mouth: If you have any questions let me know. You can meet me/us in mabox Telegram channel ( we communicate mostly in English/Spanish) at some time during the day.
This is the link to join us: Telegram: Contact @maboxlinux
Thanks.

1 Like

Thank you for your work. Something new and different.
:grinning: :+1:

1 Like

Thanks, for this indication!

1 Like