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
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ Features
--------

* shows artist, title and album name (as far as detected by mpv)
* extracts cover art using ffmpeg
* extracts cover art using ffmpeg
* writes icon, artist, album, title to temporary files (useful for OBS)

Requirements
------------

* [mpv](http://mpv.io) (>= 0.3.6)
* [Lua](http://lua.org) (>= 5.2)
* `ffmpeg` from [https://www.ffmpeg.org/](https://www.ffmpeg.org/)
* `ffmpegthumbnailer` from [ffmpegthumbnailer](https://github.com/dirkvdb/ffmpegthumbnailer) `(optional only for video thumbnails)`
* `notify-send` from [libnotify](https://github.com/GNOME/libnotify)
* `convert` from [ImageMagick](http://www.imagemagick.org)

Install mpv, lua, ffmpeg, libnotify and ImageMagick packages
Install mpv, lua, ffmpeg, ffmpegthumbnailer(optionl), libnotify and ImageMagick packages

Installation
------------
Expand All @@ -32,6 +34,6 @@ and mpv will find it. Optionally, you can add it to mpv's command line:
License
-------

mpv-notify was originally written by Roland Hieber <rohieb at rohieb.name>. I have simply
refactored it according to my needs. You may use it under the terms of the
mpv-notify was originally written by Roland Hieber <rohieb at rohieb.name>. I have simply
refactored it according to my needs. You may use it under the terms of the
[MIT license](http://choosealicense.com/licenses/mit/).
209 changes: 153 additions & 56 deletions notify.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,115 +27,198 @@
-------------------------------------------------------------------------------

function print_debug(s)
print("DEBUG: " .. s) -- comment out for no debug info
return true
print("INFO: " .. 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;
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
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, "'", "'\"'\"'").."'"
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;
local s, _ = string.gsub(str, "([^A-Za-z0-9_.-])",
function(c)
return ("%02x"):format(c:byte())
end)
return s
end

-- write the string to a file
function string.dumpf(str, path)
local file = io.open(path, "w")
file:write(str)
file:close()
end

-- check if a file exists ignoring case
function find_lowercase_file(path, filename)
local command = ("find %s -iname %s -type f"):format(string.shellescape(path), string.shellescape(filename), "r")
local pfile = io.popen(command)
local output = pfile:read()
pfile:close()
return output
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
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)
local ffmpeg_cmd_a = ("ffmpeg -loglevel -8 -vsync 2 -y -i %s %s > /dev/null"):format(
string.shellescape(audiofile), string.shellescape(imagedst)
)
-- print_debug("executing " .. ffmpeg_cmd)
if os.execute(ffmpeg_cmd) then

-- print_debug("executing " .. ffmpeg_cmd_a)
if os.execute(ffmpeg_cmd_a) then
return true
end

return false
end

-- extract image from video file
function extracted_image_from_videofile (audiofile, imagedst)
local ffmpeg_cmd_v = ("ffmpegthumbnailer -i %s -o %s 2>&1 >/dev/null"):format(
string.shellescape(audiofile), string.shellescape(imagedst)
)

-- print_debug("executing " .. ffmpeg_cmd_v)
if os.execute(ffmpeg_cmd_v) 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 ""
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"
-- return path without .. or .
function os.realpath(path)
local pfile = io.popen(("realpath %s"):format(string.shellescape(path)), "r")
local output = pfile:read()
pfile:close()
return output
end

-- copy file
function os.copy(source, dest)
local command = ("cp %s %s"):format(string.shellescape(source), string.shellescape(dest))
os.execute(command)
end

-- look for a list of possible cover art images in the same folder as the file
-- @param path absolute file path of currently played file, or nil if no match
function coppied_directory_cover(path, imagedst)
-- print_debug("get_folder_cover_art: file path is " .. path)
local cover_extensions = { "png", "jpg", "jpeg", "gif" }
local cover_names = { "cover", "folder", "front", "back", "insert" }

local dir = os.realpath(string.match(path, "^(.*)/[^/]+$"))

for _, name in pairs(cover_names) do
for _, ext in pairs(cover_extensions) do
-- print_debug("get_folder_cover_art: trying " .. cover_path)
local cover_name = name .. "." .. ext
local actual_name = find_lowercase_file(dir, cover_name)
if actual_name ~= nil then
os.copy(actual_name, imagedst)
return true
end
end
end
return false
end

COVER_ART_PATH = "/tmp/mpv.covert_art.jpg"
ICON_PATH = "/tmp/mpv.icon.jpg"
ARTIST_PATH = "/tmp/mpv.artist.txt"
ALBUM_PATH = "/tmp/mpv.album.txt"
TITLE_PATH = "/tmp/mpv.title.txt"

function notify_current_track()
os.remove(COVER_ART_PATH)
-- 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
return
end

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

-- print_debug("notify_current_track(): -> extracted metadata:")
-- print_debug("artist: " .. artist)
-- print_debug("album: " .. album)
-- 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")
-- absolute filename of currently playing audio file
local abs_filename = os.getenv("PWD") .. "/" .. mp.get_property_native("path")
local bad_string = os.getenv("PWD") .. "//"
local abs_filename = string.gsub(abs_filename,bad_string,"/")
-- 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 coppied_directory_cover(abs_filename, COVER_ART_PATH) then
if scaled_image(COVER_ART_PATH, ICON_PATH) then
params = "-i " .. ICON_PATH
end
elseif extracted_image_from_audiofile(abs_filename, COVER_ART_PATH) then
if scaled_image(COVER_ART_PATH, ICON_PATH) then
params = "-i " .. ICON_PATH
end
elseif extracted_image_from_videofile(abs_filename, COVER_ART_PATH) then
params = "-i "..COVER_ART_PATH
end

-- form notification summary
Expand All @@ -149,27 +232,41 @@ function notify_current_track()
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))
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 %s -- %s %s"):format(params, summary, body)
-- print_debug("command: " .. command)
os.execute(command)
local command = ("notify-send -a mpv %s -- %s %s"):format(params, summary, body)
-- print_debug("command: " .. command)
os.execute(command)


end

-- notify hook
function notify_metadata_updated(name, data)
notify_current_track()
notify_current_track()
end

-- empty or remove files so the clients know nothing is playing
function cleanup(event)
local text_files = {ARTIST_PATH, ALBUM_PATH, TITLE_PATH}
local emptystr = ""
for _, path in ipairs(text_files) do
emptystr:dumpf(path)
end
local binary_files = {ICON_PATH, COVER_ART_PATH}
for _, path in ipairs(binary_files) do
os.remove(path)
end
end


-- insert main() here

mp.register_event("file-loaded", notify_current_track)
mp.register_event("shutdown", cleanup)
-- mp.observe_property("metadata", nil, notify_metadata_updated)