0
0
mirror of https://github.com/mpv-player/mpv.git synced 2024-09-20 12:02:23 +02:00

ytdl_hook: add option to exclude URLs from being parsed

This is more of a niche usecase than --ytdl-format and --ytdl-raw-options,
so a simple script option should be enough.

Either create lua-settings/ytdl_hook.conf with
'exclude=example.com,sub.example.com' option or
"--script-opts=ytdl_hook-exclude=example.com,sub.example.com"
This commit is contained in:
Ricardo Constantino 2017-07-08 14:43:37 +01:00
parent b1165ce3a2
commit 042e98f4c9
No known key found for this signature in database
GPG Key ID: EFD16019AE4FF531
2 changed files with 50 additions and 4 deletions

View File

@ -531,6 +531,26 @@ Program Behavior
If the script can't do anything with an URL, it will do nothing. If the script can't do anything with an URL, it will do nothing.
The `exclude` script option accepts a comma-separated list of URL patterns
which mpv should not use with youtube-dl. The patterns are matched after
the ``http(s)://`` part of the URL.
``^`` matches the beginning of the URL, ``$`` matches its end, and you
should use ``%`` before any of the characters ``^$()%,.[]*+-?`` to match
that character.
.. admonition:: Example
``--script-opts=ytdl_hook-exclude='^youtube%.com'`` will exclude any
URL that starts with ``http://youtube.com`` or
``https://youtube.com``.
``--script-opts=ytdl_hook-exclude='%.mkv$,%.mp4$'`` will exclude any
URL that ends with ``.mkv`` or ``.mp4``.
See more `lua patterns here`__.
__ https://www.lua.org/manual/5.1/manual.html#5.4.1
``--ytdl-format=<best|worst|mp4|webm|...>`` ``--ytdl-format=<best|worst|mp4|webm|...>``
Video format/quality that is directly passed to youtube-dl. The possible Video format/quality that is directly passed to youtube-dl. The possible
values are specific to the website and the video, for a given url the values are specific to the website and the video, for a given url the

View File

@ -1,9 +1,15 @@
local utils = require 'mp.utils' local utils = require 'mp.utils'
local msg = require 'mp.msg' local msg = require 'mp.msg'
local options = require 'mp.options'
local o = {
exclude = ""
}
local ytdl = { local ytdl = {
path = "youtube-dl", path = "youtube-dl",
searched = false searched = false,
blacklisted = {}
} }
local chapter_list = {} local chapter_list = {}
@ -93,6 +99,27 @@ local function extract_chapters(data, video_length)
return ret return ret
end end
local function is_blacklisted(url)
if o.blacklist == "" then return false end
if #ytdl.blacklisted == 0 then
local joined = o.blacklist
while joined:match(',?[^,]+') do
local _, e, domain = joined:find(',?([^,]+)')
table.insert(ytdl.blacklisted, domain)
joined = joined:sub(e+1)
end
end
if #ytdl.blacklisted > 0 then
url = url:match('https?://(.+)')
for _, exclude in ipairs(ytdl.blacklisted) do
if url:match(exclude) then
return true
end
end
end
return false
end
local function edl_track_joined(fragments, protocol, is_live) local function edl_track_joined(fragments, protocol, is_live)
if not (type(fragments) == "table") or not fragments[1] then if not (type(fragments) == "table") or not fragments[1] then
msg.debug("No fragments to join into EDL") msg.debug("No fragments to join into EDL")
@ -244,9 +271,8 @@ end
mp.add_hook("on_load", 10, function () mp.add_hook("on_load", 10, function ()
local url = mp.get_property("stream-open-filename") local url = mp.get_property("stream-open-filename")
local start_time = os.clock() local start_time = os.clock()
if (url:find("ytdl://") == 1) or
if (url:find("http://") == 1) or (url:find("https://") == 1) ((url:find("https?://") == 1) and not is_blacklisted(url)) then
or (url:find("ytdl://") == 1) then
-- check for youtube-dl in mpv's config dir -- check for youtube-dl in mpv's config dir
if not (ytdl.searched) then if not (ytdl.searched) then