autoload.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. -- From https://github.com/mpv-player/mpv/blob/master/TOOLS/lua/autoload.lua
  2. -- This script automatically loads playlist entries before and after the
  3. -- the currently played file. It does so by scanning the directory a file is
  4. -- located in when starting playback. It sorts the directory entries
  5. -- alphabetically, and adds entries before and after the current file to
  6. -- the internal playlist. (It stops if the it would add an already existing
  7. -- playlist entry at the same position - this makes it "stable".)
  8. -- Add at most 5000 * 2 files when starting a file (before + after).
  9. MAXENTRIES = 5000
  10. local msg = require 'mp.msg'
  11. local options = require 'mp.options'
  12. local utils = require 'mp.utils'
  13. o = {
  14. disabled = false
  15. }
  16. options.read_options(o)
  17. function Set (t)
  18. local set = {}
  19. for _, v in pairs(t) do set[v] = true end
  20. return set
  21. end
  22. EXTENSIONS = Set {
  23. 'mkv', 'avi', 'mp4', 'ogv', 'webm', 'rmvb', 'flv', 'wmv', 'mpeg', 'mpg', 'm4v', '3gp',
  24. 'mp3', 'wav', 'ogm', 'flac', 'm4a', 'wma', 'ogg', 'opus', 'ts',
  25. }
  26. function add_files_at(index, files)
  27. index = index - 1
  28. local oldcount = mp.get_property_number("playlist-count", 1)
  29. for i = 1, #files do
  30. mp.commandv("loadfile", files[i], "append")
  31. mp.commandv("playlist-move", oldcount + i - 1, index + i - 1)
  32. end
  33. end
  34. function get_extension(path)
  35. match = string.match(path, "%.([^%.]+)$" )
  36. if match == nil then
  37. return "nomatch"
  38. else
  39. return match
  40. end
  41. end
  42. table.filter = function(t, iter)
  43. for i = #t, 1, -1 do
  44. if not iter(t[i]) then
  45. table.remove(t, i)
  46. end
  47. end
  48. end
  49. -- splitbynum and alnumcomp from alphanum.lua (C) Andre Bogus
  50. -- Released under the MIT License
  51. -- http://www.davekoelle.com/files/alphanum.lua
  52. -- split a string into a table of number and string values
  53. function splitbynum(s)
  54. local result = {}
  55. for x, y in (s or ""):gmatch("(%d*)(%D*)") do
  56. if x ~= "" then table.insert(result, tonumber(x)) end
  57. if y ~= "" then table.insert(result, y) end
  58. end
  59. return result
  60. end
  61. function clean_key(k)
  62. k = (' '..k..' '):gsub("%s+", " "):sub(2, -2):lower()
  63. return splitbynum(k)
  64. end
  65. -- compare two strings
  66. function alnumcomp(x, y)
  67. local xt, yt = clean_key(x), clean_key(y)
  68. for i = 1, math.min(#xt, #yt) do
  69. local xe, ye = xt[i], yt[i]
  70. if type(xe) == "string" then ye = tostring(ye)
  71. elseif type(ye) == "string" then xe = tostring(xe) end
  72. if xe ~= ye then return xe < ye end
  73. end
  74. return #xt < #yt
  75. end
  76. local autoloaded = nil
  77. function find_and_add_entries()
  78. local path = mp.get_property("path", "")
  79. local dir, filename = utils.split_path(path)
  80. msg.trace(("dir: %s, filename: %s"):format(dir, filename))
  81. if o.disabled then
  82. msg.verbose("stopping: autoload disabled")
  83. return
  84. elseif #dir == 0 then
  85. msg.verbose("stopping: not a local path")
  86. return
  87. end
  88. local pl_count = mp.get_property_number("playlist-count", 1)
  89. -- check if this is a manually made playlist
  90. if (pl_count > 1 and autoloaded == nil) or
  91. (pl_count == 1 and EXTENSIONS[string.lower(get_extension(filename))] == nil) then
  92. return
  93. else
  94. autoloaded = true
  95. end
  96. local pl = mp.get_property_native("playlist", {})
  97. local pl_current = mp.get_property_number("playlist-pos-1", 1)
  98. msg.trace(("playlist-pos-1: %s, playlist: %s"):format(pl_current,
  99. utils.to_string(pl)))
  100. local files = utils.readdir(dir, "files")
  101. if files == nil then
  102. return
  103. end
  104. table.filter(files, function (v, k)
  105. if string.match(v, "^%.") then
  106. return false
  107. end
  108. local ext = get_extension(v)
  109. if ext == nil then
  110. return false
  111. end
  112. return EXTENSIONS[string.lower(ext)]
  113. end)
  114. table.sort(files, alnumcomp)
  115. if dir == "." then
  116. dir = ""
  117. end
  118. -- Find the current pl entry (dir+"/"+filename) in the sorted dir list
  119. local current
  120. for i = 1, #files do
  121. if files[i] == filename then
  122. current = i
  123. break
  124. end
  125. end
  126. if current == nil then
  127. return
  128. end
  129. msg.trace("current file position in files: "..current)
  130. local append = {[-1] = {}, [1] = {}}
  131. for direction = -1, 1, 2 do -- 2 iterations, with direction = -1 and +1
  132. for i = 1, MAXENTRIES do
  133. local file = files[current + i * direction]
  134. local pl_e = pl[pl_current + i * direction]
  135. if file == nil or file[1] == "." then
  136. break
  137. end
  138. local filepath = dir .. file
  139. if pl_e then
  140. -- If there's a playlist entry, and it's the same file, stop.
  141. msg.trace(pl_e.filename.." == "..filepath.." ?")
  142. if pl_e.filename == filepath then
  143. break
  144. end
  145. end
  146. if direction == -1 then
  147. if pl_current == 1 then -- never add additional entries in the middle
  148. msg.info("Prepending " .. file)
  149. table.insert(append[-1], 1, filepath)
  150. end
  151. else
  152. msg.info("Adding " .. file)
  153. table.insert(append[1], filepath)
  154. end
  155. end
  156. end
  157. add_files_at(pl_current + 1, append[1])
  158. add_files_at(pl_current, append[-1])
  159. end
  160. mp.register_event("start-file", find_and_add_entries)