Skip to content

Commit

Permalink
ytdl_hook: exit early, save an indentation level
Browse files Browse the repository at this point in the history
  • Loading branch information
wiiaboo authored and kevmitch committed Feb 12, 2018
1 parent bf35498 commit 976daf1
Showing 1 changed file with 165 additions and 164 deletions.
329 changes: 165 additions & 164 deletions player/lua/ytdl_hook.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
end

-- strip ytdl://
if (url:find("ytdl://") == 1) then
url = url:sub(8)
-- 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

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
-- strip ytdl://
if (url:find("ytdl://") == 1) then
url = url:sub(8)
end

local command = {
ytdl.path, "--no-warnings", "-J", "--flat-playlist",
"--sub-format", "ass/srt/best", "--no-playlist"
}
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

-- 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
local command = {
ytdl.path, "--no-warnings", "-J", "--flat-playlist",
"--sub-format", "ass/srt/best", "--no-playlist"
}

format = "bestaudio/best"
msg.verbose("Video disabled. Only using audio")
end
-- 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

if (format == "") then
format = "bestvideo+bestaudio/best"
end
table.insert(command, "--format")
table.insert(command, format)
format = "bestaudio/best"
msg.verbose("Video disabled. Only using audio")
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
end
if (format == "") then
format = "bestvideo+bestaudio/best"
end
table.insert(command, "--format")
table.insert(command, format)

if (allsubs == true) then
table.insert(command, "--all-subs")
for param, arg in pairs(raw_options) do
table.insert(command, "--" .. param)
if (arg ~= "") then
table.insert(command, arg)
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
if (param == "sub-lang") and (arg ~= "") then
allsubs = false
end
if (param == "proxy") and (arg ~= "") then
proxy = arg
end
end

local json, err = utils.parse_json(json)

if (json == nil) then
msg.error("failed to parse JSON data: " .. err)
return
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

local json, err = utils.parse_json(json)

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')
msg.verbose("youtube-dl succeeded!")
msg.debug('ytdl parsing took '..os.clock()-start_time..' seconds')

json["proxy"] = json["proxy"] or proxy
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
-- 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
if (#json.entries == 0) then
msg.warn("Got empty playlist, nothing to play.")
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"]
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"]


-- 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")
-- 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)
local playlist = edl_track_joined(json.entries)

msg.debug("EDL: " .. playlist)
msg.debug("EDL: " .. playlist)

if not playlist then
return
end
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)
-- 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
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
-- 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..";"
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
msg.debug(j.." sub EDL: "..subfile)
mp.commandv("sub-add", subfile, "auto", req.ext, j)
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
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

if #playlist > 0 then
mp.set_property("stream-open-filename", "memory://" .. table.concat(playlist, "\n"))
-- 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

else -- probably a video
add_single_video(json)
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)
Expand Down

0 comments on commit 976daf1

Please sign in to comment.