├── screenshot.jpg ├── overlay.nix ├── default.nix ├── COPYING.txt ├── package.nix ├── README.rst └── notify-send.lua /screenshot.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Parranoh/mpv-notify-send/HEAD/screenshot.jpg -------------------------------------------------------------------------------- /overlay.nix: -------------------------------------------------------------------------------- 1 | self: super: { 2 | mpv-notify-send = self.callPackage ./package.nix {}; 3 | } 4 | -------------------------------------------------------------------------------- /default.nix: -------------------------------------------------------------------------------- 1 | { pkgs ? }: 2 | 3 | (import pkgs { 4 | overlays = [ (import ./overlay.nix) ]; 5 | }).mpv-notify-send 6 | -------------------------------------------------------------------------------- /COPYING.txt: -------------------------------------------------------------------------------- 1 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 2 | Version 2, December 2004 3 | 4 | Copyright (C) 2004 Sam Hocevar 5 | 6 | Everyone is permitted to copy and distribute verbatim or modified 7 | copies of this license document, and changing it is allowed as long 8 | as the name is changed. 9 | 10 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 11 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 12 | 13 | 0. You just DO WHAT THE FUCK YOU WANT TO. 14 | -------------------------------------------------------------------------------- /package.nix: -------------------------------------------------------------------------------- 1 | { stdenv 2 | , libnotify 3 | }: 4 | 5 | stdenv.mkDerivation { 6 | name = "mpv-notify-send"; 7 | 8 | src = builtins.filterSource 9 | (path: type: builtins.baseNameOf path == "notify-send.lua") 10 | ./.; 11 | 12 | patchPhase = '' 13 | substituteInPlace notify-send.lua \ 14 | --replace '"notify-send"' '"${libnotify}/bin/notify-send"' 15 | ''; 16 | 17 | installPhase = '' 18 | mkdir -p $out/share/mpv/scripts 19 | cp notify-send.lua $out/share/mpv/scripts 20 | ''; 21 | 22 | meta = with stdenv.lib; { 23 | homepage = "https://github.com/Parranoh/mpv-notify-send"; 24 | description = "A Lua script for mpv to send notifications with notify-send(1)"; 25 | license = licenses.wtfpl; 26 | platforms = platforms.all; 27 | maintainers = with maintainers; [ emily ]; 28 | }; 29 | } 30 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | =============== 2 | mpv-notify-send 3 | =============== 4 | 5 | This is a simple Lua script for mpv_ 6 | that sends notifications about the playing media. 7 | It uses ``notify-send(1)`` and should therefore work 8 | with any libnotify-compatible notifications daemon. 9 | It reports title, album and year if available, 10 | and shows a cover image if present 11 | in the same directory as the media file 12 | (embedded images aren’t currently supported). 13 | 14 | .. _mpv: https://mpv.io/ 15 | 16 | Screenshot 17 | ========== 18 | 19 | .. image:: screenshot.jpg 20 | :alt: 21 | A screenshot of GNOME displaying a notification 22 | with title “Guilford Avenue Bridge” 23 | and body “Dan Deacon — America (2012)” 24 | as mpv plays the corresponding track. 25 | :width: 1125 26 | :height: 750 27 | 28 | Usage 29 | ===== 30 | 31 | Either copy/link ``notify-send.lua`` 32 | into ``$XDG_CONFIG_HOME/mpv/scripts``, 33 | or set ``script=/path/to/notify-send.lua`` in ``mpv.conf`` 34 | or on the command line. 35 | 36 | If you use `Nix(OS)`_ then you can install ``default.nix`` directly 37 | or use the included ``overlay.nix`` in your ``configuration.nix``: 38 | 39 | .. code:: nix 40 | 41 | { pkgs, ... }: 42 | 43 | { 44 | nixpkgs.overlays = [ 45 | # ... 46 | (import /path/to/mpv-notify-send/overlay.nix) 47 | ]; 48 | 49 | # ... 50 | 51 | environment.systemPackages = with pkgs; [ 52 | # ... 53 | mpv 54 | mpv-notify-send 55 | ]; 56 | 57 | environment.pathsToLink = ["/share/mpv"]; 58 | } 59 | 60 | You can then link ``/share/mpv/scripts/notify-send.lua`` 61 | (e.g. ``/run/current-system/sw/share/...`` on NixOS, 62 | ``$HOME/.nix-profile/share/...`` when using ``nix-env``) 63 | into ``$XDG_CONFIG_HOME/mpv/scripts``. 64 | 65 | .. _Nix(OS): https://nixos.org/ 66 | 67 | Licence 68 | ======= 69 | 70 | mpv-notify-send is licensed under the WTFPL_. 71 | 72 | .. _WTFPL: COPYING.txt 73 | -------------------------------------------------------------------------------- /notify-send.lua: -------------------------------------------------------------------------------- 1 | local utils = require "mp.utils" 2 | 3 | local cover_filenames = { "cover.png", "cover.jpg", "cover.jpeg", 4 | "folder.jpg", "folder.png", "folder.jpeg", 5 | "AlbumArtwork.png", "AlbumArtwork.jpg", "AlbumArtwork.jpeg" } 6 | 7 | function notify(summary, body, options) 8 | local option_args = {} 9 | for key, value in pairs(options or {}) do 10 | table.insert(option_args, string.format("--%s=%s", key, value)) 11 | end 12 | return mp.command_native({ 13 | "run", "notify-send", 14 | summary, body, 15 | unpack(option_args) 16 | }) 17 | end 18 | 19 | function escape_pango_markup(str) 20 | return string.gsub(str, "([\"'<>&])", function (char) 21 | return string.format("&#%d;", string.byte(char)) 22 | end) 23 | end 24 | 25 | function notify_media(title, origin, thumbnail) 26 | return notify(escape_pango_markup(title), origin, { 27 | urgency = "low", 28 | ["app-name"] = "mpv", 29 | hint = "string:desktop-entry:mpv", 30 | icon = thumbnail or "mpv", 31 | }) 32 | end 33 | 34 | function file_exists(path) 35 | local info, _ = utils.file_info(path) 36 | return info ~= nil 37 | end 38 | 39 | function find_cover(dir) 40 | -- make dir an absolute path 41 | if dir[1] ~= "/" then 42 | dir = utils.join_path(utils.getcwd(), dir) 43 | end 44 | 45 | for _, file in ipairs(cover_filenames) do 46 | local path = utils.join_path(dir, file) 47 | if file_exists(path) then 48 | return path 49 | end 50 | end 51 | 52 | return nil 53 | end 54 | 55 | function first_upper(str) 56 | return (string.gsub(string.gsub(str, "^%l", string.upper), "_%l", string.upper)) 57 | end 58 | 59 | function notify_current_media() 60 | local path = mp.get_property_native("path") 61 | 62 | local dir, file = utils.split_path(path) 63 | 64 | -- TODO: handle embedded covers and videos? 65 | -- potential options: mpv's take_screenshot, ffprobe/ffmpeg, ... 66 | -- hooking off existing desktop thumbnails would be good too 67 | local thumbnail = find_cover(dir) 68 | 69 | local title = mp.get_property_native("media-title") or file 70 | local origin = dir 71 | 72 | local metadata = mp.get_property_native("metadata") 73 | if metadata then 74 | function tag(name) 75 | return metadata[string.upper(name)] or metadata[first_upper(name)] or metadata[name] 76 | end 77 | 78 | title = tag("title") or title 79 | origin = tag("artist_credit") or tag("artist") or "" 80 | 81 | local album = tag("album") 82 | if album then 83 | origin = string.format("%s — %s", origin, album) 84 | end 85 | 86 | local year = tag("original_year") or tag("year") 87 | if year then 88 | origin = string.format("%s (%s)", origin, year) 89 | end 90 | end 91 | 92 | return notify_media(title, origin, thumbnail) 93 | end 94 | 95 | mp.register_event("file-loaded", notify_current_media) 96 | --------------------------------------------------------------------------------