diff --git a/README.md b/README.md index a8bd46a..055458b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/notify.lua b/notify.lua index 98cbcf1..ad5b6d7 100644 --- a/notify.lua +++ b/notify.lua @@ -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. ------------------------------------------------------------------------------- @@ -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) @@ -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:" @@ -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)