Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Install mpv, lua, ffmpeg, libnotify and ImageMagick packages
Installation
------------

Just drop `notify.lua` into the folder `~/.config/scripts/lua` (create it when neccessary),
Just drop `notify.lua` into the folder `~/.config/mpv/scripts/` (create it when neccessary),
and mpv will find it. Optionally, you can add it to mpv's command line:

mpv --lua=/path/to/notify.lua <files and options>
Expand Down
32 changes: 24 additions & 8 deletions notify.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ function string.safe_filename(str)
return s;
end

-- check if a file exists and is readable
function file_exists(name)
local f=io.open(name, "r")
if f~=nil then io.close(f) return true else return false end
end

-------------------------------------------------------------------------------
-- here we go.
-------------------------------------------------------------------------------
Expand Down Expand Up @@ -105,6 +111,7 @@ 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)
Expand All @@ -127,16 +134,20 @@ function notify_current_track()
-- 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)
local abs_filename = mp.get_property_native("path")
if not abs_filename:match("^%/") then
abs_filename = os.getenv("PWD") .. "/" .. abs_filename
end

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
if file_exists(COVER_ART_PATH) and scaled_image(COVER_ART_PATH, ICON_PATH) then
params = "-i " .. ICON_PATH
else
params = "-i 'audio-x-generic'"
end
end
end

-- form notification summary
summary_str ="Now playing:"
Expand All @@ -157,19 +168,24 @@ function notify_current_track()

body = string.shellescape(body_str)

local command = ("notify-send -a mpv %s -- %s %s"):format(params, summary, body)
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()
function notify_pause_updated(name, value)
if value == false then
notify_current_track()
else
local command = "notify-send -a mpv -t 5000 -i audio-x-generic -- \"Music Paused\""
os.execute(command)
end
end


-- insert main() here

mp.register_event("file-loaded", notify_current_track)
-- mp.observe_property("metadata", nil, notify_metadata_updated)
mp.observe_property("pause", "bool", notify_pause_updated)