mirror of https://github.com/mpv-player/mpv
ytdl_hook: exit early, save an indentation level
This commit is contained in:
parent
bf3549839e
commit
976daf1942
|
@ -391,210 +391,211 @@ end
|
|||
|
||||
mp.add_hook(o.try_ytdl_first and "on_load" or "on_load_fail", 10, function ()
|
||||
local url = mp.get_property("stream-open-filename", "")
|
||||
if not (url:find("ytdl://") == 1) and
|
||||
not ((url:find("https?://") == 1) and not is_blacklisted(url)) then
|
||||
return
|
||||
end
|
||||
local start_time = os.clock()
|
||||
if (url:find("ytdl://") == 1) or
|
||||
((url:find("https?://") == 1) and not is_blacklisted(url)) then
|
||||
|
||||
-- check for youtube-dl in mpv's config dir
|
||||
if not (ytdl.searched) then
|
||||
local exesuf = (package.config:sub(1,1) == '\\') and '.exe' or ''
|
||||
local ytdl_mcd = mp.find_config_file("youtube-dl" .. exesuf)
|
||||
if not (ytdl_mcd == nil) then
|
||||
msg.verbose("found youtube-dl at: " .. ytdl_mcd)
|
||||
ytdl.path = ytdl_mcd
|
||||
end
|
||||
ytdl.searched = true
|
||||
-- check for youtube-dl in mpv's config dir
|
||||
if not (ytdl.searched) then
|
||||
local exesuf = (package.config:sub(1,1) == '\\') and '.exe' or ''
|
||||
local ytdl_mcd = mp.find_config_file("youtube-dl" .. exesuf)
|
||||
if not (ytdl_mcd == nil) then
|
||||
msg.verbose("found youtube-dl at: " .. ytdl_mcd)
|
||||
ytdl.path = ytdl_mcd
|
||||
end
|
||||
ytdl.searched = true
|
||||
end
|
||||
|
||||
-- strip ytdl://
|
||||
if (url:find("ytdl://") == 1) then
|
||||
url = url:sub(8)
|
||||
-- strip ytdl://
|
||||
if (url:find("ytdl://") == 1) then
|
||||
url = url:sub(8)
|
||||
end
|
||||
|
||||
local format = mp.get_property("options/ytdl-format")
|
||||
local raw_options = mp.get_property_native("options/ytdl-raw-options")
|
||||
local allsubs = true
|
||||
local proxy = nil
|
||||
|
||||
local command = {
|
||||
ytdl.path, "--no-warnings", "-J", "--flat-playlist",
|
||||
"--sub-format", "ass/srt/best", "--no-playlist"
|
||||
}
|
||||
|
||||
-- Checks if video option is "no", change format accordingly,
|
||||
-- but only if user didn't explicitly set one
|
||||
if (mp.get_property("options/vid") == "no")
|
||||
and not option_was_set("ytdl-format") then
|
||||
|
||||
format = "bestaudio/best"
|
||||
msg.verbose("Video disabled. Only using audio")
|
||||
end
|
||||
|
||||
if (format == "") then
|
||||
format = "bestvideo+bestaudio/best"
|
||||
end
|
||||
table.insert(command, "--format")
|
||||
table.insert(command, format)
|
||||
|
||||
for param, arg in pairs(raw_options) do
|
||||
table.insert(command, "--" .. param)
|
||||
if (arg ~= "") then
|
||||
table.insert(command, arg)
|
||||
end
|
||||
|
||||
local format = mp.get_property("options/ytdl-format")
|
||||
local raw_options = mp.get_property_native("options/ytdl-raw-options")
|
||||
local allsubs = true
|
||||
local proxy = nil
|
||||
|
||||
local command = {
|
||||
ytdl.path, "--no-warnings", "-J", "--flat-playlist",
|
||||
"--sub-format", "ass/srt/best", "--no-playlist"
|
||||
}
|
||||
|
||||
-- Checks if video option is "no", change format accordingly,
|
||||
-- but only if user didn't explicitly set one
|
||||
if (mp.get_property("options/vid") == "no")
|
||||
and not option_was_set("ytdl-format") then
|
||||
|
||||
format = "bestaudio/best"
|
||||
msg.verbose("Video disabled. Only using audio")
|
||||
if (param == "sub-lang") and (arg ~= "") then
|
||||
allsubs = false
|
||||
end
|
||||
|
||||
if (format == "") then
|
||||
format = "bestvideo+bestaudio/best"
|
||||
if (param == "proxy") and (arg ~= "") then
|
||||
proxy = arg
|
||||
end
|
||||
table.insert(command, "--format")
|
||||
table.insert(command, format)
|
||||
end
|
||||
|
||||
for param, arg in pairs(raw_options) do
|
||||
table.insert(command, "--" .. param)
|
||||
if (arg ~= "") then
|
||||
table.insert(command, arg)
|
||||
end
|
||||
if (param == "sub-lang") and (arg ~= "") then
|
||||
allsubs = false
|
||||
end
|
||||
if (param == "proxy") and (arg ~= "") then
|
||||
proxy = arg
|
||||
end
|
||||
if (allsubs == true) then
|
||||
table.insert(command, "--all-subs")
|
||||
end
|
||||
table.insert(command, "--")
|
||||
table.insert(command, url)
|
||||
msg.debug("Running: " .. table.concat(command,' '))
|
||||
local es, json, result = exec(command)
|
||||
|
||||
if (es < 0) or (json == nil) or (json == "") then
|
||||
local err = "youtube-dl failed: "
|
||||
if result.error and result.error == "init" then
|
||||
err = err .. "not found or not enough permissions"
|
||||
elseif not result.killed_by_us then
|
||||
err = err .. "unexpected error ocurred"
|
||||
else
|
||||
err = string.format("%s returned '%d'", err, es)
|
||||
end
|
||||
msg.error(err)
|
||||
return
|
||||
end
|
||||
|
||||
if (allsubs == true) then
|
||||
table.insert(command, "--all-subs")
|
||||
end
|
||||
table.insert(command, "--")
|
||||
table.insert(command, url)
|
||||
msg.debug("Running: " .. table.concat(command,' '))
|
||||
local es, json, result = exec(command)
|
||||
local json, err = utils.parse_json(json)
|
||||
|
||||
if (es < 0) or (json == nil) or (json == "") then
|
||||
local err = "youtube-dl failed: "
|
||||
if result.error and result.error == "init" then
|
||||
err = err .. "not found or not enough permissions"
|
||||
elseif not result.killed_by_us then
|
||||
err = err .. "unexpected error ocurred"
|
||||
else
|
||||
err = string.format("%s returned '%d'", err, es)
|
||||
end
|
||||
msg.error(err)
|
||||
if (json == nil) then
|
||||
msg.error("failed to parse JSON data: " .. err)
|
||||
return
|
||||
end
|
||||
|
||||
msg.verbose("youtube-dl succeeded!")
|
||||
msg.debug('ytdl parsing took '..os.clock()-start_time..' seconds')
|
||||
|
||||
json["proxy"] = json["proxy"] or proxy
|
||||
|
||||
-- what did we get?
|
||||
if json["direct"] then
|
||||
-- direct URL, nothing to do
|
||||
msg.verbose("Got direct URL")
|
||||
return
|
||||
elseif (json["_type"] == "playlist")
|
||||
or (json["_type"] == "multi_video") then
|
||||
-- a playlist
|
||||
|
||||
if (#json.entries == 0) then
|
||||
msg.warn("Got empty playlist, nothing to play.")
|
||||
return
|
||||
end
|
||||
|
||||
local json, err = utils.parse_json(json)
|
||||
local self_redirecting_url =
|
||||
json.entries[1]["_type"] ~= "url_transparent" and
|
||||
json.entries[1]["webpage_url"] and
|
||||
json.entries[1]["webpage_url"] == json["webpage_url"]
|
||||
|
||||
if (json == nil) then
|
||||
msg.error("failed to parse JSON data: " .. err)
|
||||
return
|
||||
end
|
||||
|
||||
msg.verbose("youtube-dl succeeded!")
|
||||
msg.debug('ytdl parsing took '..os.clock()-start_time..' seconds')
|
||||
-- some funky guessing to detect multi-arc videos
|
||||
if self_redirecting_url and #json.entries > 1
|
||||
and json.entries[1].protocol == "m3u8_native"
|
||||
and json.entries[1].url then
|
||||
msg.verbose("multi-arc video detected, building EDL")
|
||||
|
||||
json["proxy"] = json["proxy"] or proxy
|
||||
local playlist = edl_track_joined(json.entries)
|
||||
|
||||
-- what did we get?
|
||||
if json["direct"] then
|
||||
-- direct URL, nothing to do
|
||||
msg.verbose("Got direct URL")
|
||||
return
|
||||
elseif (json["_type"] == "playlist")
|
||||
or (json["_type"] == "multi_video") then
|
||||
-- a playlist
|
||||
msg.debug("EDL: " .. playlist)
|
||||
|
||||
if (#json.entries == 0) then
|
||||
msg.warn("Got empty playlist, nothing to play.")
|
||||
if not playlist then
|
||||
return
|
||||
end
|
||||
|
||||
local self_redirecting_url =
|
||||
json.entries[1]["_type"] ~= "url_transparent" and
|
||||
json.entries[1]["webpage_url"] and
|
||||
json.entries[1]["webpage_url"] == json["webpage_url"]
|
||||
-- can't change the http headers for each entry, so use the 1st
|
||||
set_http_headers(json.entries[1].http_headers)
|
||||
|
||||
mp.set_property("stream-open-filename", playlist)
|
||||
if not (json.title == nil) then
|
||||
mp.set_property("file-local-options/force-media-title",
|
||||
json.title)
|
||||
end
|
||||
|
||||
-- some funky guessing to detect multi-arc videos
|
||||
if self_redirecting_url and #json.entries > 1
|
||||
and json.entries[1].protocol == "m3u8_native"
|
||||
and json.entries[1].url then
|
||||
msg.verbose("multi-arc video detected, building EDL")
|
||||
|
||||
local playlist = edl_track_joined(json.entries)
|
||||
|
||||
msg.debug("EDL: " .. playlist)
|
||||
|
||||
if not playlist then
|
||||
return
|
||||
end
|
||||
|
||||
-- can't change the http headers for each entry, so use the 1st
|
||||
set_http_headers(json.entries[1].http_headers)
|
||||
|
||||
mp.set_property("stream-open-filename", playlist)
|
||||
if not (json.title == nil) then
|
||||
mp.set_property("file-local-options/force-media-title",
|
||||
json.title)
|
||||
end
|
||||
|
||||
-- there might not be subs for the first segment
|
||||
local entry_wsubs = nil
|
||||
for i, entry in pairs(json.entries) do
|
||||
if not (entry.requested_subtitles == nil) then
|
||||
entry_wsubs = i
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if not (entry_wsubs == nil) and
|
||||
not (json.entries[entry_wsubs].duration == nil) then
|
||||
for j, req in pairs(json.entries[entry_wsubs].requested_subtitles) do
|
||||
local subfile = "edl://"
|
||||
for i, entry in pairs(json.entries) do
|
||||
if not (entry.requested_subtitles == nil) and
|
||||
not (entry.requested_subtitles[j] == nil) and
|
||||
url_is_safe(entry.requested_subtitles[j].url) then
|
||||
subfile = subfile..edl_escape(entry.requested_subtitles[j].url)
|
||||
else
|
||||
subfile = subfile..edl_escape("memory://WEBVTT")
|
||||
end
|
||||
subfile = subfile..",length="..entry.duration..";"
|
||||
end
|
||||
msg.debug(j.." sub EDL: "..subfile)
|
||||
mp.commandv("sub-add", subfile, "auto", req.ext, j)
|
||||
end
|
||||
end
|
||||
|
||||
elseif self_redirecting_url and #json.entries == 1 then
|
||||
msg.verbose("Playlist with single entry detected.")
|
||||
add_single_video(json.entries[1])
|
||||
else
|
||||
local playlist = {"#EXTM3U"}
|
||||
for i, entry in pairs(json.entries) do
|
||||
local site = entry.url
|
||||
local title = entry.title
|
||||
|
||||
if not (title == nil) then
|
||||
title = string.gsub(title, '%s+', ' ')
|
||||
table.insert(playlist, "#EXTINF:0," .. title)
|
||||
end
|
||||
|
||||
--[[ 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, but only if we aren't going to trigger an
|
||||
infinite loop
|
||||
--]]
|
||||
if entry["webpage_url"] and not self_redirecting_url then
|
||||
site = entry["webpage_url"]
|
||||
end
|
||||
|
||||
-- links with only youtube id as returned by --flat-playlist
|
||||
if not site:find("://") then
|
||||
table.insert(playlist, "https://youtu.be/" .. site)
|
||||
elseif url_is_safe(site) then
|
||||
table.insert(playlist, site)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
if #playlist > 0 then
|
||||
mp.set_property("stream-open-filename", "memory://" .. table.concat(playlist, "\n"))
|
||||
-- there might not be subs for the first segment
|
||||
local entry_wsubs = nil
|
||||
for i, entry in pairs(json.entries) do
|
||||
if not (entry.requested_subtitles == nil) then
|
||||
entry_wsubs = i
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
else -- probably a video
|
||||
add_single_video(json)
|
||||
if not (entry_wsubs == nil) and
|
||||
not (json.entries[entry_wsubs].duration == nil) then
|
||||
for j, req in pairs(json.entries[entry_wsubs].requested_subtitles) do
|
||||
local subfile = "edl://"
|
||||
for i, entry in pairs(json.entries) do
|
||||
if not (entry.requested_subtitles == nil) and
|
||||
not (entry.requested_subtitles[j] == nil) and
|
||||
url_is_safe(entry.requested_subtitles[j].url) then
|
||||
subfile = subfile..edl_escape(entry.requested_subtitles[j].url)
|
||||
else
|
||||
subfile = subfile..edl_escape("memory://WEBVTT")
|
||||
end
|
||||
subfile = subfile..",length="..entry.duration..";"
|
||||
end
|
||||
msg.debug(j.." sub EDL: "..subfile)
|
||||
mp.commandv("sub-add", subfile, "auto", req.ext, j)
|
||||
end
|
||||
end
|
||||
|
||||
elseif self_redirecting_url and #json.entries == 1 then
|
||||
msg.verbose("Playlist with single entry detected.")
|
||||
add_single_video(json.entries[1])
|
||||
else
|
||||
local playlist = {"#EXTM3U"}
|
||||
for i, entry in pairs(json.entries) do
|
||||
local site = entry.url
|
||||
local title = entry.title
|
||||
|
||||
if not (title == nil) then
|
||||
title = string.gsub(title, '%s+', ' ')
|
||||
table.insert(playlist, "#EXTINF:0," .. title)
|
||||
end
|
||||
|
||||
--[[ 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, but only if we aren't going to trigger an
|
||||
infinite loop
|
||||
--]]
|
||||
if entry["webpage_url"] and not self_redirecting_url then
|
||||
site = entry["webpage_url"]
|
||||
end
|
||||
|
||||
-- links with only youtube id as returned by --flat-playlist
|
||||
if not site:find("://") then
|
||||
table.insert(playlist, "https://youtu.be/" .. site)
|
||||
elseif url_is_safe(site) then
|
||||
table.insert(playlist, site)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
if #playlist > 0 then
|
||||
mp.set_property("stream-open-filename", "memory://" .. table.concat(playlist, "\n"))
|
||||
end
|
||||
end
|
||||
|
||||
else -- probably a video
|
||||
add_single_video(json)
|
||||
end
|
||||
msg.debug('script running time: '..os.clock()-start_time..' seconds')
|
||||
end)
|
||||
|
|
Loading…
Reference in New Issue