Skip to content

Instantly share code, notes, and snippets.

@bitingsock
Last active August 19, 2023 18:35
Show Gist options
  • Save bitingsock/f9a87ea158035d4a36899b559d611228 to your computer and use it in GitHub Desktop.
Save bitingsock/f9a87ea158035d4a36899b559d611228 to your computer and use it in GitHub Desktop.
resolves ytdl for urls in playlist when the demuxer reaches near the end of the file
local utils = require 'mp.utils'
local ytdlPath = mp.find_config_file("youtube-dl.exe")
local fileDuration = 0
local function check_position()
if fileDuration==0 then
mp.unobserve_property(check_position)
return
end
local demuxEndPosition = mp.get_property("demuxer-cache-time")
if demuxEndPosition and
fileDuration > 0 and
(fileDuration - demuxEndPosition < 10) and
(mp.get_property("playlist-pos-1")~=mp.get_property("playlist-count")) then
local next = tonumber(mp.get_property("playlist-pos")) + 1
local nextFile = mp.get_property("playlist/"..tostring(next).."/filename")
if nextFile then
local ytdl = {}
ytdl.args = {ytdlPath, "-f "..mp.get_property("ytdl-format"), "-e", "-g", nextFile}
local res = utils.subprocess(ytdl)
local lines = {}
for s in res.stdout:gmatch("[^\r\n]+") do
table.insert(lines, s)
end
local audioURL = ""
if lines[3] then
audioURL = ',audio-file=['..lines[3]..']'
end
if lines[1] and lines[2] then
mp.commandv("loadfile", lines[2], "append", 'force-media-title=['..lines[1]..']'..audioURL)
mp.commandv("playlist_move", mp.get_property("playlist-count") - 1 , next)
mp.commandv("playlist_remove", next + 1)
end
mp.unobserve_property(check_position)
end
end
end
local function observe()
if mp.get_property("path"):find("://", 0, false) then
fileDuration = mp.get_property("duration", 0)
fileDuration = tonumber(fileDuration)
mp.observe_property("time-pos", "native", check_position)
end
end
mp.register_event("file-loaded", observe)
@bitingsock
Copy link
Author

One limitation of this script is that because the urls are resolved in this way, --resume-playback will almost certainly not work.

@zaza42
Copy link

zaza42 commented Mar 14, 2019

On Linux change line 2:
local ytdlPath = "youtube-dl"

@bitingsock
Copy link
Author

I've discovered that a lot of usefulness of this script doesn't actually work :(
It appears --prefetch-playlist doesn't actually fill the cache with subsequent data and merely opens the url and keeps it active, downloading what appears to be header and metadata and mpv just waits for that file to actually be playing before downloading any more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment