1
0
mirror of https://github.com/mpv-player/mpv synced 2025-01-08 16:10:29 +00:00

TOOLS/autocrop.lua: use VO crop always

There is no reason not to and this significantly reduces script
complexity.
This commit is contained in:
Kacper Michajłow 2023-09-10 00:49:34 +02:00 committed by Dudemanguy
parent 20e584f60b
commit 6b963857c0

View File

@ -72,13 +72,11 @@ local options = {
detect_min_ratio = 0.5, detect_min_ratio = 0.5,
detect_seconds = 1, detect_seconds = 1,
suppress_osd = false, suppress_osd = false,
use_vo_crop = true,
} }
read_options(options) read_options(options)
local label_prefix = mp.get_script_name() local label_prefix = mp.get_script_name()
local labels = { local labels = {
crop = string.format("%s-crop", label_prefix),
cropdetect = string.format("%s-cropdetect", label_prefix) cropdetect = string.format("%s-cropdetect", label_prefix)
} }
@ -123,14 +121,6 @@ function is_cropable(time_needed)
end end
function remove_filter(label) function remove_filter(label)
if options.use_vo_crop and label == labels.crop then
if mp.get_property('video-crop') ~= "" then
mp.command(string.format("%s set video-crop ''", command_prefix))
return true
end
return false
end
if is_filter_present(label) then if is_filter_present(label) then
mp.command(string.format('%s vf remove @%s', command_prefix, label)) mp.command(string.format('%s vf remove @%s', command_prefix, label))
return true return true
@ -140,13 +130,8 @@ end
function cleanup() function cleanup()
-- Remove all existing filters. -- Remove existing filter.
for key, value in pairs(labels) do remove_filter(labels.cropdetect)
-- No need to remove initial crop in vo_crop mode
if not (options.use_vo_crop and value == labels.crop) then
remove_filter(value)
end
end
-- Kill all timers. -- Kill all timers.
for index, timer in pairs(timers) do for index, timer in pairs(timers) do
@ -231,7 +216,6 @@ function detect_end()
else else
mp.msg.error("Got empty crop data.") mp.msg.error("Got empty crop data.")
mp.msg.info("You might need to increase detect_seconds.") mp.msg.info("You might need to increase detect_seconds.")
return
end end
apply_crop(meta) apply_crop(meta)
@ -240,39 +224,19 @@ end
function apply_crop(meta) function apply_crop(meta)
-- Verify if it is necessary to crop. -- Verify if it is necessary to crop.
local is_effective = meta.x > 0 or meta.y > 0 local is_effective = meta.w and meta.h and meta.x and meta.y and
or meta.w < meta.max_w or meta.h < meta.max_h (meta.x > 0 or meta.y > 0
or meta.w < meta.max_w or meta.h < meta.max_h)
if not is_effective then if not is_effective then
mp.msg.info("No area detected for cropping.") -- Clear any existing crop.
mp.command(string.format("%s set file-local-options/video-crop ''", command_prefix))
return return
end end
-- Verify it is not over cropped.
local is_excessive = meta.w < meta.min_w and meta.h < meta.min_h
if is_excessive then
mp.msg.info("The area to be cropped is too large.")
mp.msg.info("You might need to decrease detect_min_ratio.")
return
end
if options.use_vo_crop then
-- Apply crop.
mp.command(string.format("%s set video-crop %sx%s+%s+%s",
command_prefix, meta.w, meta.h, meta.x, meta.y))
return
end
-- Remove existing crop.
remove_filter(labels.crop)
-- Apply crop. -- Apply crop.
mp.command( mp.command(string.format("%s set file-local-options/video-crop %sx%s+%s+%s",
string.format("%s vf pre @%s:lavfi-crop=w=%s:h=%s:x=%s:y=%s", command_prefix, meta.w, meta.h, meta.x, meta.y))
command_prefix, labels.crop, meta.w, meta.h, meta.x, meta.y
)
)
end end
function on_start() function on_start()
@ -323,7 +287,8 @@ function on_toggle()
end end
-- Cropped => Remove it. -- Cropped => Remove it.
if remove_filter(labels.crop) then if mp.get_property("video-crop") ~= "" then
mp.command(string.format("%s set file-local-options/video-crop ''", command_prefix))
return return
end end
@ -333,7 +298,7 @@ function on_toggle()
return return
end end
-- Neither => Do delectcrop. -- Neither => Detect crop.
detect_crop() detect_crop()
end end