Skip to content

Commit 113cc6a

Browse files
llyyrdexeonify
authored andcommitted
crop: Use video-crop instead of inserting lavf crop
This change will break some major features, notably recursive cropping and thumbfast's preview. But for now, I think these are acceptable trade-offs. Ref: occivink/mpv-scripts#70
1 parent a0e326f commit 113cc6a

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

scripts/crop.lua

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
local opts = {
2-
mode = "hard", -- can be "hard" or "soft". If hard, apply a crop filter, if soft zoom + pan. Or a bonus "delogo" mode
2+
mode = "hard", -- can be "hard" or "soft". If hard, use video-crop, if soft use zoom + pan. Or a bonus "delogo" mode
33
draw_shade = true,
44
shade_opacity = "77",
55
draw_frame = false,
@@ -286,25 +286,28 @@ function crop_video(x1, y1, x2, y2)
286286
x2 = clamp(0, x2, 1)
287287
y2 = clamp(0, y2, 1)
288288
local vop = mp.get_property_native("video-out-params")
289-
local vf_table = mp.get_property_native("vf")
290289
local x = math.floor(x1 * vop.w + 0.5)
291290
local y = math.floor(y1 * vop.h + 0.5)
292291
local w = math.floor((x2 - x1) * vop.w + 0.5)
293292
local h = math.floor((y2 - y1) * vop.h + 0.5)
294-
if active_mode == "delogo" then
293+
if active_mode == "hard" then
294+
local video_crop = tostring(w) .."x".. tostring(h) .."+".. tostring(x) .."+".. tostring(y)
295+
mp.set_property_native("video-crop", video_crop)
296+
elseif active_mode == "delogo" then
297+
local vf_table = mp.get_property_native("vf")
295298
-- delogo is a little special and needs some padding to function
296299
w = math.min(vop.w - 1, w)
297300
h = math.min(vop.h - 1, h)
298301
x = math.max(1, x)
299302
y = math.max(1, y)
300303
if x + w == vop.w then w = w - 1 end
301304
if y + h == vop.h then h = h - 1 end
305+
vf_table[#vf_table + 1] = {
306+
name="delogo",
307+
params= { x = tostring(x), y = tostring(y), w = tostring(w), h = tostring(h) }
308+
}
309+
mp.set_property_native("vf", vf_table)
302310
end
303-
vf_table[#vf_table + 1] = {
304-
name=(active_mode == "hard") and "crop" or "delogo",
305-
params= { x = tostring(x), y = tostring(y), w = tostring(w), h = tostring(h) }
306-
}
307-
mp.set_property_native("vf", vf_table)
308311
end
309312
end
310313

@@ -356,7 +359,7 @@ function start_crop(mode)
356359
return
357360
end
358361
local mode_maybe = mode or opts.mode
359-
if mode_maybe ~= 'soft' then
362+
if mode_maybe == "delogo" then
360363
local hwdec = mp.get_property("hwdec-current")
361364
if hwdec and hwdec ~= "no" and not string.find(hwdec, "-copy$") then
362365
msg.error("Cannot crop with hardware decoding active (see manual)")
@@ -387,12 +390,11 @@ function toggle_crop(mode)
387390
local toggle_mode = mode or opts.mode
388391
if toggle_mode == "soft" then return end -- can't toggle soft mode
389392

390-
local remove_filter = function()
391-
local to_remove = (toggle_mode == "hard") and "crop" or "delogo"
393+
local remove_delogo = function()
392394
local vf_table = mp.get_property_native("vf")
393395
if #vf_table > 0 then
394396
for i = #vf_table, 1, -1 do
395-
if vf_table[i].name == to_remove then
397+
if vf_table[i].name == "delogo" then
396398
for j = i, #vf_table-1 do
397399
vf_table[j] = vf_table[j+1]
398400
end
@@ -404,7 +406,18 @@ function toggle_crop(mode)
404406
end
405407
return false
406408
end
407-
if not remove_filter() then
409+
if toggle_mode == "delogo" and not remove_delogo() then
410+
start_crop(mode)
411+
end
412+
local remove_hard = function()
413+
video_crop = mp.get_property_native("video-crop")
414+
if video_crop == "" then
415+
return false
416+
end
417+
mp.set_property_native("video-crop", "")
418+
return true
419+
end
420+
if toggle_mode == "hard" and not remove_hard() then
408421
start_crop(mode)
409422
end
410423
end

0 commit comments

Comments
 (0)