Skip to content

Instantly share code, notes, and snippets.

@Gutter-chan
Forked from bitingsock/ytdl-preload.lua
Created August 19, 2021 13:24
Show Gist options
  • Save Gutter-chan/da1dda8bf892e4403529105e42f01d1c to your computer and use it in GitHub Desktop.
Save Gutter-chan/da1dda8bf892e4403529105e42f01d1c to your computer and use it in GitHub Desktop.
(Updated) precache the next entry in your playlist if it is a network source by downloading it to a temp file ahead of time. Make sure to change Line 1 to a directory where you want the files to download to. This update makes it much more usable by downloading all streams in parallel so you can watch it while it downloads. Only downside is it do…
local cachePath = "R:/ytdl"
local nextIndex
local observeInitial
mp.enable_messages("info")
local function listener(event)
if event.prefix == mp.get_script_name() then
local destination = string.match(event.text, "%[download%] Destination: (.+).mkv") or
string.match(event.text, "%[download%] (.+).mkv has already been downloaded")
if destination and string.find(destination,":\\") then
mp.commandv("loadfile", destination..".mkv", "append", "audio-file="..destination..".webm")--,sub-file="..destination..".en.vtt") --in case they are not set up to autoload
mp.commandv("playlist_move", mp.get_property("playlist-count") - 1 , nextIndex)
mp.commandv("playlist_remove", nextIndex + 1)
mp.unregister_event(listener)
end
end
end
local function DL()
mp.add_timeout(1, function()
if tonumber(mp.get_property("playlist-pos-1"))>0 and mp.get_property("playlist-pos-1")~=mp.get_property("playlist-count") then
nextIndex = tonumber(mp.get_property("playlist-pos")) + 1
local nextFile = mp.get_property("playlist/"..tostring(nextIndex).."/filename")
if nextFile and nextFile:find("://", 0, false) then
mp.register_event("log-message", listener)
mp.command_native_async({
name="subprocess",
args = {"C:/mpv/youtube-dl.exe", "-f", "bestvideo", "--no-part", "-o", cachePath.."/%(title)s.mkv", nextFile},
playback_only = false
}, function() end)
mp.command_native_async({
name="subprocess",
args = {"C:/mpv/youtube-dl.exe", "-q", "-f", "bestaudio", "--no-part", "-o", cachePath.."/%(title)s.webm", nextFile},
playback_only = false
}, function() end)
mp.command_native_async({
name="subprocess",
args = {"youtube-dl", "-q", "--skip-download", "--sub-lang", "en", "--write-sub", "-o", cachePath.."/%(title)s", nextFile},
playback_only = false
}, function() end)
end
end
end)
end
local function clearCache()
if package.config:sub(1,1) ~= '/' then
os.execute('rd /s/q "'..cachePath..'"')
else
os.execute('rm -rd "'..cachePath..'"')
end
end
local f=io.open(cachePath,"r")
if f~=nil then
io.close(f)
clearCache()
end
mp.observe_property("playlist-count", "number", function()
if observeInitial then
DL()
else
observeInitial = true
end
end)
mp.register_event("start-file", DL)
mp.register_event("shutdown", clearCache)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment