2014-05-25 17:52:26 +00:00
|
|
|
-- This script automatically loads playlist entries before and after the
|
|
|
|
-- the currently played file. It does so by scanning the directory a file is
|
|
|
|
-- located in when starting playback. It sorts the directory entries
|
|
|
|
-- alphabetically, and adds entries before and after the current file to
|
|
|
|
-- the internal playlist. (It stops if the it would add an already existing
|
|
|
|
-- playlist entry at the same position - this makes it "stable".)
|
2015-08-20 20:04:36 +00:00
|
|
|
-- Add at most 5000 * 2 files when starting a file (before + after).
|
|
|
|
MAXENTRIES = 5000
|
2014-05-25 17:52:26 +00:00
|
|
|
|
2016-12-05 21:09:28 +00:00
|
|
|
local options = require 'mp.options'
|
|
|
|
|
|
|
|
o = {
|
|
|
|
disabled = false
|
|
|
|
}
|
|
|
|
options.read_options(o)
|
|
|
|
|
2015-04-21 16:30:52 +00:00
|
|
|
function Set (t)
|
|
|
|
local set = {}
|
|
|
|
for _, v in pairs(t) do set[v] = true end
|
|
|
|
return set
|
|
|
|
end
|
|
|
|
|
|
|
|
EXTENSIONS = Set {
|
|
|
|
'mkv', 'avi', 'mp4', 'ogv', 'webm', 'rmvb', 'flv', 'wmv', 'mpeg', 'mpg', 'm4v', '3gp',
|
|
|
|
'mp3', 'wav', 'ogv', 'flac', 'm4a', 'wma',
|
|
|
|
}
|
|
|
|
|
2014-05-26 19:46:01 +00:00
|
|
|
mputils = require 'mp.utils'
|
|
|
|
|
2014-05-25 17:52:26 +00:00
|
|
|
function add_files_at(index, files)
|
|
|
|
index = index - 1
|
|
|
|
local oldcount = mp.get_property_number("playlist-count", 1)
|
|
|
|
for i = 1, #files do
|
|
|
|
mp.commandv("loadfile", files[i], "append")
|
2016-07-14 18:04:59 +00:00
|
|
|
mp.commandv("playlist-move", oldcount + i - 1, index + i - 1)
|
2014-05-25 17:52:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-21 16:30:52 +00:00
|
|
|
function get_extension(path)
|
2015-10-18 04:26:35 +00:00
|
|
|
match = string.match(path, "%.([^%.]+)$" )
|
|
|
|
if match == nil then
|
|
|
|
return "nomatch"
|
|
|
|
else
|
|
|
|
return match
|
|
|
|
end
|
2015-04-21 16:30:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
table.filter = function(t, iter)
|
|
|
|
for i = #t, 1, -1 do
|
|
|
|
if not iter(t[i]) then
|
|
|
|
table.remove(t, i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-25 17:52:26 +00:00
|
|
|
function find_and_add_entries()
|
|
|
|
local path = mp.get_property("path", "")
|
2014-05-26 19:46:01 +00:00
|
|
|
local dir, filename = mputils.split_path(path)
|
2016-12-05 21:09:28 +00:00
|
|
|
if o.disabled or #dir == 0 then
|
2014-05-25 17:52:26 +00:00
|
|
|
return
|
|
|
|
end
|
2015-08-13 01:28:30 +00:00
|
|
|
local pl_count = mp.get_property_number("playlist-count", 1)
|
|
|
|
if (pl_count > 1 and autoload == nil) or
|
|
|
|
(pl_count == 1 and EXTENSIONS[string.lower(get_extension(filename))] == nil) then
|
2015-08-08 00:52:44 +00:00
|
|
|
return
|
2015-08-13 01:28:30 +00:00
|
|
|
else
|
|
|
|
autoload = true
|
2015-08-08 00:52:44 +00:00
|
|
|
end
|
2015-04-21 17:09:44 +00:00
|
|
|
|
2014-05-26 19:46:01 +00:00
|
|
|
local files = mputils.readdir(dir, "files")
|
2015-01-03 13:48:33 +00:00
|
|
|
if files == nil then
|
|
|
|
return
|
|
|
|
end
|
2015-04-21 16:30:52 +00:00
|
|
|
table.filter(files, function (v, k)
|
2017-05-13 05:16:45 +00:00
|
|
|
if string.match(v, "^%.") then
|
|
|
|
return false
|
|
|
|
end
|
2015-04-21 16:30:52 +00:00
|
|
|
local ext = get_extension(v)
|
|
|
|
if ext == nil then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return EXTENSIONS[string.lower(ext)]
|
|
|
|
end)
|
2015-04-18 18:33:35 +00:00
|
|
|
table.sort(files, function (a, b)
|
2016-02-02 18:28:33 +00:00
|
|
|
local len = string.len(a) - string.len(b)
|
|
|
|
if len ~= 0 then -- case for ordering filename ending with such as X.Y.Z
|
|
|
|
local ext = string.len(get_extension(a)) + 1
|
2017-05-04 13:23:39 +00:00
|
|
|
a = string.sub(a, 1, -ext)
|
|
|
|
b = string.sub(b, 1, -ext)
|
2016-02-02 18:28:33 +00:00
|
|
|
end
|
2015-04-18 18:33:35 +00:00
|
|
|
return string.lower(a) < string.lower(b)
|
|
|
|
end)
|
2015-04-21 17:09:44 +00:00
|
|
|
|
2014-10-26 13:34:46 +00:00
|
|
|
if dir == "." then
|
|
|
|
dir = ""
|
|
|
|
end
|
2015-04-21 17:09:44 +00:00
|
|
|
|
2014-05-25 17:52:26 +00:00
|
|
|
local pl = mp.get_property_native("playlist", {})
|
|
|
|
local pl_current = mp.get_property_number("playlist-pos", 0) + 1
|
|
|
|
-- Find the current pl entry (dir+"/"+filename) in the sorted dir list
|
|
|
|
local current
|
|
|
|
for i = 1, #files do
|
|
|
|
if files[i] == filename then
|
|
|
|
current = i
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if current == nil then
|
|
|
|
return
|
|
|
|
end
|
2015-04-21 17:09:44 +00:00
|
|
|
|
2014-05-25 17:52:26 +00:00
|
|
|
local append = {[-1] = {}, [1] = {}}
|
2014-10-26 13:34:37 +00:00
|
|
|
for direction = -1, 1, 2 do -- 2 iterations, with direction = -1 and +1
|
2014-05-25 17:52:26 +00:00
|
|
|
for i = 1, MAXENTRIES do
|
2014-10-26 13:34:37 +00:00
|
|
|
local file = files[current + i * direction]
|
|
|
|
local pl_e = pl[pl_current + i * direction]
|
2014-05-25 17:52:26 +00:00
|
|
|
if file == nil or file[1] == "." then
|
|
|
|
break
|
|
|
|
end
|
2015-04-21 17:09:44 +00:00
|
|
|
|
2014-10-26 13:34:46 +00:00
|
|
|
local filepath = dir .. file
|
2014-05-25 17:52:26 +00:00
|
|
|
if pl_e then
|
|
|
|
-- If there's a playlist entry, and it's the same file, stop.
|
2014-10-26 13:34:46 +00:00
|
|
|
if pl_e.filename == filepath then
|
2014-05-25 17:52:26 +00:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2015-04-21 17:09:44 +00:00
|
|
|
|
2014-10-26 13:34:37 +00:00
|
|
|
if direction == -1 then
|
2014-05-25 18:17:03 +00:00
|
|
|
if pl_current == 1 then -- never add additional entries in the middle
|
2014-05-25 17:52:26 +00:00
|
|
|
mp.msg.info("Prepending " .. file)
|
2014-10-26 13:34:46 +00:00
|
|
|
table.insert(append[-1], 1, filepath)
|
2014-05-25 17:52:26 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
mp.msg.info("Adding " .. file)
|
2014-10-26 13:34:46 +00:00
|
|
|
table.insert(append[1], filepath)
|
2014-05-25 17:52:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-04-21 17:09:44 +00:00
|
|
|
|
2014-05-25 17:52:26 +00:00
|
|
|
add_files_at(pl_current + 1, append[1])
|
|
|
|
add_files_at(pl_current, append[-1])
|
|
|
|
end
|
|
|
|
|
|
|
|
mp.register_event("start-file", find_and_add_entries)
|