Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- cache for alredy resolved titles and
- -- titles that could not be resolved (filenames)
- -- filename => title
- local title_cache = {}
- -- poor man's mutex
- -- filename => true / false
- local title_resolving = {}
- -- resolve title using youtube-dl
- -- ... and abusig gnu coreutils for what could be done in lua
- function resolve_title(filename)
- -- workaround about poor man's mutex
- -- ensures that the actual check won't happen twice
- if title_cache[filename] then
- return title_cache[filename]
- end
- title_resolving[filename] = true
- local res = io.popen('youtube-dl --flat-playlist -e '.. filename .. ' | head -n 1'):read("*all")
- if res and res ~= '' then
- title_cache[filename] = res
- else
- title_cache[filename] = filename
- end
- title_resolving[filename] = false
- end
- function get_playlist()
- local pos = mp.get_property_number('playlist-pos', 0) + 1
- local count, limlist = limited_list('playlist', pos)
- if count == 0 then
- return 'Empty playlist.'
- end
- local message = string.format('Playlist [%d/%d]:\n', pos, count)
- for i, v in ipairs(limlist) do
- local title = v.title
- if not title then
- -- try printing title from the cache
- if title_cache[v.filename] then
- title = title_cache[v.filename]
- -- if not, check if it's resolving in the background
- elseif title_resolving[v.filename] then
- title = v.filename
- -- if not, try to resolve it if filename looks like a network stream
- elseif string.match(v.filename, "https?://") then
- -- doesn't work for whatever reason
- --mp.add_timeout(0.05, resolve_title(v.filename))
- resolve_title(v.filename)
- title = v.filename
- -- else assume a local file and just print the filename instead
- else
- local _
- _, title = utils.split_path(v.filename)
- title_cache[v.filename] = title
- end
- end
- message = string.format('%s %s %s\n', message,
- (v.current and '●' or '○'), title)
- end
- return message
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement