Skip to content

Commit

Permalink
chore: update all user scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
noelsimbolon committed Jun 17, 2023
1 parent 4bc9232 commit 3383530
Show file tree
Hide file tree
Showing 8 changed files with 1,634 additions and 461 deletions.
1 change: 1 addition & 0 deletions input.conf
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ l cycle-values sub-ass-override "yes" "force" "no" # https://mpv.io
# SCRIPT KEYBINDINGS
~ script-message cycle-commands "apply-profile HDR ; show-text 'HDR profile applied'" "apply-profile HDR restore ; show-text 'HDR profile restored'"
# ~ script-message cycle-commands "apply-profile Clip ; show-text 'Clip profile applied'" "apply-profile Mobius ; show-text 'Mobius profile applied'" "apply-profile Reinhard ; show-text 'Reinhard profile applied'" "apply-profile Hable ; show-text 'Hable profile applied'" "apply-profile bt.2390 ; show-text 'bt.2390 profile applied'" "apply-profile Gamma ; show-text 'Gamma profile applied'" "apply-profile Linear ; show-text 'Linear profile applied'"
c script-binding cycle-visualizer # cycle audio visualizer (audio-visualizer.lua)
b script-binding set_gif_start # set the start timestamp for to make GIF (mpv-gif.lua)
B script-binding set_gif_end # set the stop timestamp for to make GIF (mpv-gif.lua)
ctrl+b script-binding make_gif # make the GIF using start and stop timestamps (mpv-gif.lua)
Expand Down
66 changes: 42 additions & 24 deletions scripts/audio_visualizer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ local opts = {

height = 6,
-- [4 .. 12]

forcewindow = true,
-- true (yes) always run visualizer regardless of force-window settings
-- false (no) does not run visualizer when force-window is no
}

-- key bindings
Expand Down Expand Up @@ -136,7 +140,11 @@ options.read_options(opts)
opts.height = math.min(12, math.max(4, opts.height))
opts.height = math.floor(opts.height)

local function get_visualizer(name, quality, vtrack, albumart)
if not opts.forcewindow and mp.get_property('force-window') == "no" then
return
end

local function get_visualizer(name, quality, vtrack)
local w, h, fps

if quality == "verylow" then
Expand Down Expand Up @@ -255,14 +263,20 @@ local function get_visualizer(name, quality, vtrack, albumart)
"mode = p2p," ..
"format = rgb0 [vo]"
elseif name == "off" then
if vtrack > 0 or albumart > 0 then
local hasvideo = false
for id, track in ipairs(mp.get_property_native("track-list")) do
if track.type == "video" then
hasvideo = true
break
end
end
if hasvideo then
return "[aid1] asetpts=PTS [ao]; [vid1] setpts=PTS [vo]"
else
return "[aid1] asetpts=PTS [ao];" ..
"color =" ..
"c = Black:" ..
"s =" .. w .. "x" .. h .. ":" ..
"d = 0.04," ..
"s =" .. w .. "x" .. h .. "," ..
"format = yuv420p [vo]"
end
end
Expand All @@ -271,19 +285,19 @@ local function get_visualizer(name, quality, vtrack, albumart)
return ""
end

local function select_visualizer(atrack, vtrack, albumart)
local function select_visualizer(vtrack)
if opts.mode == "off" then
return ""
elseif opts.mode == "force" then
return get_visualizer(opts.name, opts.quality, vtrack, albumart)
return get_visualizer(opts.name, opts.quality, vtrack)
elseif opts.mode == "noalbumart" then
if albumart == 0 and vtrack == 0 then
return get_visualizer(opts.name, opts.quality, vtrack, albumart)
if vtrack == nil then
return get_visualizer(opts.name, opts.quality, vtrack)
end
return ""
elseif opts.mode == "novideo" then
if vtrack == 0 then
return get_visualizer(opts.name, opts.quality, vtrack, albumart)
if vtrack == nil or vtrack.albumart then
return get_visualizer(opts.name, opts.quality, vtrack)
end
return ""
end
Expand All @@ -294,30 +308,34 @@ end

local function visualizer_hook()
local count = mp.get_property_number("track-list/count", -1)
local atrack = 0
local vtrack = 0
local albumart = 0
if count <= 0 then
return
end
for tr = 0,count-1 do
if mp.get_property("track-list/" .. tr .. "/type") == "audio" then
atrack = atrack + 1
else
if mp.get_property("track-list/" .. tr .. "/type") == "video" then
if mp.get_property("track-list/" .. tr .. "/albumart") == "yes" then
albumart = 1
else
vtrack = vtrack + 1
end

