Skip to content

Instantly share code, notes, and snippets.

@rikkimax
Last active March 5, 2022 23:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rikkimax/39b46f7ff3aa0c93b5cf to your computer and use it in GitHub Desktop.
Save rikkimax/39b46f7ff3aa0c93b5cf to your computer and use it in GitHub Desktop.
-- CONFIGDIR=http://www.videolan.org/support/faq.html#Config
-- Save to: $CONFIGDIR/lua/extensions/nowplaying.lua
require "io"
require "string"
function descriptor()
return {
title = "Now playing",
version = "2.0",
author = "Richard Andrew Cattermole",
url = 'https://gist.github.com/rikkimax/39b46f7ff3aa0c93b5cf',
shortdesc = "Saves to file the currently playing song",
description = "Saves the currently playing song name and artist to ~/now_playing.txt",
capabilities = {"input-listener", "meta-listener", "playing-listener"}
}
end
function activate()
-- this is where extension starts
-- for example activation of extension opens custom dialog box:
vlc.msg.dbg("[Now Playing] Activating")
handleItem()
end
function deactivate()
-- what should be done on deactivation of extension
vlc.msg.dbg("[Now Playing] Deactivating")
handleItem()
end
function input_changed()
-- related to capabilities={"input-listener"} in descriptor()
-- triggered by Start/Stop media input event
handleItem()
end
function playing_changed()
-- related to capabilities={"playing-listener"} in descriptor()
-- triggered by Pause/Play madia input event
handleItem()
end
function meta_changed()
-- related to capabilities={"meta-listener"} in descriptor()
-- triggered by available media input meta data?
handleItem()
end
function handleItem()
item = (vlc.player and vlc.player.item()) or vlc.item or vlc.input.item()
-- handle Windows and posix.
-- on Windows vlc.config.homedir returns Documents directory not the /actual/ home directory
local homeDir = os.getenv("USERPROFILE") or vlc.config.homedir()
local theFile = homeDir .. "/now_playing.txt"
vlc.msg.dbg("[Now Playing] " .. theFile)
file=io.open(theFile, "w+")
if not (item == nil) and ((vlc.player and vlc.player.is_playing()) or vlc.input.is_playing()) then
metas=item:metas()
artist=trim(metas.artist)
title=trim(metas.title)
local text=title .. " - " .. artist
vlc.msg.dbg("[Now Playing] " .. text)
file:write(text)
else
vlc.msg.dbg("[Now Playing] no input item playing")
file:write("No song currently playing")
end
file:close()
end
function trim(s)
local _,i1 = string.find(s,'^%s*')
local i2 = string.find(s,'%s*$')
return string.sub(s,i1+1,i2-1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment