ytdl_hook.lua: write cookies via cookies-file instead of stream-lavf-o

stream-lavf-o[cookies] only feeds the lavf stream backend. Instead of
side-loading it, use core `--cookies-file` option.
This commit is contained in:
Kacper Michajłow
2026-05-08 21:30:49 +02:00
parent 893313e5af
commit 6017d1fd1c
+18 -32
View File
@@ -193,40 +193,33 @@ local function parse_cookies(cookies_line)
return cookies
end
-- serialize cookies for avformat
local function serialize_cookies_for_avformat(cookies)
-- serialize cookies in Netscape cookies.txt format
local function serialize_cookies(cookies)
local result = ''
for _, cookie in pairs(cookies) do
local cookie_str = ('%s=%s; '):format(cookie.name, cookie.value:gsub('^"(.+)"$', '%1'))
for k, v in pairs(cookie) do
if k ~= "name" and k ~= "value" then
cookie_str = cookie_str .. ('%s=%s; '):format(k, v)
end
end
result = result .. cookie_str .. '\r\n'
local domain = cookie.domain or ""
result = result .. ("%s\t%s\t%s\t%s\t%s\t%s\t%s\n"):format(
domain,
(domain:sub(1, 1) == ".") and "TRUE" or "FALSE",
cookie.path or "/",
cookie.secure and "TRUE" or "FALSE",
cookie.expires or "0",
cookie.name,
cookie.value:gsub('^"(.+)"$', '%1'))
end
return result
end
-- set file-local cookies, preserving existing ones
-- set file-local cookies-file pointing at an in-memory cookies.txt
local function set_cookies(cookies)
if not cookies or cookies == "" then
return
end
local option_key = "file-local-options/stream-lavf-o"
local stream_opts = mp.get_property_native(option_key, {})
local existing_cookies = parse_cookies(stream_opts["cookies"])
local new_cookies = parse_cookies(cookies)
for cookie_key, cookie in pairs(new_cookies) do
if not existing_cookies[cookie_key] then
existing_cookies[cookie_key] = cookie
end
local data = serialize_cookies(parse_cookies(cookies))
if data ~= "" then
mp.set_property_bool("file-local-options/cookies", true)
mp.set_property("file-local-options/cookies-file", "memory://" .. data)
end
stream_opts["cookies"] = serialize_cookies_for_avformat(existing_cookies)
mp.set_property_native(option_key, stream_opts)
end
local function append_libav_opt(props, name, value)
@@ -854,14 +847,7 @@ local function add_single_video(json)
"http_proxy", json.proxy)
end
if cookies and cookies ~= "" then
local existing_cookies = parse_cookies(stream_opts["cookies"])
local new_cookies = parse_cookies(cookies)
for cookie_key, cookie in pairs(new_cookies) do
existing_cookies[cookie_key] = cookie
end
stream_opts["cookies"] = serialize_cookies_for_avformat(existing_cookies)
end
set_cookies(cookies)
local chunk_size = math.huge
if has_requested_formats then
@@ -1061,7 +1047,7 @@ local function run_ytdl_hook(url)
-- can't change the http headers for each entry, so use the 1st
set_http_headers(json.entries[1].http_headers)
set_cookies(json.entries[1].cookies or json.cookies)
playlist_cookies[playlist] = json.entries[1].cookies or json.cookies
mp.set_property("stream-open-filename", playlist)
if json.title and mp.get_property("force-media-title", "") == "" then