diff options
Diffstat (limited to 'TOOLS/lua')
-rw-r--r-- | TOOLS/lua/acompressor.lua | 11 | ||||
-rw-r--r-- | TOOLS/lua/audio-hotplug-test.lua | 6 | ||||
-rw-r--r-- | TOOLS/lua/autocrop.lua | 364 | ||||
-rw-r--r-- | TOOLS/lua/autodeint.lua | 4 | ||||
-rw-r--r-- | TOOLS/lua/autoload.lua | 385 | ||||
-rw-r--r-- | TOOLS/lua/command-test.lua | 23 | ||||
-rw-r--r-- | TOOLS/lua/cycle-deinterlace-pullup.lua | 2 | ||||
-rw-r--r-- | TOOLS/lua/nan-test.lua | 37 | ||||
-rw-r--r-- | TOOLS/lua/observe-all.lua | 8 | ||||
-rw-r--r-- | TOOLS/lua/ontop-playback.lua | 2 | ||||
-rw-r--r-- | TOOLS/lua/osd-test.lua | 35 | ||||
-rw-r--r-- | TOOLS/lua/pause-when-minimize.lua | 2 | ||||
-rw-r--r-- | TOOLS/lua/skip-logo.lua | 21 | ||||
-rw-r--r-- | TOOLS/lua/status-line.lua | 2 | ||||
-rw-r--r-- | TOOLS/lua/test-hooks.lua | 32 |
15 files changed, 717 insertions, 217 deletions
diff --git a/TOOLS/lua/acompressor.lua b/TOOLS/lua/acompressor.lua index 5fc5063e8d..e94dc3e0fb 100644 --- a/TOOLS/lua/acompressor.lua +++ b/TOOLS/lua/acompressor.lua @@ -2,7 +2,7 @@ -- filter including key bindings for adjusting parameters. -- -- See https://ffmpeg.org/ffmpeg-filters.html#acompressor for explanation --- of the parameteres. +-- of the parameters. local mp = require 'mp' local options = require 'mp.options' @@ -53,8 +53,10 @@ local params = { } local function parse_value(value) - -- Using nil here because tonumber differs between lua 5.1 and 5.2 when parsing fractions in combination with explicit base argument set to 10. - -- And we can't omit it because gsub returns 2 values which would get unpacked and cause more problems. Gotta love scripting languages. + -- Using nil here because tonumber differs between lua 5.1 and 5.2 when + -- parsing fractions in combination with explicit base argument set to 10. + -- And we can't omit it because gsub returns 2 values which would get + -- unpacked and cause more problems. Gotta love scripting languages. return tonumber(value:gsub('dB$', ''), nil) end @@ -76,7 +78,8 @@ local function show_osd(filter) for _,param in ipairs(params) do local value = parse_value(filter.params[param.name]) if not (param.hide_default and value == o['default_' .. param.name]) then - pretty[#pretty+1] = string.format('%s: %g%s', param.name:gsub("^%l", string.upper), value, param.dB) + pretty[#pretty+1] = string.format('%s: %g%s', param.name:gsub("^%l", string.upper), + value, param.dB) end end diff --git a/TOOLS/lua/audio-hotplug-test.lua b/TOOLS/lua/audio-hotplug-test.lua index 8dedc68cbe..e0ef223c0c 100644 --- a/TOOLS/lua/audio-hotplug-test.lua +++ b/TOOLS/lua/audio-hotplug-test.lua @@ -1,8 +1,6 @@ -local utils = require("mp.utils") - -mp.observe_property("audio-device-list", "native", function(name, val) +mp.observe_property("audio-device-list", "native", function(_, val) print("Audio device list changed:") - for index, e in ipairs(val) do + for _, e in ipairs(val) do print(" - '" .. e.name .. "' (" .. e.description .. ")") end end) diff --git a/TOOLS/lua/autocrop.lua b/TOOLS/lua/autocrop.lua index 2316a46d00..eff659e50a 100644 --- a/TOOLS/lua/autocrop.lua +++ b/TOOLS/lua/autocrop.lua @@ -1,114 +1,298 @@ --- This script uses the lavfi cropdetect filter to automatically --- insert a crop filter with appropriate parameters for the currently --- playing video. --- --- It registers the key-binding "C" (shift+c), which when pressed, --- inserts the filter vf=lavfi=cropdetect. After 1 second, it then --- inserts the filter vf=crop=w:h:x:y, where w,h,x,y are determined --- from the vf-metadata gathered by cropdetect. The cropdetect filter --- is removed immediately after the crop filter is inserted as it is --- no longer needed. --- --- If the "C" key is pressed again, the crop filter is removed --- restoring playback to its original state. --- --- Since the crop parameters are determined from the 1 second of video --- between inserting the cropdetect and crop filters, the "C" key --- should be pressed at a position in the video where the crop region --- is unambiguous (i.e., not a black frame, black background title --- card, or dark scene). --- --- The default delay between insertion of the cropdetect and --- crop filters may be overridden by adding --- --- --script-opts=autocrop.detect_seconds=<number of seconds> --- --- to mpv's arguments. This may be desirable to allow cropdetect more --- time to collect data. -require "mp.msg" - -script_name = mp.get_script_name() -cropdetect_label = string.format("%s-cropdetect", script_name) -crop_label = string.format("%s-crop", script_name) - --- number of seconds to gather cropdetect data -detect_seconds = tonumber(mp.get_opt(string.format("%s.detect_seconds", script_name))) -if not detect_seconds then - detect_seconds = 1 +--[[ +This script uses the lavfi cropdetect filter and the video-crop property to +automatically crop the currently playing video with appropriate parameters. + +It automatically crops the video when playback starts. + +You can also manually crop the video by pressing the "C" (shift+c) key. +Pressing it again undoes the crop. + +The workflow is as follows: First, it inserts the cropdetect filter. After +<detect_seconds> (default is 1) seconds, it then sets video-crop based on the +vf-metadata values gathered by cropdetect. The cropdetect filter is removed +after video-crop is set as it is no longer needed. + +Since the crop parameters are determined from the 1 second of video between +inserting the cropdetect filter and setting video-crop, the "C" key should be +pressed at a position in the video where the crop region is unambiguous (i.e., +not a black frame, black background title card, or dark scene). + +If non-copy-back hardware decoding is in use, hwdec is temporarily disabled for +the duration of cropdetect as the filter would fail otherwise. + +These are the default options. They can be overridden by adding +script-opts-append=autocrop-<parameter>=<value> to mpv.conf. +--]] +local options = { + -- Whether to automatically apply crop at the start of playback. If you + -- don't want to crop automatically, add + -- script-opts-append=autocrop-auto=no to mpv.conf. + auto = true, + -- Delay before starting crop in auto mode. You can try to increase this + -- value to avoid dark scenes or fade ins at beginning. Automatic cropping + -- will not occur if the value is larger than the remaining playback time. + auto_delay = 4, + -- Black threshold for cropdetect. Smaller values will generally result in + -- less cropping. See limit of + -- https://ffmpeg.org/ffmpeg-filters.html#cropdetect + detect_limit = "24/255", + -- The value which the width/height should be divisible by. Smaller + -- values have better detection accuracy. If you have problems with + -- other filters, you can try to set it to 4 or 16. See round of + -- https://ffmpeg.org/ffmpeg-filters.html#cropdetect + detect_round = 2, + -- The ratio of the minimum clip size to the original. A number from 0 to + -- 1. If the picture is over cropped, try adjusting this value. + detect_min_ratio = 0.5, + -- How long to gather cropdetect data. Increasing this may be desirable to + -- allow cropdetect more time to collect data. + detect_seconds = 1, + -- Whether the OSD shouldn't be used when cropdetect and video-crop are + -- applied and removed. + suppress_osd = false, +} + +require "mp.options".read_options(options) + +local cropdetect_label = mp.get_script_name() .. "-cropdetect" + +timers = { + auto_delay = nil, + detect_crop = nil +} + +local hwdec_backup + +local command_prefix = options.suppress_osd and 'no-osd' or '' + +function is_enough_time(seconds) + + -- Plus 1 second for deviation. + local time_needed = seconds + 1 + local playtime_remaining = mp.get_property_native("playtime-remaining") + + return playtime_remaining and time_needed < playtime_remaining +end + +function is_cropable(time_needed) + if mp.get_property_native('current-tracks/video/image') ~= false then + mp.msg.warn("autocrop only works for videos.") + return false + end + + if not is_enough_time(time_needed) then + mp.msg.warn("Not enough time to detect crop.") + return false + end + + return true +end + +function remove_cropdetect() + for _, filter in pairs(mp.get_property_native("vf")) do + if filter.label == cropdetect_label then + mp.command( + string.format("%s vf remove @%s", command_prefix, filter.label)) + + return + end + end end -function del_filter_if_present(label) - -- necessary because mp.command('vf del @label:filter') raises an - -- error if the filter doesn't exist - local vfs = mp.get_property_native("vf") - for i,vf in pairs(vfs) do - if vf["label"] == label then - table.remove(vfs, i) - mp.set_property_native("vf", vfs) - return true +function restore_hwdec() + if hwdec_backup then + mp.set_property("hwdec", hwdec_backup) + hwdec_backup = nil + end +end + +function cleanup() + remove_cropdetect() + + -- Kill all timers. + for index, timer in pairs(timers) do + if timer then + timer:kill() + timers[index] = nil end end - return false + + restore_hwdec() end -function autocrop_start() - -- exit if cropdetection is already in progress - if timer then - mp.msg.warn("already cropdetecting!") +function detect_crop() + local time_needed = options.detect_seconds + + if not is_cropable(time_needed) then return end - -- if there's a crop filter, remove it and exit - if del_filter_if_present(crop_label) then - return + local hwdec_current = mp.get_property("hwdec-current") + if hwdec_current:find("-copy$") == nil and hwdec_current ~= "no" and + hwdec_current ~= "crystalhd" and hwdec_current ~= "rkmpp" then + hwdec_backup = mp.get_property("hwdec") + mp.set_property("hwdec", "no") end - -- insert the cropdetect filter - ret=mp.command( + -- Insert the cropdetect filter. + local limit = options.detect_limit + local round = options.detect_round + + mp.command( string.format( - 'vf add @%s:cropdetect=limit=%f:round=2:reset=0', - cropdetect_label, 24/255 + '%s vf pre @%s:cropdetect=limit=%s:round=%d:reset=0', + command_prefix, cropdetect_label, limit, round ) ) - -- wait to gather data - timer=mp.add_timeout(detect_seconds, do_crop) + + -- Wait to gather data. + timers.detect_crop = mp.add_timeout(time_needed, detect_end) end -function do_crop() - -- get the metadata +function detect_end() + + -- Get the metadata and remove the cropdetect filter. local cropdetect_metadata = mp.get_property_native( - string.format("vf-metadata/%s", cropdetect_label) - ) + "vf-metadata/" .. cropdetect_label) + remove_cropdetect() + + -- Remove the timer of detect crop. + if timers.detect_crop then + timers.detect_crop:kill() + timers.detect_crop = nil + end + + restore_hwdec() + + local meta - -- use it to crop if its valid + -- Verify the existence of metadata. if cropdetect_metadata then - if cropdetect_metadata["lavfi.cropdetect.w"] - and cropdetect_metadata["lavfi.cropdetect.h"] - and cropdetect_metadata["lavfi.cropdetect.x"] - and cropdetect_metadata["lavfi.cropdetect.y"] - then - mp.command(string.format("vf add @%s:lavfi-crop=w=%s:h=%s:x=%s:y=%s", - crop_label, - cropdetect_metadata["lavfi.cropdetect.w"], - cropdetect_metadata["lavfi.cropdetect.h"], - cropdetect_metadata["lavfi.cropdetect.x"], - cropdetect_metadata["lavfi.cropdetect.y"])) - else - mp.msg.error( - "Got empty crop data. You might need to increase detect_seconds." - ) - end + meta = { + w = cropdetect_metadata["lavfi.cropdetect.w"], + h = cropdetect_metadata["lavfi.cropdetect.h"], + x = cropdetect_metadata["lavfi.cropdetect.x"], + y = cropdetect_metadata["lavfi.cropdetect.y"], + } else - mp.msg.error( - "No crop data. Was the cropdetect filter successfully inserted?" - ) - mp.msg.error( - "Does your version of ffmpeg/libav support AVFrame metadata?" + mp.msg.error("No crop data.") + mp.msg.info("Was the cropdetect filter successfully inserted?") + mp.msg.info("Does your version of FFmpeg support AVFrame metadata?") + return + end + + -- Verify that the metadata meets the requirements and convert it. + if meta.w and meta.h and meta.x and meta.y then + local width = mp.get_property_native("width") + local height = mp.get_property_native("height") + + meta = { + w = tonumber(meta.w), + h = tonumber(meta.h), + x = tonumber(meta.x), + y = tonumber(meta.y), + min_w = width * options.detect_min_ratio, + min_h = height * options.detect_min_ratio, + max_w = width, + max_h = height + } + else + mp.msg.error("Got empty crop data.") + mp.msg.info("You might need to increase detect_seconds.") + end + + apply_crop(meta) +end + +function apply_crop(meta) + + -- Verify if it is necessary to crop. + local is_effective = meta.w and meta.h and meta.x and meta.y and + (meta.x > 0 or meta.y > 0 + or meta.w < meta.max_w or meta.h < meta.max_h) + + -- Verify it is not over cropped. + local is_excessive = false + if is_effective and (meta.w < meta.min_w or meta.h < meta.min_h) then + mp.msg.info("The area to be cropped is too large.") + mp.msg.info("You might need to decrease detect_min_ratio.") + is_excessive = true + end + + if not is_effective or is_excessive then + -- Clear any existing crop. + mp.command(string.format("%s set file-local-options/video-crop ''", command_prefix)) + return + end + + -- Apply crop. + mp.command(string.format("%s set file-local-options/video-crop %sx%s+%s+%s", + command_prefix, meta.w, meta.h, meta.x, meta.y)) +end + +function on_start() + + -- Clean up at the beginning. + cleanup() + + -- If auto is not true, exit. + if not options.auto then + return + end + + -- If it is the beginning, wait for detect_crop + -- after auto_delay seconds, otherwise immediately. + local playback_time = mp.get_property_native("playback-time") + local is_delay_needed = playback_time + and options.auto_delay > playback_time + + if is_delay_needed then + + -- Verify if there is enough time for autocrop. + local time_needed = options.auto_delay + options.detect_seconds + + if not is_cropable(time_needed) then + return + end + + timers.auto_delay = mp.add_timeout(time_needed, + function() + detect_crop() + + -- Remove the timer of auto delay. + timers.auto_delay:kill() + timers.auto_delay = nil + end ) + else + detect_crop() + end +end + +function on_toggle() + + -- If it is during auto_delay, kill the timer. + if timers.auto_delay then + timers.auto_delay:kill() + timers.auto_delay = nil + end + + -- Cropped => Remove it. + if mp.get_property("video-crop") ~= "" then + mp.command(string.format("%s set file-local-options/video-crop ''", command_prefix)) + return end - -- remove the cropdetect filter - del_filter_if_present(cropdetect_label) - timer=nil + + -- Detecting => Leave it. + if timers.detect_crop then + mp.msg.warn("Already cropdetecting!") + return + end + + -- Neither => Detect crop. + detect_crop() end -mp.add_key_binding("C", "auto_crop", autocrop_start) +mp.add_key_binding("C", "toggle_crop", on_toggle) +mp.register_event("end-file", cleanup) +mp.register_event("file-loaded", on_start) diff --git a/TOOLS/lua/autodeint.lua b/TOOLS/lua/autodeint.lua index b891c9a838..7615f52406 100644 --- a/TOOLS/lua/autodeint.lua +++ b/TOOLS/lua/autodeint.lua @@ -8,7 +8,7 @@ -- telecined and the interlacing field dominance. -- -- Based on this information, it may set mpv's ``deinterlace`` property (which --- usually inserts the yadif filter), or insert the ``pullup`` filter if the +-- usually inserts the bwdif filter), or insert the ``pullup`` filter if the -- content is telecined. It also sets field dominance with lavfi setfield. -- -- OPTIONS: @@ -87,7 +87,7 @@ function stop_detect() timer = nil end -progressive, interlaced_tff, interlaced_bff, interlaced = 0, 1, 2, 3, 4 +progressive, interlaced_tff, interlaced_bff, interlaced = 0, 1, 2, 3 function judge(label) -- get the metadata diff --git a/TOOLS/lua/autoload.lua b/TOOLS/lua/autoload.lua index 92456bc02d..486801e10d 100644 --- a/TOOLS/lua/autoload.lua +++ b/TOOLS/lua/autoload.lua @@ -2,24 +2,38 @@ -- 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 +-- the internal playlist. (It stops if it would add an already existing -- playlist entry at the same position - this makes it "stable".) -- Add at most 5000 * 2 files when starting a file (before + after). --[[ -To configure this script use file autoload.conf in director script-opts (the "script-opts" +To configure this script use file autoload.conf in directory script-opts (the "script-opts" directory must be in the mpv configuration directory, typically ~/.config/mpv/). +Option `ignore_patterns` is a comma-separated list of patterns (see lua.org/pil/20.2.html). +Additionally to the standard lua patterns, you can also escape commas with `%`, +for example, the option `bak%,x%,,another` will be resolved as patterns `bak,x,` and `another`. +But it does not mean you need to escape all lua patterns twice, +so the option `bak%%,%.mp4,` will be resolved as two patterns `bak%%` and `%.mp4`. + Example configuration would be: -disabled=false -images=false -videos=true -audio=true +disabled=no +images=no +videos=yes +audio=yes +additional_image_exts=list,of,ext +additional_video_exts=list,of,ext +additional_audio_exts=list,of,ext +ignore_hidden=yes +same_type=yes +directory_mode=recursive +ignore_patterns=^~,^bak-,%.bak$ --]] MAXENTRIES = 5000 +MAXDIRSTACK = 20 local msg = require 'mp.msg' local options = require 'mp.options' @@ -29,9 +43,26 @@ o = { disabled = false, images = true, videos = true, - audio = true + audio = true, + additional_image_exts = "", + additional_video_exts = "", + additional_audio_exts = "", + ignore_hidden = true, + same_type = false, + directory_mode = "auto", + ignore_patterns = "" } -options.read_options(o) +options.read_options(o, nil, function(list) + split_option_exts(list.additional_video_exts, list.additional_audio_exts, list.additional_image_exts) + if list.videos or list.additional_video_exts or + list.audio or list.additional_audio_exts or + list.images or list.additional_image_exts then + create_extensions() + end + if list.directory_mode then + validate_directory_mode() + end +end) function Set (t) local set = {} @@ -40,35 +71,111 @@ function Set (t) end 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 + for k in pairs(b) do a[k] = true end + return a +end + +-- Returns first and last positions in string or past-to-end indices +function FindOrPastTheEnd (string, pattern, start_at) + local pos1, pos2 = string.find(string, pattern, start_at) + return pos1 or #string + 1, + pos2 or #string + 1 +end + +function Split (list) + local set = {} + + local item_pos = 1 + local item = "" + + while item_pos <= #list do + local pos1, pos2 = FindOrPastTheEnd(list, "%%*,", item_pos) + + local pattern_length = pos2 - pos1 + local is_comma_escaped = pattern_length % 2 + + local pos_before_escape = pos1 - 1 + local item_escape_count = pattern_length - is_comma_escaped + + item = item .. string.sub(list, item_pos, pos_before_escape + item_escape_count) + + if is_comma_escaped == 1 then + item = item .. "," + else + set[item] = true + item = "" + end + + item_pos = pos2 + 1 + end + + set[item] = true + + -- exclude empty items + set[""] = nil + + return set end -EXTENSIONS_VIDEO = Set { - 'mkv', 'avi', 'mp4', 'ogv', 'webm', 'rmvb', 'flv', 'wmv', 'mpeg', 'mpg', 'm4v', '3gp' +EXTENSIONS_VIDEO_DEFAULT = Set { + '3g2', '3gp', 'avi', 'flv', 'm2ts', 'm4v', 'mj2', 'mkv', 'mov', + 'mp4', 'mpeg', 'mpg', 'ogv', 'rmvb', 'webm', 'wmv', 'y4m' } -EXTENSIONS_AUDIO = Set { - 'mp3', 'wav', 'ogm', 'flac', 'm4a', 'wma', 'ogg', 'opus' +EXTENSIONS_AUDIO_DEFAULT = Set { + 'aiff', 'ape', 'au', 'flac', 'm4a', 'mka', 'mp3', 'oga', 'ogg', + 'ogm', 'opus', 'wav', 'wma' } -EXTENSIONS_IMAGES = Set { - 'jpg', 'jpeg', 'png', 'tif', 'tiff', 'gif', 'webp', 'svg', 'bmp' +EXTENSIONS_IMAGES_DEFAULT = Set { + 'avif', 'bmp', 'gif', 'j2k', 'jp2', 'jpeg', 'jpg', 'jxl', 'png', + 'svg', 'tga', 'tif', 'tiff', 'webp' } -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 +function split_option_exts(video, audio, image) + if video then o.additional_video_exts = Split(o.additional_video_exts) end + if audio then o.additional_audio_exts = Split(o.additional_audio_exts) end + if image then o.additional_image_exts = Split(o.additional_image_exts) end +end +split_option_exts(true, true, true) + +function split_patterns() + o.ignore_patterns = Split(o.ignore_patterns) +end +split_patterns() + +function create_extensions() + EXTENSIONS = {} + EXTENSIONS_VIDEO = {} + EXTENSIONS_AUDIO = {} + EXTENSIONS_IMAGES = {} + if o.videos then + SetUnion(SetUnion(EXTENSIONS_VIDEO, EXTENSIONS_VIDEO_DEFAULT), o.additional_video_exts) + SetUnion(EXTENSIONS, EXTENSIONS_VIDEO) + end + if o.audio then + SetUnion(SetUnion(EXTENSIONS_AUDIO, EXTENSIONS_AUDIO_DEFAULT), o.additional_audio_exts) + SetUnion(EXTENSIONS, EXTENSIONS_AUDIO) + end + if o.images then + SetUnion(SetUnion(EXTENSIONS_IMAGES, EXTENSIONS_IMAGES_DEFAULT), o.additional_image_exts) + SetUnion(EXTENSIONS, EXTENSIONS_IMAGES) + end +end +create_extensions() -function add_files_at(index, files) - index = index - 1 +function validate_directory_mode() + if o.directory_mode ~= "recursive" and o.directory_mode ~= "lazy" and o.directory_mode ~= "ignore" then + o.directory_mode = nil + end +end +validate_directory_mode() + +function add_files(files) local oldcount = mp.get_property_number("playlist-count", 1) for i = 1, #files do - mp.commandv("loadfile", files[i], "append") - mp.commandv("playlist-move", oldcount + i - 1, index + i - 1) + mp.commandv("loadfile", files[i][1], "append") + mp.commandv("playlist-move", oldcount + i - 1, files[i][2]) end end @@ -81,67 +188,149 @@ function get_extension(path) end end -table.filter = function(t, iter) - for i = #t, 1, -1 do - if not iter(t[i]) then - table.remove(t, i) +function is_ignored(file) + for pattern, _ in pairs(o.ignore_patterns) do + if string.match(file, pattern) then + return true end end + + return false end --- splitbynum and alnumcomp from alphanum.lua (C) Andre Bogus --- Released under the MIT License --- http://www.davekoelle.com/files/alphanum.lua +-- alphanum sorting for humans in Lua +-- http://notebook.kulchenko.com/algorithms/alphanumeric-natural-sorting-for-humans-in-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 +function alphanumsort(filenames) + local function padnum(n, d) + return #d > 0 and ("%03d%s%.12f"):format(#n, n, tonumber(d) / (10 ^ #d)) + or ("%03d%s"):format(#n, n) 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 + local tuples = {} + for i, f in ipairs(filenames) do + tuples[i] = {f:lower():gsub("0*(%d+)%.?(%d*)", padnum), f} end - return #xt < #yt + table.sort(tuples, function(a, b) + return a[1] == b[1] and #b[2] < #a[2] or a[1] < b[1] + end) + for i, tuple in ipairs(tuples) do filenames[i] = tuple[2] end + return filenames end local autoloaded = nil +local added_entries = {} +local autoloaded_dir = nil + +function scan_dir(path, current_file, dir_mode, separator, dir_depth, total_files, extensions) + if dir_depth == MAXDIRSTACK then + return + end + msg.trace("scanning: " .. path) + local files = utils.readdir(path, "files") or {} + local dirs = dir_mode ~= "ignore" and utils.readdir(path, "dirs") or {} + local prefix = path == "." and "" or path + + local filter = function(t, iter) + for i = #t, 1, -1 do + if not iter(t[i]) then + table.remove(t, i) + end + end + end + + filter(files, function (v) + -- The current file could be a hidden file, ignoring it doesn't load other + -- files from the current directory. + local current = prefix .. v == current_file + if o.ignore_hidden and not current and string.match(v, "^%.") then + return false + end + if not current and is_ignored(v) then + return false + end + + local ext = get_extension(v) + if ext == nil then + return false + end + return extensions[string.lower(ext)] + end) + filter(dirs, function(d) + return not ((o.ignore_hidden and string.match(d, "^%."))) + end) + alphanumsort(files) + alphanumsort(dirs) + + for i, file in ipairs(files) do + files[i] = prefix .. file + end + + local append = function(t1, t2) + local t1_size = #t1 + for i = 1, #t2 do + t1[t1_size + i] = t2[i] + end + end + + append(total_files, files) + if dir_mode == "recursive" then + for _, dir in ipairs(dirs) do + scan_dir(prefix .. dir .. separator, current_file, dir_mode, + separator, dir_depth + 1, total_files, extensions) + end + else + for i, dir in ipairs(dirs) do + dirs[i] = prefix .. dir + end + append(total_files, dirs) + end +end function find_and_add_entries() + local aborted = mp.get_property_native("playback-abort") + if aborted then + msg.debug("stopping: playback aborted") + return + end + local path = mp.get_property("path", "") 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") + msg.debug("stopping: autoload disabled") return elseif #dir == 0 then - msg.verbose("stopping: not a local path") + msg.debug("stopping: not a local path") return end local pl_count = mp.get_property_number("playlist-count", 1) + this_ext = get_extension(filename) -- check if this is a manually made playlist if (pl_count > 1 and autoloaded == nil) or - (pl_count == 1 and EXTENSIONS[string.lower(get_extension(filename))] == nil) then - msg.verbose("stopping: manually made playlist") + (pl_count == 1 and EXTENSIONS[string.lower(this_ext)] == nil) then + msg.debug("stopping: manually made playlist") return else - autoloaded = true + if pl_count == 1 then + autoloaded = true + autoloaded_dir = dir + added_entries = {} + end + end + + local extensions + if o.same_type then + if EXTENSIONS_VIDEO[string.lower(this_ext)] ~= nil then + extensions = EXTENSIONS_VIDEO + elseif EXTENSIONS_AUDIO[string.lower(this_ext)] ~= nil then + extensions = EXTENSIONS_AUDIO + else + extensions = EXTENSIONS_IMAGES + end + else + extensions = EXTENSIONS end local pl = mp.get_property_native("playlist", {}) @@ -149,31 +338,22 @@ function find_and_add_entries() msg.trace(("playlist-pos-1: %s, playlist: %s"):format(pl_current, utils.to_string(pl))) - local files = utils.readdir(dir, "files") - if files == nil then - msg.verbose("no other files in directory") - return + local files = {} + do + local dir_mode = o.directory_mode or mp.get_property("directory-mode", "lazy") + local separator = mp.get_property_native("platform") == "windows" and "\\" or "/" + scan_dir(autoloaded_dir, path, dir_mode, separator, 0, files, extensions) end - table.filter(files, function (v, k) - if string.match(v, "^%.") then - return false - end - local ext = get_extension(v) - if ext == nil then - return false - end - return EXTENSIONS[string.lower(ext)] - end) - table.sort(files, alnumcomp) - if dir == "." then - dir = "" + if next(files) == nil then + msg.debug("no other files or directories in directory") + return end -- Find the current pl entry (dir+"/"+filename) in the sorted dir list local current for i = 1, #files do - if files[i] == filename then + if files[i] == path then current = i break end @@ -183,38 +363,49 @@ function find_and_add_entries() end msg.trace("current file position in files: "..current) + -- treat already existing playlist entries, independent of how they got added + -- as if they got added by autoload + for _, entry in ipairs(pl) do + added_entries[entry.filename] = true + end + local append = {[-1] = {}, [1] = {}} for direction = -1, 1, 2 do -- 2 iterations, with direction = -1 and +1 for i = 1, MAXENTRIES do - local file = files[current + i * direction] - local pl_e = pl[pl_current + i * direction] + local pos = current + i * direction + local file = files[pos] if file == nil or file[1] == "." then break end - local filepath = dir .. file - if pl_e then - -- If there's a playlist entry, and it's the same file, stop. - msg.trace(pl_e.filename.." == "..filepath.." ?") - if pl_e.filename == filepath then - break + -- skip files that are/were already in the playlist + if not added_entries[file] then + if direction == -1 then + msg.verbose("Prepending " .. file) + table.insert(append[-1], 1, {file, pl_current + i * direction + 1}) + else + msg.verbose("Adding " .. file) + |