Skip to content

Commit

Permalink
ytdl_hook: make path and json available to other scripts
Browse files Browse the repository at this point in the history
It's useful for user scripts to be able to use the same ytdl binary that
ytdl_hook uses without having to replicate ytdl_hook's process of
searching for the ytdl binary.

Some user scripts might also find it useful to be able to access ytdl's
json output that the ytdl_hook already receives, sparing user scripts
from having to make a duplicate ytdl binary invocation to get the json
output.

Providing just the json output is not enough though, as ytdl doesn't communicate
errors though it -- if an error occurs, ytdl provides no json output and instead
prints to stderr. So without stderr, there is no way for user scripts to figure
out why ytdl has failed: no such username / video id, the channel is not live
yet, etc. Because of that, the entire result of the subprocess call is provided
to the user scripts, containing stdout (json), stderr, ytdl's exit code, etc.
  • Loading branch information
nurupo committed May 26, 2024
1 parent 2fa66b8 commit 6700176
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
13 changes: 13 additions & 0 deletions DOCS/man/input.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3526,6 +3526,19 @@ Property list
and not using raw mode, the underlying content will be given (e.g. strings will be
printed directly, rather than quoted and JSON-escaped).

``user-data/ytdl``
Data shared by the builtin ytdl hook script.

``user-data/ytdl/path``
Path to the ytdl executable, if found, or an empty string otherwise.
The property is not set until the script attempts to find the ytdl
executable, i.e. until an URL is being loaded by the script.

``user-data/ytdl/json-subprocess-result``
Result of executing ytdl to retrieve the JSON data of the URL being
loaded. The format is the same as ``subprocess``'s result, capturing
stdout and stderr.

``menu-data`` (RW)
This property stores the raw menu definition. See `Context Menu`_ section for details.

Expand Down
9 changes: 9 additions & 0 deletions player/lua/ytdl_hook.lua
Original file line number Diff line number Diff line change
Expand Up @@ -972,12 +972,16 @@ function run_ytdl_hook(url)
end

ytdl.searched = true

mp.set_property("user-data/ytdl/path", ytdl.path or "")
end

if result.killed_by_us then
return
end

mp.set_property_native("user-data/ytdl/json-subprocess-result", result)

local json = result.stdout
local parse_err = nil

Expand Down Expand Up @@ -1187,3 +1191,8 @@ mp.add_hook("on_preloaded", 10, function ()
chapter_list = {}
end
end)

mp.add_hook("on_after_end_file", 50, function ()
msg.verbose("Deleting per-file properties")
mp.del_property("user-data/ytdl/json-subprocess-result")
end)

0 comments on commit 6700176

Please sign in to comment.