local atrack = mp.get_property_native("current-tracks/audio")
local vtrack = mp.get_property_native("current-tracks/video")

--no tracks selected (yet)
if atrack == nil and vtrack == nil then
for id, track in ipairs(mp.get_property_native("track-list")) do
if track.type == "video" and (vtrack == nil or vtrack.albumart == true) and mp.get_property("vid") ~= "no" then
vtrack = track
elseif track.type == "audio" then
atrack = track
end
end
end

mp.set_property("options/lavfi-complex", select_visualizer(atrack, vtrack, albumart))
local lavfi = select_visualizer(vtrack)
--prevent endless loop
if lavfi ~= mp.get_property("options/lavfi-complex", "") then
mp.set_property("options/lavfi-complex", lavfi)
end
end

mp.add_hook("on_preloaded", 50, visualizer_hook)
mp.observe_property("current-tracks/audio", "native", visualizer_hook)
mp.observe_property("current-tracks/video", "native", visualizer_hook)

local function cycle_visualizer()
local i, index = 1
Expand Down
121 changes: 80 additions & 41 deletions scripts/autoload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,36 @@
-- 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 directory script-opts (the "script-opts"
directory must be in the mpv configuration directory, typically ~/.config/mpv/).
Example configuration would be:
disabled=no
images=no
videos=yes
audio=yes
ignore_hidden=yes
--]]

MAXENTRIES = 5000

local msg = require 'mp.msg'
local options = require 'mp.options'
local utils = require 'mp.utils'

o = {
disabled = false
disabled = false,
images = true,
videos = true,
audio = true,
ignore_hidden = true
}
options.read_options(o)

Expand All @@ -22,11 +41,33 @@ function Set (t)
return set
end

EXTENSIONS = Set {
'mkv', 'avi', 'mp4', 'ogv', 'webm', 'rmvb', 'flv', 'wmv', 'mpeg', 'mpg', 'm4v', '3gp',
'mp3', 'wav', 'ogm', 'flac', 'm4a', 'wma', 'ogg', 'opus',
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 {
'3g2', '3gp', 'avi', 'flv', 'm2ts', 'm4v', 'mj2', 'mkv', 'mov',
'mp4', 'mpeg', 'mpg', 'ogv', 'rmvb', 'webm', 'wmv', 'y4m'
}

EXTENSIONS_AUDIO = Set {
'aiff', 'ape', 'au', 'flac', 'm4a', 'mka', 'mp3', 'oga', 'ogg',
'ogm', 'opus', 'wav', 'wma'
}

EXTENSIONS_IMAGES = 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 add_files_at(index, files)
index = index - 1
local oldcount = mp.get_property_number("playlist-count", 1)
Expand All @@ -53,39 +94,38 @@ table.filter = function(t, iter)
end
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

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

function find_and_add_entries()
local path = mp.get_property("path", "")
local dir, filename = utils.split_path(path)
Expand All @@ -98,10 +138,11 @@ function find_and_add_entries()
return
end

local pl_count = mp.get_property_number("playlist-count", 1)
pl_count = mp.get_property_number("playlist-count", 1)
-- 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")
return
else
autoloaded = true
Expand All @@ -114,10 +155,13 @@ function find_and_add_entries()

local files = utils.readdir(dir, "files")
if files == nil then
msg.verbose("no other files in directory")
return
end
table.filter(files, function (v, k)
if string.match(v, "^%.") then
-- 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
return false
end
local ext = get_extension(v)
Expand All @@ -126,7 +170,7 @@ function find_and_add_entries()
end
return EXTENSIONS[string.lower(ext)]
end)
table.sort(files, alnumcomp)
alphanumsort(files)

if dir == "." then
dir = ""
Expand All @@ -146,22 +190,17 @@ function find_and_add_entries()
msg.trace("current file position in files: "..current)

local append = {[-1] = {}, [1] = {}}
local filenames = get_playlist_filenames()
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]
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
end
end
-- skip files already in playlist
if filenames[file] then break end

if direction == -1 then
if pl_current == 1 then -- never add additional entries in the middle
Expand Down
Loading

0 comments on commit 3383530

Please sign in to comment.