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
|
2020-02-14 06:53:33 +00:00
|
|
|
-- the internal playlist. (It stops if it would add an already existing
|
2014-05-25 17:52:26 +00:00
|
|
|
-- 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).
|
2019-09-24 20:16:30 +00:00
|
|
|
|
|
|
|
--[[
|
2020-02-14 06:53:33 +00:00
|
|
|
To configure this script use file autoload.conf in directory script-opts (the "script-opts"
|
2019-09-24 20:16:30 +00:00
|
|
|
directory must be in the mpv configuration directory, typically ~/.config/mpv/).
|
|
|
|
|
|
|
|
Example configuration would be:
|
|
|
|
|
2020-02-14 06:53:33 +00:00
|
|
|
disabled=no
|
|
|
|
images=no
|
|
|
|
videos=yes
|
|
|
|
audio=yes
|
2021-06-23 17:14:56 +00:00
|
|
|
ignore_hidden=yes
|
2019-09-24 20:16:30 +00:00
|
|
|
|
|
|
|
--]]
|
|
|
|
|
2015-08-20 20:04:36 +00:00
|
|
|
MAXENTRIES = 5000
|
2014-05-25 17:52:26 +00:00
|
|
|
|
2018-03-10 12:42:35 +00:00
|
|
|
local msg = require 'mp.msg'
|
2016-12-05 21:09:28 +00:00
|
|
|
local options = require 'mp.options'
|
2018-03-10 12:42:35 +00:00
|
|
|
local utils = require 'mp.utils'
|
2016-12-05 21:09:28 +00:00
|
|
|
|
|
|
|
o = {
|
2019-09-24 20:16:30 +00:00
|
|
|
disabled = false,
|
|
|
|
images = true,
|
|
|
|
videos = true,
|
2021-06-23 17:14:56 +00:00
|
|
|
audio = true,
|
|
|
|
ignore_hidden = true
|
2016-12-05 21:09:28 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
|
2019-09-24 20:16:30 +00:00
|
|
|
function SetUnion (a,b)
|
|
|
|
local res = {}
|
|
|
|
for k in pairs(a) do res[k] = true end
|
|
|
|
for k in pairs(b) do res[k] = true end
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
|
|
|
EXTENSIONS_VIDEO = Set {
|
2022-09-20 18:17:25 +00:00
|
|
|
'3g2', '3gp', 'avi', 'flv', 'm2ts', 'm4v', 'mj2', 'mkv', 'mov',
|
|
|
|
'mp4', 'mpeg', 'mpg', 'ogv', 'rmvb', 'webm', 'wmv', 'y4m'
|
2019-09-24 20:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EXTENSIONS_AUDIO = Set {
|
2022-09-20 18:17:25 +00:00
|
|
|
'aiff', 'ape', 'au', 'flac', 'm4a', 'mka', 'mp3', 'oga', 'ogg',
|
|
|
|
'ogm', 'opus', 'wav', 'wma'
|
2015-04-21 16:30:52 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 20:16:30 +00:00
|
|
|
EXTENSIONS_IMAGES = Set {
|
2022-09-20 18:17:25 +00:00
|
|
|
'avif', 'bmp', 'gif', 'j2k', 'jp2', 'jpeg', 'jpg', 'jxl', 'png',
|
|
|
|
'svg', 'tga', 'tif', 'tiff', 'webp'
|
2019-09-24 20:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EXTENSIONS = Set {}
|
|
|
|
if o.videos then EXTENSIONS = SetUnion(EXTENSIONS, EXTENSIONS_VIDEO) end
|
|
|
|
if o.audio then EXTENSIONS = SetUnion(EXTENSIONS, EXTENSIONS_AUDIO) end
|
|
|
|
if o.images then EXTENSIONS = SetUnion(EXTENSIONS, EXTENSIONS_IMAGES) end
|
|
|
|
|
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
|
|
|
|
|
2017-08-15 16:51:50 +00:00
|
|
|
-- splitbynum and alnumcomp from alphanum.lua (C) Andre Bogus
|
|
|
|
-- Released under the MIT License
|
|
|
|
-- http://www.davekoelle.com/files/alphanum.lua
|
|
|
|
|
|
|
|
-- split a string into a table of number and string values
|
|
|
|
function splitbynum(s)
|
|
|
|
local result = {}
|
|
|
|
for x, y in (s or ""):gmatch("(%d*)(%D*)") do
|
|
|
|
if x ~= "" then table.insert(result, tonumber(x)) end
|
|
|
|
if y ~= "" then table.insert(result, y) end
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
function clean_key(k)
|
|
|
|
k = (' '..k..' '):gsub("%s+", " "):sub(2, -2):lower()
|
|
|
|
return splitbynum(k)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- compare two strings
|
|
|
|
function alnumcomp(x, y)
|
|
|
|
local xt, yt = clean_key(x), clean_key(y)
|
|
|
|
for i = 1, math.min(#xt, #yt) do
|
|
|
|
local xe, ye = xt[i], yt[i]
|
|
|
|
if type(xe) == "string" then ye = tostring(ye)
|
|
|
|
elseif type(ye) == "string" then xe = tostring(xe) end
|
|
|
|
if xe ~= ye then return xe < ye end
|
|
|
|
end
|
|
|
|
return #xt < #yt
|
|
|
|
end
|
|
|
|
|
2018-03-10 12:42:35 +00:00
|
|
|
local autoloaded = nil
|
|
|
|
|
2022-10-23 14:51:50 +00:00
|
|
|
function get_playlist_filenames()
|
|
|
|
local filenames = {}
|
|
|
|
for n = 0, pl_count - 1, 1 do
|
|
|
|
local filename = mp.get_property('playlist/'..n..'/filename')
|
|
|
|
local _, file = utils.split_path(filename)
|
|
|
|
filenames[file] = true
|
|
|
|
end
|
|
|
|
return filenames
|
|
|
|
end
|
|
|
|
|
2014-05-25 17:52:26 +00:00
|
|
|
function find_and_add_entries()
|
|
|
|
local path = mp.get_property("path", "")
|
2018-03-10 12:42:35 +00:00
|
|
|
local dir, filename = utils.split_path(path)
|
|
|
|
msg.trace(("dir: %s, filename: %s"):format(dir, filename))
|
|
|
|
if o.disabled then
|
|
|
|
msg.verbose("stopping: autoload disabled")
|
2018-06-20 21:06:27 +00:00
|
|
|
return
|
2018-03-10 12:42:35 +00:00
|
|
|
elseif #dir == 0 then
|
|
|
|
msg.verbose("stopping: not a local path")
|
2014-05-25 17:52:26 +00:00
|
|
|
return
|
|
|
|
end
|
2018-03-10 12:42:35 +00:00
|
|
|
|
2022-10-23 14:51:50 +00:00
|
|
|
pl_count = mp.get_property_number("playlist-count", 1)
|
2018-03-10 12:42:35 +00:00
|
|
|
-- check if this is a manually made playlist
|
|
|
|
if (pl_count > 1 and autoloaded == nil) or
|
2015-08-13 01:28:30 +00:00
|
|
|
(pl_count == 1 and EXTENSIONS[string.lower(get_extension(filename))] == nil) then
|
2019-09-24 20:16:30 +00:00
|
|
|
msg.verbose("stopping: manually made playlist")
|
2015-08-08 00:52:44 +00:00
|
|
|
return
|
2015-08-13 01:28:30 +00:00
|
|
|
else
|
2018-03-10 12:42:35 +00:00
|
|
|
autoloaded = true
|
2015-08-08 00:52:44 +00:00
|
|
|
end
|
2015-04-21 17:09:44 +00:00
|
|
|
|
2018-03-10 12:42:35 +00:00
|
|
|
local pl = mp.get_property_native("playlist", {})
|
|
|
|
local pl_current = mp.get_property_number("playlist-pos-1", 1)
|
|
|
|
msg.trace(("playlist-pos-1: %s, playlist: %s"):format(pl_current,
|
|
|
|
utils.to_string(pl)))
|
|
|
|
|
|
|
|
local files = utils.readdir(dir, "files")
|
2015-01-03 13:48:33 +00:00
|
|
|
if files == nil then
|
2019-09-24 20:16:30 +00:00
|
|
|
msg.verbose("no other files in directory")
|
2015-01-03 13:48:33 +00:00
|
|
|
return
|
|
|
|
end
|
2015-04-21 16:30:52 +00:00
|
|
|
table.filter(files, function (v, k)
|
2021-06-23 17:17:34 +00:00
|
|
|
-- The current file could be a hidden file, ignoring it doesn't load other
|
|
|
|
-- files from the current directory.
|
|
|
|
if (o.ignore_hidden and not (v == filename) and string.match(v, "^%.")) then
|
2017-05-13 05:16:45 +00:00
|
|
|
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)
|
2017-08-15 16:51:50 +00:00
|
|
|
table.sort(files, alnumcomp)
|
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
|
|
|
-- 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
|
2018-03-10 12:42:35 +00:00
|
|
|
msg.trace("current file position in files: "..current)
|
2015-04-21 17:09:44 +00:00
|
|
|
|
2014-05-25 17:52:26 +00:00
|
|
|
local append = {[-1] = {}, [1] = {}}
|
2022-10-23 14:51:50 +00:00
|
|
|
local filenames = get_playlist_filenames()
|
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]
|
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
|
2022-10-23 14:51:50 +00:00
|
|
|
-- skip files already in playlist
|
|
|
|
if filenames[file] then break 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
|
2018-03-10 12:42:35 +00:00
|
|
|
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
|
2018-03-10 12:42:35 +00:00
|
|
|
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)
|