Skip to content

Commit

Permalink
ytdl_hook: expose some JSON fields as tags
Browse files Browse the repository at this point in the history
Shows uploader, channel, description fields. This works only if the
media media is constructed as EDL (for youtube it usually does this),
and if the all_formats option is not set to true (does anyone even use
it?). The former usually happens because youtube serves audio and video
separately, though it will not for live HLS/DASH. The latter uses
delayed media opening, which breaks the global_tags mechanism (see
previous commit).
  • Loading branch information
Your Name committed May 11, 2021
1 parent adcf51d commit cbb8f53
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions player/lua/ytdl_hook.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ function iif(cond, if_true, if_false)
return if_false
end

-- youtube-dl JSON name to mpv tag name
local tag_list = {
["uploader"] = "uploader",
["channel_url"] = "channel_url",
-- these titles tend to be a bit too long, so hide them on the terminal
-- (default --display-tags does not include this name)
["description"] = "ytdl_description",
-- "title" is handled by force-media-title
}

local safe_protos = Set {
"http", "https", "ftp", "ftps",
"rtmp", "rtmps", "rtmpe", "rtmpt", "rtmpts", "rtmpte",
Expand Down Expand Up @@ -341,6 +351,20 @@ local function as_integer(v, def)
return def
end

local function tags_to_edl(json)
local tags = {}
for json_name, mp_name in pairs(tag_list) do
local v = json[json_name]
if v then
tags[#tags + 1] = mp_name .. "=" .. edl_escape(tostring(v))
end
end
if #tags == 0 then
return nil
end
return "!global_tags," .. table.concat(tags, ",")
end

-- Convert a format list from youtube-dl to an EDL URL, or plain URL.
-- json: full json blob by youtube-dl
-- formats: format list by youtube-dl
Expand Down Expand Up @@ -475,6 +499,11 @@ local function formats_to_edl(json, formats, use_all_formats)
if #streams == 1 and single_url then
res.url = single_url
elseif #streams > 0 then
local tags = tags_to_edl(json)
if tags then
-- not a stream; just for the sake of concatenating the EDL string
streams[#streams + 1] = tags
end
res.url = "edl://" .. table.concat(streams, ";")
else
return nil
Expand Down

0 comments on commit cbb8f53

Please sign in to comment.