2014-11-19 17:37:23 +00:00
|
|
|
local utils = require 'mp.utils'
|
|
|
|
local msg = require 'mp.msg'
|
|
|
|
|
|
|
|
local ytdl = {
|
|
|
|
path = "youtube-dl",
|
2015-02-23 17:59:32 +00:00
|
|
|
minver = "2015.02.23.1",
|
2014-11-19 17:37:23 +00:00
|
|
|
vercheck = nil,
|
|
|
|
}
|
|
|
|
|
2015-01-24 19:54:35 +00:00
|
|
|
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
|
2014-11-19 17:37:23 +00:00
|
|
|
end
|
2015-01-24 19:54:35 +00:00
|
|
|
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
|
2014-11-19 17:37:23 +00:00
|
|
|
|
2015-04-03 17:04:31 +00:00
|
|
|
local function append_rtmp_prop(props, name, value)
|
|
|
|
if not name or not value then
|
|
|
|
return props
|
|
|
|
end
|
|
|
|
|
|
|
|
if props and props ~= "" then
|
|
|
|
props = props..","
|
|
|
|
else
|
|
|
|
props = ""
|
|
|
|
end
|
|
|
|
|
|
|
|
return props..name.."=\""..value.."\""
|
|
|
|
end
|
|
|
|
|
2015-01-24 19:54:35 +00:00
|
|
|
mp.add_hook("on_load", 10, function ()
|
2014-11-19 17:37:23 +00:00
|
|
|
local url = mp.get_property("stream-open-filename")
|
|
|
|
|
|
|
|
if (url:find("http://") == 1) or (url:find("https://") == 1)
|
|
|
|
or (url:find("ytdl://") == 1) then
|
|
|
|
|
|
|
|
-- check version of youtube-dl if not done yet
|
|
|
|
if (ytdl.vercheck == nil) then
|
|
|
|
|
|
|
|
-- check for youtube-dl in mpv's config dir
|
|
|
|
local ytdl_mcd = mp.find_config_file("youtube-dl")
|
|
|
|
if not (ytdl_mcd == nil) then
|
|
|
|
msg.verbose("found youtube-dl at: " .. ytdl_mcd)
|
|
|
|
ytdl.path = ytdl_mcd
|
|
|
|
end
|
|
|
|
|
|
|
|
msg.debug("checking ytdl version ...")
|
|
|
|
local es, version = exec({ytdl.path, "--version"})
|
|
|
|
if (es < 0) then
|
|
|
|
msg.warn("youtube-dl not found, not executable, or broken.")
|
|
|
|
ytdl.vercheck = false
|
|
|
|
elseif (version < ytdl.minver) then
|
|
|
|
msg.verbose("found youtube-dl version: " .. version)
|
|
|
|
msg.warn("Your version of youtube-dl is too old! "
|
|
|
|
.. "You need at least version '"..ytdl.minver
|
|
|
|
.. "', try running `youtube-dl -U`.")
|
|
|
|
ytdl.vercheck = false
|
|
|
|
else
|
|
|
|
msg.verbose("found youtube-dl version: " .. version)
|
|
|
|
ytdl.vercheck = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if not (ytdl.vercheck) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- strip ytdl://
|
|
|
|
if (url:find("ytdl://") == 1) then
|
|
|
|
url = url:sub(8)
|
|
|
|
end
|
|
|
|
|
2014-11-19 22:33:28 +00:00
|
|
|
local format = mp.get_property("options/ytdl-format")
|
2015-02-22 20:32:42 +00:00
|
|
|
local raw_options = mp.get_property_native("options/ytdl-raw-options")
|
2014-11-19 22:33:28 +00:00
|
|
|
|
2014-11-19 17:37:23 +00:00
|
|
|
-- subformat workaround
|
2015-02-23 17:59:32 +00:00
|
|
|
local subformat = "ass/srt/best"
|
2014-11-19 17:37:23 +00:00
|
|
|
|
2014-11-19 22:33:28 +00:00
|
|
|
local command = {
|
2015-02-24 09:58:09 +00:00
|
|
|
ytdl.path, "--no-warnings", "-J", "--flat-playlist", "--all-subs",
|
2014-11-19 22:33:28 +00:00
|
|
|
"--sub-format", subformat, "--no-playlist"
|
|
|
|
}
|
2015-04-27 20:30:10 +00:00
|
|
|
|
2015-05-09 19:41:44 +00:00
|
|
|
-- Checks if video option is "no", change options accordingly
|
|
|
|
if (mp.get_property("options/vid") == "no") then
|
|
|
|
format = "bestaudio/best"
|
|
|
|
msg.verbose("Video disabled. Only using audio")
|
|
|
|
end
|
2015-04-27 20:30:10 +00:00
|
|
|
|
2014-11-19 22:33:28 +00:00
|
|
|
if (format ~= "") then
|
|
|
|
table.insert(command, "--format")
|
|
|
|
table.insert(command, format)
|
|
|
|
end
|
2015-04-09 13:26:12 +00:00
|
|
|
|
2015-02-22 20:32:42 +00:00
|
|
|
for param, arg in pairs(raw_options) do
|
|
|
|
table.insert(command, "--" .. param)
|
|
|
|
if (arg ~= "") then
|
|
|
|
table.insert(command, arg)
|
|
|
|
end
|
|
|
|
end
|
2014-11-19 22:33:28 +00:00
|
|
|
table.insert(command, "--")
|
|
|
|
table.insert(command, url)
|
|
|
|
local es, json = exec(command)
|
2014-11-19 17:37:23 +00:00
|
|
|
|
|
|
|
if (es < 0) or (json == nil) or (json == "") then
|
|
|
|
msg.warn("youtube-dl failed, trying to play URL directly ...")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local json, err = utils.parse_json(json)
|
|
|
|
|
|
|
|
if (json == nil) then
|
|
|
|
msg.error("failed to parse JSON data: " .. err)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2015-01-03 02:38:23 +00:00
|
|
|
msg.verbose("youtube-dl succeeded!")
|
2014-11-19 17:37:23 +00:00
|
|
|
|
|
|
|
-- what did we get?
|
|
|
|
if not (json["direct"] == nil) and (json["direct"] == true) then
|
|
|
|
-- direct URL, nothing to do
|
|
|
|
msg.verbose("Got direct URL")
|
|
|
|
return
|
2015-06-07 08:13:29 +00:00
|
|
|
elseif not (json["_type"] == nil) and ((json["_type"] == "playlist") or (json["_type"] == "multi_video")) then
|
2014-11-19 17:37:23 +00:00
|
|
|
-- a playlist
|
|
|
|
|
2015-01-16 05:59:16 +00:00
|
|
|
if (#json.entries == 0) then
|
|
|
|
msg.warn("Got empty playlist, nothing to play.")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-11-26 16:36:23 +00:00
|
|
|
-- some funky guessing to detect multi-arc videos
|
|
|
|
if not (json.entries[1]["webpage_url"] == nil)
|
|
|
|
and (json.entries[1]["webpage_url"] == json["webpage_url"]) then
|
|
|
|
msg.verbose("multi-arc video detected, building EDL")
|
|
|
|
|
|
|
|
|
|
|
|
local playlist = "edl://"
|
|
|
|
for i, entry in pairs(json.entries) do
|
|
|
|
|
2015-05-07 18:41:46 +00:00
|
|
|
local urllength = string.len(entry.url)
|
|
|
|
playlist = playlist .. "%" .. urllength .. "%" .. entry.url .. ";"
|
|
|
|
|
2014-11-19 17:37:23 +00:00
|
|
|
end
|
|
|
|
|
2014-11-26 16:36:23 +00:00
|
|
|
msg.debug("EDL: " .. playlist)
|
|
|
|
|
2015-01-24 19:54:35 +00:00
|
|
|
-- 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
|
2014-11-26 16:36:23 +00:00
|
|
|
|
|
|
|
mp.set_property("stream-open-filename", playlist)
|
|
|
|
if not (json.title == nil) then
|
2015-05-22 18:03:14 +00:00
|
|
|
mp.set_property("file-local-options/force-media-title", json.title)
|
2014-11-26 16:36:23 +00:00
|
|
|
end
|
2014-11-19 17:37:23 +00:00
|
|
|
|
2014-11-26 16:36:23 +00:00
|
|
|
else
|
|
|
|
|
|
|
|
local playlist = "#EXTM3U\n"
|
|
|
|
for i, entry in pairs(json.entries) do
|
|
|
|
local site = entry.url
|
|
|
|
|
|
|
|
-- some extractors will still return the full info for
|
|
|
|
-- all clips in the playlist and the URL will point
|
|
|
|
-- directly to the file in that case, which we don't
|
|
|
|
-- want so get the webpage URL instead, which is what
|
|
|
|
-- we want
|
|
|
|
if not (entry["webpage_url"] == nil) then
|
|
|
|
site = entry["webpage_url"]
|
|
|
|
end
|
|
|
|
|
|
|
|
playlist = playlist .. "ytdl://" .. site .. "\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
mp.set_property("stream-open-filename", "memory://" .. playlist)
|
|
|
|
end
|
2014-11-19 17:37:23 +00:00
|
|
|
|
|
|
|
else -- probably a video
|
|
|
|
local streamurl = ""
|
|
|
|
|
|
|
|
-- DASH?
|
|
|
|
if not (json["requested_formats"] == nil) then
|
2014-11-26 16:17:05 +00:00
|
|
|
msg.info("Using DASH, expect inaccurate duration.")
|
|
|
|
if not (json.duration == nil) then
|
2015-01-16 05:59:16 +00:00
|
|
|
msg.info("Actual duration: " .. mp.format_time(json.duration))
|
2014-11-26 16:17:05 +00:00
|
|
|
end
|
|
|
|
|
2014-11-19 17:37:23 +00:00
|
|
|
-- video url
|
|
|
|
streamurl = json["requested_formats"][1].url
|
|
|
|
|
|
|
|
-- audio url
|
|
|
|
mp.set_property("file-local-options/audio-file",
|
|
|
|
json["requested_formats"][2].url)
|
|
|
|
|
|
|
|
-- workaround for slow startup (causes inaccurate duration)
|
|
|
|
mp.set_property("file-local-options/demuxer-lavf-o",
|
|
|
|
"fflags=+ignidx")
|
|
|
|
|
|
|
|
elseif not (json.url == nil) then
|
|
|
|
-- normal video
|
|
|
|
streamurl = json.url
|
2015-01-24 19:54:35 +00:00
|
|
|
set_http_headers(json.http_headers)
|
2014-11-19 17:37:23 +00:00
|
|
|
else
|
|
|
|
msg.error("No URL found in JSON data.")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
msg.debug("streamurl: " .. streamurl)
|
|
|
|
|
|
|
|
mp.set_property("stream-open-filename", streamurl)
|
|
|
|
|
2015-05-23 13:15:16 +00:00
|
|
|
mp.set_property("file-local-options/force-media-title", json.title)
|
2014-11-19 17:37:23 +00:00
|
|
|
|
|
|
|
-- add subtitles
|
2015-02-23 17:59:32 +00:00
|
|
|
if not (json.requested_subtitles == nil) then
|
|
|
|
for lang, sub_info in pairs(json.requested_subtitles) do
|
2014-11-19 17:37:23 +00:00
|
|
|
msg.verbose("adding subtitle ["..lang.."]")
|
|
|
|
|
|
|
|
local slang = lang
|
|
|
|
if (lang:len() > 3) then
|
|
|
|
slang = lang:sub(1,2)
|
|
|
|
end
|
|
|
|
|
2015-02-23 17:59:32 +00:00
|
|
|
if not (sub_info.data == nil) then
|
|
|
|
sub = "memory://"..sub_info.data
|
|
|
|
else
|
|
|
|
sub = sub_info.url
|
|
|
|
end
|
|
|
|
mp.commandv("sub_add", sub,
|
|
|
|
"auto", lang.." "..sub_info.ext, slang)
|
2014-11-19 17:37:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- for rtmp
|
|
|
|
if not (json.play_path == nil) then
|
2015-04-03 17:04:31 +00:00
|
|
|
local rtmp_prop = append_rtmp_prop(nil, "rtmp_tcurl", streamurl)
|
|
|
|
rtmp_prop = append_rtmp_prop(rtmp_prop, "rtmp_pageurl", json.page_url)
|
|
|
|
rtmp_prop = append_rtmp_prop(rtmp_prop, "rtmp_playpath", json.play_path)
|
|
|
|
rtmp_prop = append_rtmp_prop(rtmp_prop, "rtmp_swfverify", json.player_url)
|
|
|
|
rtmp_prop = append_rtmp_prop(rtmp_prop, "rtmp_app", json.app)
|
|
|
|
|
|
|
|
mp.set_property("file-local-options/stream-lavf-o", rtmp_prop)
|
2014-11-19 17:37:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|