Last active
February 26, 2023 21:10
-
-
Save kevinlekiller/1c8a40a905916a6bf36d24f9033a025e to your computer and use it in GitHub Desktop.
Mpv script to enable interpolation or force vrr.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
Copyright (C) 2023 kevincs | |
This program is free software; you can redistribute it and/or | |
modify it under the terms of the GNU General Public License | |
as published by the Free Software Foundation; either version 2 | |
of the License, or (at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
https://www.gnu.org/licenses/gpl-2.0.html | |
--]] | |
-- Script to enable VRR (on Plasma Wayland ; requires kscreen-doctor) if the video is fullscreen. | |
-- If the display doesn't have VRR, interpolation is enabled. | |
-- For mpv running under flatpak, this requires the "System Bus -> Talks -> org.freedesktop.NetworkManager" permission. | |
-- Settings: | |
-- Don't enable interpolation under this threshold. | |
-- For example, display is 120Hz video is 25 fps : lua -e 'print(120 % 25)' = 20, 20 is higher than 0.13 so enable interpolation. | |
-- display is 120Hz video is 23.976fps : lua -e 'print(120 % 23.976)' = 0.12, 0.12 is lower than 0.13 so don't enable interpolation.' | |
local limit = 0.13 | |
-- Set vrr to always on for this display (run `kscreen-doctor -o` to find list of displays). | |
-- Here's example output of kscreen-doctor: Output: 1 DP-1 enabled connected primary DisplayPort | |
-- Output: 2 HDMI-A-1 enabled connected HDMI | |
-- Valid values could be DP-1, HDMI-A-1, DisplayPort, HDMI, etc. | |
local VrrDisplay = "DP-1" | |
--------------------------------------------------------------------------------------------- | |
--------------------------------------------------------------------------------------------- | |
local displayFPS | |
local foundDisplay | |
function main(name, vfps) | |
if (mp.get_property_bool("interpolation") or vfps == nil) then | |
return | |
end | |
local rem = 0.0 | |
displayFPS = math.floor(displayFPS + 0.5) | |
vfps = math.floor(vfps * 100) / 100 | |
if (displayFPS < vfps) then | |
rem = vfps % displayFPS | |
elseif (displayFPS > vfps) then | |
rem = displayFPS % vfps | |
end | |
if (rem >= limit) then | |
if not (mp.get_property_bool("display-sync-active")) then | |
mp.set_property("video-sync", "display-resample") | |
end | |
mp.set_property("interpolation", "yes") | |
print("Enabled interpolation.") | |
end | |
mp.unobserve_property(main) | |
end | |
function start(name, dfps) | |
if (dfps == nil) then | |
return | |
end | |
displayFPS = dfps | |
mp.unobserve_property(start) | |
if (VrrDisplay ~= "" and foundDisplay == nil) then | |
handle = assert(io.popen('flatpak-spawn --host kscreen-doctor -o | grep -Po "Output: .+?' .. VrrDisplay .. '.+? Modes:" | grep -Po "^.+enabled" | sed "s/Output: \\S* //" | sed "s/ \\S*enabled//"')) | |
for line in handle:lines() do | |
if (line ~= nil and line ~= '') then | |
foundDisplay = line | |
print("Found screen to ignore: " .. foundDisplay) | |
mp.observe_property("fullscreen", "bool", vrr) | |
mp.register_event("shutdown", vrr_auto) | |
return | |
end | |
end | |
end | |
local test = mp.get_property("container-fps") | |
if (test == nil or test == "nil property unavailable") then | |
test = mp.get_property("estimated-vf-fps") | |
if (test == nil or test == "nil property unavailable") then | |
return | |
end | |
mp.observe_property("estimated-vf-fps", "number", main) | |
else | |
mp.observe_property("container-fps", "number", main) | |
end | |
end | |
function vrr(name, forced) | |
if (foundDisplay == nil or foundDisplay == "") then | |
return | |
end | |
local mode | |
if (forced == true) then | |
mode = "always" | |
else | |
mode = "automatic" | |
end | |
print("Setting vrr to " .. mode .. " " .. foundDisplay) | |
os.execute('flatpak-spawn --host kscreen-doctor output."' .. foundDisplay .. '".vrrpolicy.' .. mode .. ' &> /dev/null &') | |
end | |
function vrr_auto() | |
vrr("vrr_auto", false) | |
end | |
-- Wait until we get a video fps. | |
function check() | |
mp.observe_property("display-fps", "number", start) | |
end | |
mp.register_event("file-loaded", check) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment