Last active
August 19, 2023 18:35
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
One limitation of this script is that because the urls are resolved in this way, --resume-playback will almost certainly not work.
On Linux change line 2:
local ytdlPath = "youtube-dl"
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
Line 2 will most likely need to be changed when used outside Windows.
Line 12 (demuxEndPosition < 10) decides when the url resolution is performed; change 10 to be how long before the end of the current file you want to resolve the next url in the playlist. The total time that the url will begin resolving will actually be this number plus whatever your demuxer-read-ahead is. It needs to be sufficient enough so that the url has time to resolve before the demuxer reaches the end otherwise mpv will not prefetch the data (assuming resolution was necessary). Otherwise if you set this to something like 999999999 then the url will be resolved as soon as the current file is loaded; this is usually ok if you are watching videos quickly but the resolved url might expire if you don't use it soon enough.