Skip to content

Commit

Permalink
TOOLS/autocrop.lua: use VO crop always
Browse files Browse the repository at this point in the history
There is no reason not to and this significantly reduces script
complexity.
  • Loading branch information
kasper93 authored and Dudemanguy committed Sep 20, 2023
1 parent 20e584f commit 6b96385
Showing 1 changed file with 12 additions and 47 deletions.
59 changes: 12 additions & 47 deletions TOOLS/lua/autocrop.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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.")
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))
-- Clear any existing crop.
mp.command(string.format("%s set file-local-options/video-crop ''", command_prefix))
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()
Expand Down Expand Up @@ -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

Expand All @@ -333,7 +298,7 @@ function on_toggle()
return
end

-- Neither => Do delectcrop.
-- Neither => Detect crop.
detect_crop()
end

Expand Down

0 comments on commit 6b96385

Please sign in to comment.