summaryrefslogtreecommitdiff
path: root/mpv/scripts/blacklist-extensions.lua
blob: a8ec638a9f4ef9a871a588c71c4fd28c21dfec7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
-- From: https://github.com/occivink/mpv-scripts

opts = {
    blacklist="",
    whitelist="",
    remove_files_without_extension = false,
    oneshot = true,
}
(require 'mp.options').read_options(opts)
local msg = require 'mp.msg'

function split(input)
    local ret = {}
    for str in string.gmatch(input, "([^,]+)") do
        ret[#ret + 1] = str
    end
    return ret
end

opts.blacklist = split(opts.blacklist)
opts.whitelist = split(opts.whitelist)

local exclude
if #opts.whitelist > 0 then
    exclude = function(extension)
        for _, ext in pairs(opts.whitelist) do
            if extension == ext then
                return false
            end
        end
        return true
    end
elseif #opts.blacklist > 0 then
    exclude = function(extension)
        for _, ext in pairs(opts.blacklist) do
            if extension == ext then
                return true
            end
        end
        return false
    end
else
    return
end

function should_remove(filename)
    if string.find(filename, "://") then
        return false
    end
    local extension = string.match(filename, "%.([^./]+)$")
    if not extension and opts.remove_file_without_extension then
        return true
    end
    if extension and exclude(string.lower(extension)) then
        return true
    end
    return false
end

function process(playlist_count)
    if playlist_count < 2 then return end
    if opts.oneshot then
        mp.unobserve_property(observe)
    end
    local playlist = mp.get_property_native("playlist")
    local removed = 0
    for i = #playlist, 1, -1 do
        if should_remove(playlist[i].filename) then
            mp.commandv("playlist-remove", i-1)
            removed = removed + 1
        end
    end
    if removed == #playlist then
        msg.warn("Removed eveything from the playlist")
    end
end

function observe(k,v) process(v) end

mp.observe_property("playlist-count", "number", observe)