local cache_size function on_load() -- Get the system's total memory in MB local mem_total = tonumber(io.popen("free -m | awk '/Mem:/ {print $2}'"):read()) -- Calculate the cache size based on total memory cache_size = math.floor(mem_total / 32) -- 128MB for 4GB RAM, increase proportionally -- Set initial cache properties mp.set_property_number("demuxer-max-bytes", tostring(cache_size * 1024 * 1024)) -- Set cache size in bytes mp.set_property("demuxer-readahead-secs", "20") -- Set read-ahead in seconds, adjust as needed end function on_pause_change(name, value) if value then -- Force caching when paused and free backwards cache mp.set_property("demuxer-max-back-bytes", "0") -- Free backwards cache mp.command("script-message-to cache flush") mp.command("script-message-to cache prefill") else -- Reset cache properties when playback resumes mp.set_property("demuxer-max-back-bytes", tostring(cache_size * 1024 * 1024)) -- Reset backwards cache size end end mp.register_event("file-loaded", on_load) mp.observe_property("pause", "bool", on_pause_change)