Skip to content

Commit c3c22da

Browse files
committed
[crop] use video-crop instead of inserting lavf crop
1 parent af360f3 commit c3c22da

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ UX largely inspired by [this script](https://github.com/aidanholm/mpv-easycrop),
1616

1717
Press the binding to enter crop mode. Click once to define the first corner of the cropped zone, click a second time to define the second corner.
1818

19-
Note that [hardware decoding is in general not compatible with filters](https://mpv.io/manual/master/#options-hwdec), and will therefore not work with this script.
20-
2119
# encode.lua
2220

2321
**You need ffmpeg in your PATH (or in the same folder as mpv) for this script to work.**

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,
@@ -280,25 +280,28 @@ function crop_video(x1, y1, x2, y2)
280280
x2 = clamp(0, x2, 1)
281281
y2 = clamp(0, y2, 1)
282282
local vop = mp.get_property_native("video-out-params")
283-
local vf_table = mp.get_property_native("vf")
284283
local x = math.floor(x1 * vop.w + 0.5)
285284
local y = math.floor(y1 * vop.h + 0.5)
286285
local w = math.floor((x2 - x1) * vop.w + 0.5)
287286
local h = math.floor((y2 - y1) * vop.h + 0.5)
288-
if active_mode == "delogo" then
287+
if active_mode == "hard" then
288+
local video_crop = tostring(w) .."x".. tostring(h) .."+".. tostring(x) .."+".. tostring(y)
289+
mp.set_property_native("video-crop", video_crop)
290+
elseif active_mode == "delogo" then
291+
local vf_table = mp.get_property_native("vf")
289292
-- delogo is a little special and needs some padding to function
290293
w = math.min(vop.w - 1, w)
291294
h = math.min(vop.h - 1, h)
292295
x = math.max(1, x)
293296
y = math.max(1, y)
294297
if x + w == vop.w then w = w - 1 end
295298
if y + h == vop.h then h = h - 1 end
299+
vf_table[#vf_table + 1] = {
300+
name="delogo",
301+
params= { x = tostring(x), y = tostring(y), w = tostring(w), h = tostring(h) }
302+
}
303+
mp.set_property_native("vf", vf_table)
296304
end
297-
vf_table[#vf_table + 1] = {
298-
name=(active_mode == "hard") and "crop" or "delogo",
299-
params= { x = tostring(x), y = tostring(y), w = tostring(w), h = tostring(h) }
300-
}
301-
mp.set_property_native("vf", vf_table)
302305
end
303306
end
304307

@@ -350,7 +353,7 @@ function start_crop(mode)
350353
return
351354
end
352355
local mode_maybe = mode or opts.mode
353-
if mode_maybe ~= 'soft' then
356+
if mode_maybe == "delogo" then
354357
local hwdec = mp.get_property("hwdec-current")
355358
if hwdec and hwdec ~= "no" and not string.find(hwdec, "-copy$") then
356359
msg.error("Cannot crop with hardware decoding active (see manual)")
@@ -381,12 +384,11 @@ function toggle_crop(mode)
381384
local toggle_mode = mode or opts.mode
382385
if toggle_mode == "soft" then return end -- can't toggle soft mode
383386

384-
local remove_filter = function()
385-
local to_remove = (toggle_mode == "hard") and "crop" or "delogo"
387+
local remove_delogo = function()
386388
local vf_table = mp.get_property_native("vf")
387389
if #vf_table > 0 then
388390
for i = #vf_table, 1, -1 do
389-
if vf_table[i].name == to_remove then
391+
if vf_table[i].name == "delogo" then
390392
for j = i, #vf_table-1 do
391393
vf_table[j] = vf_table[j+1]
392394
end
@@ -398,7 +400,18 @@ function toggle_crop(mode)
398400
end
399401
return false
400402
end
401-
if not remove_filter() then
403+
if toggle_mode == "delogo" and not remove_delogo() then
404+
start_crop(mode)
405+
end
406+
local remove_hard = function()
407+
video_crop = mp.get_property_native("video-crop")
408+
if video_crop == "0x0+0+0" then
409+
return false
410+
end
411+
mp.set_property_native("video-crop", "0x0")
412+
return true
413+
end
414+
if toggle_mode == "hard" and not remove_hard() then
402415
start_crop(mode)
403416
end
404417
end

0 commit comments

Comments
 (0)