Skip to content

Commit

Permalink
crop: Use video-crop instead of inserting lavf crop
Browse files Browse the repository at this point in the history
Merges occivink/mpv-scripts#70
This change will break some major features, notably recursive cropping
and thumbfast's preview. But for now, I think these are acceptable
trade-offs.
  • Loading branch information
dexeonify committed Sep 24, 2023
1 parent e4ce5e4 commit c5e39b4
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions scripts/crop.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local opts = {
mode = "hard", -- can be "hard" or "soft". If hard, apply a crop filter, if soft zoom + pan. Or a bonus "delogo" mode
mode = "hard", -- can be "hard" or "soft". If hard, use video-crop, if soft use zoom + pan. Or a bonus "delogo" mode
draw_shade = true,
shade_opacity = "77",
draw_frame = false,
Expand Down Expand Up @@ -287,25 +287,28 @@ function crop_video(x1, y1, x2, y2)
x2 = clamp(0, x2, 1)
y2 = clamp(0, y2, 1)
local vop = mp.get_property_native("video-out-params")
local vf_table = mp.get_property_native("vf")
local x = math.floor(x1 * vop.w + 0.5)
local y = math.floor(y1 * vop.h + 0.5)
local w = math.floor((x2 - x1) * vop.w + 0.5)
local h = math.floor((y2 - y1) * vop.h + 0.5)
if active_mode == "delogo" then
if active_mode == "hard" then
local video_crop = tostring(w) .."x".. tostring(h) .."+".. tostring(x) .."+".. tostring(y)
mp.set_property_native("video-crop", video_crop)
elseif active_mode == "delogo" then
local vf_table = mp.get_property_native("vf")
-- delogo is a little special and needs some padding to function
w = math.min(vop.w - 1, w)
h = math.min(vop.h - 1, h)
x = math.max(1, x)
y = math.max(1, y)
if x + w == vop.w then w = w - 1 end
if y + h == vop.h then h = h - 1 end
vf_table[#vf_table + 1] = {
name="delogo",
params= { x = tostring(x), y = tostring(y), w = tostring(w), h = tostring(h) }
}
mp.set_property_native("vf", vf_table)
end
vf_table[#vf_table + 1] = {
name=(active_mode == "hard") and "crop" or "delogo",
params= { x = tostring(x), y = tostring(y), w = tostring(w), h = tostring(h) }
}
mp.set_property_native("vf", vf_table)
local subdata = mp.get_property_native("sub-ass-extradata")
if subdata ~= nil and opts.fix_borders then
local playresy = subdata:match("PlayResY:%s*(%d+)")
Expand Down Expand Up @@ -362,7 +365,7 @@ function start_crop(mode)
return
end
local mode_maybe = mode or opts.mode
if mode_maybe ~= 'soft' then
if mode_maybe == "delogo" then
local hwdec = mp.get_property("hwdec-current")
if hwdec and hwdec ~= "no" and not string.find(hwdec, "-copy$") then
msg.error("Cannot crop with hardware decoding active (see manual)")
Expand Down Expand Up @@ -393,12 +396,11 @@ function toggle_crop(mode)
local toggle_mode = mode or opts.mode
if toggle_mode == "soft" then return end -- can't toggle soft mode

local remove_filter = function()
local to_remove = (toggle_mode == "hard") and "crop" or "delogo"
local remove_delogo = function()
local vf_table = mp.get_property_native("vf")
if #vf_table > 0 then
for i = #vf_table, 1, -1 do
if vf_table[i].name == to_remove then
if vf_table[i].name == "delogo" then
for j = i, #vf_table-1 do
vf_table[j] = vf_table[j+1]
end
Expand All @@ -415,7 +417,18 @@ function toggle_crop(mode)
end
return false
end
if not remove_filter() then
if toggle_mode == "delogo" and not remove_delogo() then
start_crop(mode)
end
local remove_hard = function()
video_crop = mp.get_property_native("video-crop")
if video_crop == "" then
return false
end
mp.set_property_native("video-crop", "")
return true
end
if toggle_mode == "hard" and not remove_hard() then
start_crop(mode)
end
end
Expand Down

0 comments on commit c5e39b4

Please sign in to comment.