I've started making a lua script that pulls a song's Title and Artist from the Now Playing metadata which is provided in a radio streams, I want to then lookup that song to grab it's album's art work and then update it in VLC. Which I managed to do and sets the metadata but the image doesn't actually show unless, I then do "Download cover art" option on the artwork, which then it loads the image. Is there a way to instruct VLC to download the artwork after it's been set. I've been scanning through lua examples on the vlc github and also looked at https://vlc.verg.ca/ which someone has nicely documented documented it. Is there something I'm not seeing, or is this just not possible? At least without a code change to add a function to do the download/refresh?
Love to hear back
Code: Select all
require "simplexml"
json = require("dkjson")
local currentSong = ""
function descriptor()
return {
title = "Now Playing Album Art",
version = "1.0",
author = "FingerlessGloves",
shortdesc = "Displays album art for the currently playing track",
description = "Fetches album art using metadata from a radio stream and displays it in VLC.",
capabilities = { "input-listener" }
}
end
function activate()
vlc.msg.dbg("[AlbumArt] Extension Activated")
fetch_album_art()
end
function deactivate()
vlc.msg.dbg("[AlbumArt] Extension Deactivated")
end
function input_changed()
fetch_album_art()
end
function meta_changed()
input_changed()
return
end
function set_album(url)
local item = vlc.input.item()
url = url or "https://imgs.xkcd.com/comics/rotary_tool.png"
item:set_meta("artwork_url", url)
end
function fetch_album_art()
local item = vlc.input.item()
if not item then
vlc.msg.dbg("[AlbumArt] No track currently playing.")
set_album()
return
end
local meta = item:metas()
if not meta or not meta["now_playing"] then
vlc.msg.dbg("[AlbumArt] No 'Now Playing' metadata found.")
set_album()
return
end
local now_playing = meta["now_playing"]
if currentSong == now_playing then
return
end
currentSong = now_playing
if now_playing == "" then
vlc.msg.dbg("now_playing currently blank")
set_album()
return
end
local track, artist = now_playing:match("(.+) %- (.+)")
if not artist or not track then
vlc.msg.dbg("[AlbumArt] Could not parse artist and track from metadata.")
vlc.msg.dbg("got: " .. now_playing)
set_album()
return
end
vlc.msg.dbg("[AlbumArt] Fetching artwork for: " .. artist .. " - " .. track)
local artwork_url = search_album_art(artist, track)
if artwork_url then
vlc.msg.dbg("[AlbumArt] Artwork URL: " .. artwork_url)
set_album(artwork_url)
else
vlc.msg.dbg("[AlbumArt] No artwork found.")
set_album()
end
end
function search_album_art(artist, track)
local url =
"https://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=mykey&artist="
.. vlc.strings.encode_uri_component(artist)
.. "&track="
.. vlc.strings.encode_uri_component(track)
.. "&format=json"
local response = get_http_content(url)
if not response then return nil end
local data, _, err = json.decode(response)
if err or not data or not data.track or not data.track.album or not data.track.album.image then
return nil
end
for _, img in ipairs(data.track.album.image) do
if img.size == "extralarge" then
return img["#text"]
end
end
return nil
end
function get_http_content(url)
local stream = vlc.stream(url)
if not stream then return nil end
local content = ""
local line = stream:read(1024)
while line do
content = content .. line
line = stream:read(1024)
end
return content
end