From 390e995b6a03539372c0c187c01c807b86cb3c86 Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 21 Feb 2020 00:20:22 +0100 Subject: ytdl_hook: delay-load interleaved files (Or if it's about HLS, just "muxed"/multiplexed streams.) This only affects all_formats=yes,skip_muxed=no modes. --- player/lua/ytdl_hook.lua | 59 +++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/player/lua/ytdl_hook.lua b/player/lua/ytdl_hook.lua index 687f765865..8951e06c44 100644 --- a/player/lua/ytdl_hook.lua +++ b/player/lua/ytdl_hook.lua @@ -355,23 +355,22 @@ local function formats_to_edl(json, formats, use_all_formats) return nil end - local media_type = nil - local codec = nil - local interlaved_streams = false + local tracks = {} if track.vcodec and track.vcodec ~= "none" then - media_type = "video" - codec = track.vcodec + tracks[#tracks + 1] = { + media_type = "video", + codec = track.vcodec, + } end -- Tries to follow the strange logic that vcodec unset means it's -- an audio stream, even if acodec is (supposedly) sometimes unset. - if (not codec) or (track.acodec and track.acodec ~= "none") then - if codec then - interlaved_streams = true - end - media_type = "audio" - codec = track.acodec + if (#tracks == 0) or (track.acodec and track.acodec ~= "none") then + tracks[#tracks + 1] = { + media_type = "audio", + codec = track.acodec, + } end - if not media_type then + if #tracks == 0 then return nil end @@ -381,33 +380,47 @@ local function formats_to_edl(json, formats, use_all_formats) local params = "" if use_all_formats then - if interlaved_streams then - hdr[#hdr + 1] = "!track_meta,title=muxed-" .. index - as_muxed = o.skip_muxed - else + if #tracks > 1 and o.skip_muxed then + as_muxed = true + tracks = {} -- skip + end + for _, sub in ipairs(tracks) do -- A single track that is either audio or video. Delay load it. - local codec = map_codec_to_mpv(codec) - hdr[#hdr + 1] = "!delay_open,media_type=" .. media_type .. + local codec = map_codec_to_mpv(sub.codec) + hdr[#hdr + 1] = "!delay_open,media_type=" .. sub.media_type .. ",codec=" .. (codec or "null") .. ",w=" .. as_integer(track.width) .. ",h=" .. as_integer(track.height) -- Add bitrate information etc. for better user selection. local size = as_integer(track["filesize"]) local byterate = 0 - for _, f in ipairs({"tbr", "vbr", "abr"}) do + local rates = {"tbr", "vbr", "abr"} + if #tracks > 1 then + rates = {({video = "vbr", audio = "abr"})[sub.media_type]} + end + for _, f in ipairs(rates) do local br = as_integer(track[f]) if br > 0 then byterate = math.floor(br * 1000 / 8) break end end - if byterate == 0 and size > 0 and duration > 0 then + if byterate < 1 and size > 0 and duration > 0 and #tracks < 2 then byterate = as_integer(size / duration) end + local title = track.format_note or "" + if #tracks > 1 then + if #title > 0 then + title = title .. " " + end + title = title .. "muxed-" .. index + end hdr[#hdr + 1] = "!track_meta,title=" .. - edl_escape(track.format_note or "") .. - ",byterate=" .. byterate - separate_present[media_type] = true + edl_escape(title) .. ",byterate=" .. byterate + + if #tracks == 1 then + separate_present[sub.media_type] = true + end if duration > 0 then params = params .. ",length=" .. duration -- cgit v1.2.3