Edit: I don't know why but specifying the filename of the script-opts file made it work even though the basenames are the same.
Change:
From:
-- Define a table with default options
local o = {
memory_usage_percentage = 80,
reserved_memory_gb = 6
}
-- Read the options from the script-opts file
options.read_options(o)
-- Print the read options for debugging purposes
msg.info("memory_usage_percentage: " .. o.memory_usage_percentage)
msg.info("reserved_memory_gb: " .. o.reserved_memory_gb)
To:
-- Define a table with default options
local opts = {
memory_usage_percentage = 80,
reserved_memory_gb = 6
}
-- Read the options from the script-opts file
(require 'mp.options').read_options(opts, "demuxer-max-bytes")
-- Print the read options for debugging purposes
msg.info("memory_usage_percentage: " .. opts.memory_usage_percentage)
msg.info("reserved_memory_gb: " .. opts.reserved_memory_gb)
Original post:
I am making a lua script to adjust buffer size dynamically based on available ram. The script is working with the hardcoded values:
memory_usage_percentage = 80,
reserved_memory_gb = 6
but it is not reading the new values from my script-opts file. Do you see why?
cat /home/cmysmiaczxotoy/.config/mpv/script-opts/demuxer-max-bytes.conf
memory_usage_percentage=70
reserved_memory_gb=8
cat /home/cmysmiaczxotoy/.config/mpv/scripts/demuxer-max-bytes.lua (Now edited with fix)
-- Require the necessary modules
local mp = require 'mp'
local msg = require 'mp.msg'
-- Define a table with default options
local opts = {
memory_usage_percentage = 80,
reserved_memory_gb = 6
}
-- Read the options from the script-opts file
(require 'mp.options').read_options(opts, "demuxer-max-bytes")
-- Print the read options for debugging purposes
msg.info("memory_usage_percentage: " .. opts.memory_usage_percentage)
msg.info("reserved_memory_gb: " .. opts.reserved_memory_gb)
local function is_windows()
return package.config:sub(1,1) == '\\'
end
local function set_memory_properties(free_mem_kib, total_mem_kib)
-- Calculate the percentage of the total memory in KiB
local allowed_mem_kib = total_mem_kib * (opts.memory_usage_percentage / 100)
-- Convert reserved memory from GB to KiB
local reserved_mem_kib = opts.reserved_memory_gb * 1024 * 1024
-- Calculate the amount of memory to use, leaving the reserved memory free
local mem_to_use_kib = allowed_mem_kib - (total_mem_kib - free_mem_kib - reserved_mem_kib)
-- Apply various checks and calculations to mem_to_use_kib
if mem_to_use_kib > free_mem_kib - reserved_mem_kib then
mem_to_use_kib = free_mem_kib - reserved_mem_kib
end
if mem_to_use_kib < 0 then
mem_to_use_kib = free_mem_kib - reserved_mem_kib
end
if mem_to_use_kib < 0 then
mem_to_use_kib = 1024
end
-- Convert to bytes and round to nearest integer
local mem_to_use_bytes = math.floor(mem_to_use_kib * 1024)
-- Set demuxer-max-bytes to the calculated value
mp.set_property("demuxer-max-bytes", tostring(mem_to_use_bytes))
mp.msg.info("Set demuxer-max-bytes to: " .. mem_to_use_bytes .. " bytes")
-- Set cache-related properties
mp.set_property("cache", "yes")
mp.set_property("cache-pause", "yes")
mp.set_property("force-seekable", "yes")
mp.set_property("demuxer-readahead-secs", "30")
end
-- Function to gather memory info on Windows
local function get_memory_info_windows()
local ps_script = 'Get-CimInstance Win32_OperatingSystem | ' ..
'ForEach-Object { $_.FreePhysicalMemory, $_.TotalVisibleMemorySize }'
local handle = io.popen('powershell -NoProfile -Command "' .. ps_script .. '"', 'r')
local free_mem_kib = handle:read("*l")
local total_mem_kib = handle:read("*l")
handle:close()
return tonumber(free_mem_kib), tonumber(total_mem_kib)
end
-- Function to gather memory info on Linux
local function get_memory_info_linux()
local total_mem_kib = 0
local free_mem_kib = 0
local meminfo = io.open("/proc/meminfo", "r")
if meminfo then
for line in meminfo:lines() do
local key, value = line:match("(%w+):%s+(%d+)")
if key == "MemAvailable" then
free_mem_kib = tonumber(value)
elseif key == "MemTotal" then
total_mem_kib = tonumber(value)
end
end
meminfo:close()
end
return free_mem_kib, total_mem_kib
end
-- This hook runs at the start of file loading
mp.register_event("start-file", function()
local free_mem_kib, total_mem_kib
if is_windows() then
free_mem_kib, total_mem_kib = get_memory_info_windows()
else
free_mem_kib, total_mem_kib = get_memory_info_linux()
end
if total_mem_kib > 0 then
set_memory_properties(free_mem_kib, total_mem_kib)
else
mp.msg.error("Could not determine total memory.")
end
end)