Skip to content

Instantly share code, notes, and snippets.

@myfreeer
Last active August 26, 2017 13:17
Show Gist options
  • Save myfreeer/ec1178939c8f32c22843551ca75cacf1 to your computer and use it in GitHub Desktop.
Save myfreeer/ec1178939c8f32c22843551ca75cacf1 to your computer and use it in GitHub Desktop.
optimize performance for 4k
-- optimize performance for 4k
-- on high-res (width > 1920) videos:
-- pre-scale high-res to half the size using lavfi scale filter
-- change deband to no
-- change correct-downscaling to no
-- on normal-res videos:
-- revert everything back
-- from https://github.com/mpv-player/mpv/blob/39e04e929483847a3e0722d86d53f69837ed99db/TOOLS/lua/autocrop.lua
function del_filter_if_present(label)
-- necessary because mp.command('vf del @label:filter') raises an
-- error if the filter doesn't exist
local vfs = mp.get_property_native("vf")
for i,vf in pairs(vfs) do
if vf["label"] == label then
table.remove(vfs, i)
mp.set_property_native("vf", vfs)
return true
end
end
return false
end
local filename = ''
local cscale = mp.get_property_native('cscale', 'spline36')
local dscale = mp.get_property_native('dscale', 'mitchell')
local deband = mp.get_property_native('deband', 'no')
local correct_downscaling = mp.get_property_native('correct-downscaling', 'no')
local function optScale4kEventListener()
if filename == mp.get_property_native('filename') then
-- mp.osd_message("perf-opt-4k: enabled", osd_time)
return false
end
filename = mp.get_property_native('filename')
local scale = mp.get_property_native("window-scale")
del_filter_if_present('prescale4k')
if mp.get_property_number("width") > 1920 then
mp.command('vf add @prescale4k:lavfi=graph=[scale=w=iw*0.5:h=ih*0.5:flags=bicubic]')
mp.set_property("cscale", "bicubic_fast")
mp.set_property("dscale", "bicubic_fast")
mp.set_property("deband", "no")
mp.set_property("correct-downscaling", "no")
-- mp.osd_message("perf-opt-4k: enabled", osd_time)
else
mp.set_property("cscale", cscale)
mp.set_property("dscale", dscale)
mp.set_property("deband", deband)
mp.set_property("correct-downscaling", correct_downscaling)
-- mp.osd_message("perf-opt-4k: disabled", osd_time)
end
end
mp.register_event("file-loaded", optScale4kEventListener)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment