save_properties.lua 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. local msg = require "mp.msg"
  2. local opts = {
  3. properties = "volume,sub-scale",
  4. }(require 'mp.options').read_options(opts, "save_properties")
  5. local function explode(s, delimiter)
  6. local result = {};
  7. for match in (s..delimiter):gmatch("(.-)"..delimiter) do
  8. table.insert(result, match);
  9. end
  10. return result;
  11. end
  12. PropertyList = explode(opts.properties, ",")
  13. ConfigDir=os.getenv("HOME") .. "/.config/mpv/"
  14. local function onStartup()
  15. local file = io.open(ConfigDir .. "saved_properties", "r")
  16. if file == nil then
  17. return
  18. end
  19. local line = file:read()
  20. while line ~= nil and line ~= false and line ~= "" do
  21. PropertyPair=explode(line, "=")
  22. if not mp.set_property(PropertyPair[1], PropertyPair[2]) then
  23. msg.log("warning", "Could not set Property " .. PropertyPair[1] .. " to " .. PropertyPair[2])
  24. end
  25. line = file:read()
  26. end
  27. file:close()
  28. end
  29. local function onShutdown()
  30. local file = io.open(ConfigDir .. "saved_properties", "w")
  31. for i in pairs(PropertyList) do
  32. file:write(PropertyList[i] .. "=" .. mp.get_property(PropertyList[i]) .. "\n")
  33. end
  34. file:close()
  35. end
  36. onStartup()
  37. mp.register_event("shutdown", function()
  38. onShutdown()
  39. end)