ytdl_hook: support thumbnails

Similar to "write-sub=", adding "write-thumbnail=" to raw-options will add the highest resolution thumbnail as a video track.

Closes #7041
This commit is contained in:
Eva 2020-06-21 03:30:59 +02:00 committed by Dudemanguy
parent 040622f6b7
commit a5b0eceeba
1 changed files with 36 additions and 3 deletions

View File

@ -616,7 +616,7 @@ local function formats_to_edl(json, formats, use_all_formats)
return res
end
local function add_single_video(json)
local function add_single_video(json, thumbnail, allthumbs)
local streamurl = ""
local format_info = ""
local max_bitrate = 0
@ -754,6 +754,32 @@ local function add_single_video(json)
end
end
-- add thumbnails
if (thumbnail == true) and not (json.thumbnails == nil) then
local thumb = nil
local thumb_height = -1
for _, thumb_info in ipairs(json.thumbnails) do
if not (thumb_info.url == nil) then
if (allthumbs == true) then
msg.verbose("adding thumbnail")
mp.commandv("video-add", thumb_info.url, "auto")
thumb_height = 0
elseif ((thumb_info.height or 0) > thumb_height) then
thumb = thumb_info.url
thumb_height = thumb_info.height or 0
end
end
end
if not (thumb == nil) then
msg.verbose("adding thumbnail")
mp.commandv("video-add", thumb, "auto")
elseif (thumb_height == -1) then
msg.verbose("No thumbnail url")
end
end
-- add chapters
if json.chapters then
msg.debug("Adding pre-parsed chapters")
@ -859,6 +885,8 @@ function run_ytdl_hook(url)
local format = mp.get_property("options/ytdl-format")
local raw_options = mp.get_property_native("options/ytdl-raw-options")
local allsubs = true
local thumbnail = false
local allthumbs = false
local proxy = nil
local use_playlist = false
@ -890,6 +918,11 @@ function run_ytdl_hook(url)
end
if (param == "sub-lang" or param == "sub-langs" or param == "srt-lang") and (arg ~= "") then
allsubs = false
elseif (param == "write-thumbnail") then
thumbnail = true
elseif (param == "write-all-thumbnails") then
thumbnail = true
allthumbs = true
elseif (param == "proxy") and (arg ~= "") then
proxy = arg
elseif (param == "yes-playlist") then
@ -1061,7 +1094,7 @@ function run_ytdl_hook(url)
elseif self_redirecting_url and #json.entries == 1 then
msg.verbose("Playlist with single entry detected.")
add_single_video(json.entries[1])
add_single_video(json.entries[1], thumbnail, allthumbs)
else
local playlist_index = parse_yt_playlist(url, json)
local playlist = {"#EXTM3U"}
@ -1115,7 +1148,7 @@ function run_ytdl_hook(url)
end
else -- probably a video
add_single_video(json)
add_single_video(json, thumbnail, allthumbs)
end
msg.debug('script running time: '..os.clock()-start_time..' seconds')
end