From 047788e3b1354562f99ce8dacdba1972ad990d03 Mon Sep 17 00:00:00 2001 From: wm4 Date: Sat, 24 Jan 2015 20:54:35 +0100 Subject: 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. --- player/lua/ytdl_hook.lua | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) (limited to 'player') diff --git a/player/lua/ytdl_hook.lua b/player/lua/ytdl_hook.lua index d59b3a4fd9..b947fdec86 100644 --- a/player/lua/ytdl_hook.lua +++ b/player/lua/ytdl_hook.lua @@ -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 +local function exec(args) + local ret = utils.subprocess({args = args}) + return ret.status, ret.stdout +end + +-- 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 -- cgit v1.2.3