1
0
mirror of https://github.com/mpv-player/mpv synced 2024-12-22 23:02:37 +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_seconds = 1,
suppress_osd = false,
use_vo_crop = true,
}
read_options(options)
local label_prefix = mp.get_script_name()
local labels = {
crop = string.format("%s-crop", label_prefix),
cropdetect = string.format("%s-cropdetect", label_prefix)
}
@ -123,14 +121,6 @@ function is_cropable(time_needed)
end
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
mp.command(string.format('%s vf remove @%s', command_prefix, label))
return true
@ -140,13 +130,8 @@ end
function cleanup()
-- Remove all existing filters.
for key, value in pairs(labels) do
-- 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
-- Remove existing filter.
remove_filter(labels.cropdetect)
-- Kill all timers.
for index, timer in pairs(timers) do
@ -231,7 +216,6 @@ function detect_end()
else
mp.msg.error("Got empty crop data.")
mp.msg.info("You might need to increase detect_seconds.")
return
end
apply_crop(meta)
@ -240,39 +224,19 @@ end
function apply_crop(meta)
-- Verify if it is necessary to crop.
local is_effective = meta.x > 0 or meta.y > 0
or meta.w < meta.max_w or meta.h < meta.max_h
local is_effective = meta.w and meta.h and meta.x and meta.y and
(meta.x > 0 or meta.y > 0
or meta.w < meta.max_w or meta.h < meta.max_h)
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
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.
mp.command(
string.format("%s vf pre @%s:lavfi-crop=w=%s:h=%s:x=%s:y=%s",
command_prefix, labels.crop, meta.w, meta.h, meta.x, meta.y
)
)
mp.command(string.format("%s set file-local-options/video-crop %sx%s+%s+%s",
command_prefix, meta.w, meta.h, meta.x, meta.y))
end
function on_start()
@ -323,7 +287,8 @@ function on_toggle()
end
-- 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
end
@ -333,7 +298,7 @@ function on_toggle()
return
end
-- Neither => Do delectcrop.
-- Neither => Detect crop.
detect_crop()
end