Skip to content

Instantly share code, notes, and snippets.

@jonniek
Last active May 1, 2019 09:29
Show Gist options
  • Save jonniek/483f591bb2432051346a8e564595896e to your computer and use it in GitHub Desktop.
Save jonniek/483f591bb2432051346a8e564595896e to your computer and use it in GitHub Desktop.
mpv url resolver
-- resolve_ytdl helper command: https://gist.github.com/jonniek/4a9d850d224bbd704b71f9ec882eeef6
-- copy it and add it to some path dir and update the below path to match
local resolver = '/usr/bin/resolve_ytdl'
local tmpfile = '/tmp/resolvedtitle'
local utils = require("mp.utils")
local processed_urls = {}
local resolved_urls = {}
mp.observe_property('playlist-count', "number", function()
local length = mp.get_property_number('playlist-count', 0)
if length < 2 then return end
local i=0
while i < length do
local filename = mp.get_property('playlist/'..i..'/filename')
if filename:match('^https?://') and not processed_urls[filename] then
processed_urls[filename] = true
utils.subprocess_detached({ args= {resolver, filename, tmpfile} })
end
i=i+1
end
end)
mp.add_periodic_timer(1, function()
local file, e = io.open(tmpfile, 'r')
if file then
for line in file:lines() do
local url, title = line:match("^(https?://.-)%s(.+)$")
if not resolved_urls[url] and url and title then
resolved_urls[url] = true
mp.commandv("script-message", "playlistmanager", "addurl", url, title)
end
end
file:close()
end
end)
mp.register_event('shutdown', function()
os.remove(tmpfile)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment