diff options
Diffstat (limited to 'player/lua/ytdl_hook.lua')
-rw-r--r-- | player/lua/ytdl_hook.lua | 91 |
1 files changed, 81 insertions, 10 deletions
diff --git a/player/lua/ytdl_hook.lua b/player/lua/ytdl_hook.lua index a637d4e1aa..fa97dddc7b 100644 --- a/player/lua/ytdl_hook.lua +++ b/player/lua/ytdl_hook.lua @@ -6,6 +6,8 @@ local ytdl = { searched = false } +local chapter_list = {} + local function exec(args) local ret = utils.subprocess({args = args}) return ret.status, ret.stdout, ret @@ -57,6 +59,35 @@ local function edl_escape(url) return "%" .. string.len(url) .. "%" .. url end +local function time_to_secs(time_string) + local ret + + local a, b, c = time_string:match("(%d+):(%d%d?):(%d%d)") + if a ~= nil then + ret = (a*3600 + b*60 + c) + else + a, b = time_string:match("(%d%d?):(%d%d)") + if a ~= nil then + ret = (a*60 + b) + end + end + + return ret +end + +local function extract_chapters(data, video_length) + local ret = {} + + for line in data:gmatch("[^\r\n]+") do + local time = time_to_secs(line) + if time and (time < video_length) then + table.insert(ret, {time = time, title = line}) + end + end + + return ret +end + mp.add_hook("on_load", 10, function () local url = mp.get_property("stream-open-filename") @@ -93,14 +124,15 @@ mp.add_hook("on_load", 10, function () if (mp.get_property("options/vid") == "no") and not option_was_set("ytdl-format") then - format = "bestaudio/best" + format = "bestaudio" msg.verbose("Video disabled. Only using audio") end - if (format ~= "") then - table.insert(command, "--format") - table.insert(command, format) + if (format == "") then + format = "bestvideo+bestaudio" end + table.insert(command, "--format") + table.insert(command, string.format('(%s)[protocol!=http_dash_segments]/best', format)) for param, arg in pairs(raw_options) do table.insert(command, "--" .. param) @@ -180,15 +212,27 @@ mp.add_hook("on_load", 10, function () json.title) end - if not (json.entries[1].requested_subtitles == nil) then - for j, req in pairs(json.entries[1].requested_subtitles) do + -- 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 - subfile = subfile..edl_escape(entry.requested_subtitles[j].url) - if not (entry.duration == nil) then - subfile = subfile..",start=0,length="..entry.duration + if not (entry.requested_subtitles == nil) and + not (entry.requested_subtitles[j] == nil) then + subfile = subfile..edl_escape(entry.requested_subtitles[j].url) + else + subfile = subfile..edl_escape("memory://WEBVTT") end - subfile = subfile .. ";" + subfile = subfile..",start=0,length="..entry.duration..";" end msg.debug(j.." sub EDL: "..subfile) mp.commandv("sub-add", subfile, "auto", req.ext, j) @@ -200,6 +244,12 @@ mp.add_hook("on_load", 10, function () local playlist = "#EXTM3U\n" 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+', ' ') + playlist = playlist .. "#EXTINF:0," .. title .. "\n" + end -- some extractors will still return the full info for -- all clips in the playlist and the URL will point @@ -271,12 +321,23 @@ mp.add_hook("on_load", 10, function () end end + -- add chapters from description + if not (json.description == nil) and not (json.duration == nil) then + chapter_list = extract_chapters(json.description, json.duration) + end + -- set start time if not (json.start_time == nil) then msg.debug("Setting start to: " .. json.start_time .. " secs") mp.set_property("file-local-options/start", json.start_time) end + -- set aspect ratio for anamorphic video + if not (json.stretched_ratio == nil) and + not option_was_set("video-aspect") then + mp.set_property('file-local-options/video-aspect', json.stretched_ratio) + end + -- for rtmp if (json.protocol == "rtmp") then local rtmp_prop = append_rtmp_prop(nil, @@ -295,3 +356,13 @@ mp.add_hook("on_load", 10, function () end end end) + + +mp.add_hook("on_preloaded", 10, function () + if next(chapter_list) ~= nil then + msg.verbose("Setting chapters from video's description") + + mp.set_property_native("chapter-list", chapter_list) + chapter_list = {} + end +end) |