Skip to content

Improve blur-edges.lua #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 49 additions & 38 deletions scripts/blur-edges.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
local options = require 'mp.options'

local opts = {
blur_radius = 10,
blur_radius = 255,
blur_power = 10,
minimum_black_bar_size = 3,
mode = "all",
active = true,
reapply_delay = 0.5,
watch_later_fix = false,
watch_later_fix = true,
only_fullscreen = true,
}
options.read_options(opts)
Expand All @@ -16,22 +16,18 @@ local active = opts.active
local applied = false

function set_lavfi_complex(filter)
if not filter and mp.get_property("lavfi-complex") == "" then return end
local force_window = mp.get_property("force-window")
local sub = mp.get_property("sub")
mp.set_property("force-window", "yes")
if not filter then
mp.set_property("lavfi-complex", "")
mp.set_property("vid", "1")
else
if not opts.watch_later_fix then
mp.set_property("vid", "no")
end
mp.set_property("lavfi-complex", filter)
end
mp.set_property("sub", "no")
mp.commandv(
"vf",
"add",
string.format('@blur-edges:lavfi=[%s]', filter)
)

-- mp.set_property("sub", "no")
mp.set_property("force-window", force_window)
mp.set_property("sub", sub)
-- mp.set_property("sub", sub)
end

function set_blur()
Expand All @@ -41,82 +37,93 @@ function set_blur()
local video_aspect = mp.get_property_number("video-aspect-override")
local ww, wh = mp.get_osd_size()

if math.abs(ww/wh - video_aspect) < 0.05 then return end
if opts.mode == "horizontal" and ww/wh < video_aspect then return end
if opts.mode == "vertical" and ww/wh > video_aspect then return end
if math.abs(ww / wh - video_aspect) < 0.05 then return end
if opts.mode == "horizontal" and ww / wh < video_aspect then return end
if opts.mode == "vertical" and ww / wh > video_aspect then return end

-- local par = mp.get_property_number("video-params/par")
-- local height = mp.get_property_number("video-params/h")
-- local width = mp.get_property_number("video-params/w")
local par = 1
local height = mp.get_property_number("height")
local width = mp.get_property_number("width")
-- print("video_aspect", video_aspect, "\n",
-- "ww", ww, "wh", wh, "\n",
-- "width", width, "height", height)

local par = mp.get_property_number("video-params/par")
local height = mp.get_property_number("video-params/h")
local width = mp.get_property_number("video-params/w")
if height == nil or width == nil or par == nil then return end

local split = "[vid1] split=3 [a] [v] [b]"
local split = "split=3 [a] [v] [b]"
local crop_format = "crop=%s:%s:%s:%s"
local scale_format = "scale=width=%s:height=%s:flags=neighbor"

local stack_direction, cropped_scaled_1, cropped_scaled_2, blur_size

if ww/wh > video_aspect then
blur_size = math.floor((ww/wh)*height/par-width)
if ww / wh > video_aspect then
blur_size = math.floor((ww / wh) * height / par - width)
local nudge = blur_size % 2
blur_size = blur_size / 2

local height_with_maximized_width = height / width * ww
local visible_height = math.floor(height * par * wh / height_with_maximized_width)
local visible_width = math.floor(blur_size * wh / height_with_maximized_width)

local cropped_1 = string.format(crop_format, visible_width, visible_height, "0", (height - visible_height)/2)
local cropped_1 = string.format(crop_format, visible_width, visible_height, "0", (height - visible_height) / 2)
local scaled_1 = string.format(scale_format, blur_size + nudge, height)
cropped_scaled_1 = cropped_1 .. "," .. scaled_1

local cropped_2 = string.format(crop_format, visible_width, visible_height, width - visible_width, (height - visible_height)/2)
local cropped_2 = string.format(crop_format, visible_width, visible_height, width - visible_width,
(height - visible_height) / 2)
local scaled_2 = string.format(scale_format, blur_size, height)
cropped_scaled_2 = cropped_2 .. "," .. scaled_2
stack_direction = "h"
else
blur_size = math.floor((wh/ww)*width*par-height)
blur_size = math.floor((wh / ww) * width * par - height)
local nudge = blur_size % 2
blur_size = blur_size / 2

local width_with_maximized_height = width / height * wh
local visible_width = math.floor(width * ww / width_with_maximized_height)
local visible_height = math.floor(blur_size * ww / width_with_maximized_height)

local cropped_1 = string.format(crop_format, visible_width, visible_height, (width - visible_width)/2, "0")
local cropped_1 = string.format(crop_format, visible_width, visible_height, (width - visible_width) / 2, "0")
local scaled_1 = string.format(scale_format, width, blur_size + nudge)
cropped_scaled_1 = cropped_1 .. "," .. scaled_1

local cropped_2 = string.format(crop_format, visible_width, visible_height, (width - visible_width)/2, height - visible_height)
local cropped_2 = string.format(crop_format, visible_width, visible_height, (width - visible_width) / 2,
height - visible_height)
local scaled_2 = string.format(scale_format, width, blur_size)
cropped_scaled_2 = cropped_2 .. "," .. scaled_2
stack_direction = "v"
end

if blur_size < math.max(1, opts.minimum_black_bar_size) then return end
local lr = math.min(opts.blur_radius, math.floor(blur_size/2)-1)
local cr = math.min(opts.blur_radius, math.floor(blur_size/4)-1)
local blur = string.format("boxblur=lr=%i:lp=%i:cr=%i:cp=%i",
lr, opts.blur_power, cr, opts.blur_power)
-- local lr = math.min(opts.blur_radius, math.floor(blur_size/2)-1)
-- local cr = math.min(opts.blur_radius, math.floor(blur_size/4)-1)
-- local blur = string.format("boxblur=lr=%i:lp=%i:cr=%i:cp=%i", lr, opts.blur_power, cr, opts.blur_power)
local blur = string.format("gblur=sigma=%i", opts.blur_radius)

zone_1 = string.format("[a] %s,%s [a_fin]", cropped_scaled_1, blur)
zone_2 = string.format("[b] %s,%s [b_fin]", cropped_scaled_2, blur)

local par_fix = "setsar=ratio=" .. tostring(par) .. ":max=10000"

stack = string.format("[a_fin] [v] [b_fin] %sstack=3,%s [vo]", stack_direction, par_fix)
stack = string.format("[a_fin] [v] [b_fin] %sstack=3,%s", stack_direction, par_fix)
filter = string.format("%s;%s;%s;%s", split, zone_1, zone_2, stack)
print("using filter:", filter)
set_lavfi_complex(filter)
applied = true
end

function unset_blur()
set_lavfi_complex()
mp.commandv("vf", "remove", "@blur-edges")
applied = false
end

local reapplication_timer = mp.add_timeout(opts.reapply_delay, set_blur)
reapplication_timer:kill()

function reset_blur(k,v)
function reset_blur(k, v)
unset_blur()
reapplication_timer:kill()
reapplication_timer:resume()
Expand All @@ -137,11 +144,15 @@ function toggle()
end
end

if active then
active = false
toggle()
function on_start()
if active then
active = false
toggle()
end
end

mp.register_event("file-loaded", on_start)

mp.add_key_binding(nil, "toggle-blur", toggle)
mp.add_key_binding(nil, "set-blur", set_blur)
mp.add_key_binding(nil, "unset-blur", unset_blur)