ytdl: implement user-agent and cookie overrides

For some sites, youtube-dl sends a special user-agent. If we don't send
the same user-agent, the server will reject mpv's connection attempt.
This was observed with trailers.apple.com. Fix it by forcing the
user-agent youtube-dl uses.

Some sites set cookies when doing a website access, and require the
client to provide these cookies when downloading the actual media. This
is needed at least by nicovideo.jp. Fix by adding youtube-dl's cookies
to our request headers.

Both of these require a very recent youtube-dl version (youtube-dl added
the necessary headers a few hours ago). The script still works with
older youtube-dl versions, though.
This commit is contained in:
wm4 2015-01-24 20:54:35 +01:00 committed by Diogo Franco (Kovensky)
parent 9c83e8483e
commit 0f83f1e4b3
1 changed files with 33 additions and 4 deletions

View File

@ -7,13 +7,37 @@ local ytdl = {
vercheck = nil,
}
mp.add_hook("on_load", 10, function ()
local function exec(args)
local ret = utils.subprocess({args = args})
return ret.status, ret.stdout
end
local function exec(args)
local ret = utils.subprocess({args = args})
return ret.status, ret.stdout
-- return if it was explicitly set on the command line
local function option_was_set(name)
return mp.get_property_bool("option-info/" .. name .. "/set-from-commandline",
false)
end
-- youtube-dl may set special http headers for some sites (user-agent, cookies)
local function set_http_headers(http_headers)
if not http_headers then
return
end
local headers = {}
local useragent = http_headers["User-Agent"]
if useragent and not option_was_set("user-agent") then
mp.set_property("file-local-options/user-agent", useragent)
end
local cookies = http_headers["Cookie"]
if cookies then
headers[#headers + 1] = "Cookie: " .. cookies
end
if #headers > 0 and not option_was_set("http-header-fields") then
mp.set_property_native("file-local-options/http-header-fields", headers)
end
end
mp.add_hook("on_load", 10, function ()
local url = mp.get_property("stream-open-filename")
if (url:find("http://") == 1) or (url:find("https://") == 1)
@ -117,6 +141,10 @@ mp.add_hook("on_load", 10, function ()
msg.debug("EDL: " .. playlist)
-- can't change the http headers for each entry, so use the 1st
if json.entries[1] then
set_http_headers(json.entries[1].http_headers)
end
mp.set_property("stream-open-filename", playlist)
if not (json.title == nil) then
@ -168,6 +196,7 @@ mp.add_hook("on_load", 10, function ()
elseif not (json.url == nil) then
-- normal video
streamurl = json.url
set_http_headers(json.http_headers)
else
msg.error("No URL found in JSON data.")
return