mirror of
https://github.com/mpv-player/mpv
synced 2025-02-16 12:17:12 +00:00
TOOLS/autocrop.lua: fix lint warnings
This commit is contained in:
parent
ee514c6acf
commit
142b75a95f
@ -110,7 +110,6 @@ max_line_length = 100
|
|||||||
-- TODO: Remove everything below this line
|
-- TODO: Remove everything below this line
|
||||||
local todo = {
|
local todo = {
|
||||||
"player/lua/osc.lua",
|
"player/lua/osc.lua",
|
||||||
"TOOLS/lua/autocrop.lua",
|
|
||||||
"TOOLS/lua/autodeint.lua",
|
"TOOLS/lua/autodeint.lua",
|
||||||
"TOOLS/lua/autoload.lua",
|
"TOOLS/lua/autoload.lua",
|
||||||
"TOOLS/lua/command-test.lua",
|
"TOOLS/lua/command-test.lua",
|
||||||
|
@ -56,7 +56,7 @@ require "mp.options".read_options(options)
|
|||||||
|
|
||||||
local cropdetect_label = mp.get_script_name() .. "-cropdetect"
|
local cropdetect_label = mp.get_script_name() .. "-cropdetect"
|
||||||
|
|
||||||
timers = {
|
local timers = {
|
||||||
auto_delay = nil,
|
auto_delay = nil,
|
||||||
detect_crop = nil
|
detect_crop = nil
|
||||||
}
|
}
|
||||||
@ -65,8 +65,7 @@ local hwdec_backup
|
|||||||
|
|
||||||
local command_prefix = options.suppress_osd and 'no-osd' or ''
|
local command_prefix = options.suppress_osd and 'no-osd' or ''
|
||||||
|
|
||||||
function is_enough_time(seconds)
|
local function is_enough_time(seconds)
|
||||||
|
|
||||||
-- Plus 1 second for deviation.
|
-- Plus 1 second for deviation.
|
||||||
local time_needed = seconds + 1
|
local time_needed = seconds + 1
|
||||||
local playtime_remaining = mp.get_property_native("playtime-remaining")
|
local playtime_remaining = mp.get_property_native("playtime-remaining")
|
||||||
@ -74,7 +73,7 @@ function is_enough_time(seconds)
|
|||||||
return playtime_remaining and time_needed < playtime_remaining
|
return playtime_remaining and time_needed < playtime_remaining
|
||||||
end
|
end
|
||||||
|
|
||||||
function is_cropable(time_needed)
|
local function is_cropable(time_needed)
|
||||||
if mp.get_property_native('current-tracks/video/image') ~= false then
|
if mp.get_property_native('current-tracks/video/image') ~= false then
|
||||||
mp.msg.warn("autocrop only works for videos.")
|
mp.msg.warn("autocrop only works for videos.")
|
||||||
return false
|
return false
|
||||||
@ -88,7 +87,7 @@ function is_cropable(time_needed)
|
|||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
function remove_cropdetect()
|
local function remove_cropdetect()
|
||||||
for _, filter in pairs(mp.get_property_native("vf")) do
|
for _, filter in pairs(mp.get_property_native("vf")) do
|
||||||
if filter.label == cropdetect_label then
|
if filter.label == cropdetect_label then
|
||||||
mp.command(
|
mp.command(
|
||||||
@ -99,14 +98,14 @@ function remove_cropdetect()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function restore_hwdec()
|
local function restore_hwdec()
|
||||||
if hwdec_backup then
|
if hwdec_backup then
|
||||||
mp.set_property("hwdec", hwdec_backup)
|
mp.set_property("hwdec", hwdec_backup)
|
||||||
hwdec_backup = nil
|
hwdec_backup = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function cleanup()
|
local function cleanup()
|
||||||
remove_cropdetect()
|
remove_cropdetect()
|
||||||
|
|
||||||
-- Kill all timers.
|
-- Kill all timers.
|
||||||
@ -120,37 +119,32 @@ function cleanup()
|
|||||||
restore_hwdec()
|
restore_hwdec()
|
||||||
end
|
end
|
||||||
|
|
||||||
function detect_crop()
|
local function apply_crop(meta)
|
||||||
local time_needed = options.detect_seconds
|
-- Verify if it is necessary to crop.
|
||||||
|
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_cropable(time_needed) then
|
-- Verify it is not over cropped.
|
||||||
|
local is_excessive = false
|
||||||
|
if is_effective and (meta.w < meta.min_w or meta.h < meta.min_h) then
|
||||||
|
mp.msg.info("The area to be cropped is too large.")
|
||||||
|
mp.msg.info("You might need to decrease detect_min_ratio.")
|
||||||
|
is_excessive = true
|
||||||
|
end
|
||||||
|
|
||||||
|
if not is_effective or is_excessive then
|
||||||
|
-- Clear any existing crop.
|
||||||
|
mp.command(string.format("%s set file-local-options/video-crop ''", command_prefix))
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local hwdec_current = mp.get_property("hwdec-current")
|
-- Apply crop.
|
||||||
if hwdec_current:find("-copy$") == nil and hwdec_current ~= "no" and
|
mp.command(string.format("%s set file-local-options/video-crop %sx%s+%s+%s",
|
||||||
hwdec_current ~= "crystalhd" and hwdec_current ~= "rkmpp" then
|
command_prefix, meta.w, meta.h, meta.x, meta.y))
|
||||||
hwdec_backup = mp.get_property("hwdec")
|
|
||||||
mp.set_property("hwdec", "no")
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Insert the cropdetect filter.
|
|
||||||
local limit = options.detect_limit
|
|
||||||
local round = options.detect_round
|
|
||||||
|
|
||||||
mp.command(
|
|
||||||
string.format(
|
|
||||||
'%s vf pre @%s:cropdetect=limit=%s:round=%d:reset=0',
|
|
||||||
command_prefix, cropdetect_label, limit, round
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
-- Wait to gather data.
|
|
||||||
timers.detect_crop = mp.add_timeout(time_needed, detect_end)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function detect_end()
|
local function detect_end()
|
||||||
|
|
||||||
-- Get the metadata and remove the cropdetect filter.
|
-- Get the metadata and remove the cropdetect filter.
|
||||||
local cropdetect_metadata = mp.get_property_native(
|
local cropdetect_metadata = mp.get_property_native(
|
||||||
"vf-metadata/" .. cropdetect_label)
|
"vf-metadata/" .. cropdetect_label)
|
||||||
@ -204,33 +198,36 @@ function detect_end()
|
|||||||
apply_crop(meta)
|
apply_crop(meta)
|
||||||
end
|
end
|
||||||
|
|
||||||
function apply_crop(meta)
|
local function detect_crop()
|
||||||
|
local time_needed = options.detect_seconds
|
||||||
|
|
||||||
-- Verify if it is necessary to crop.
|
if not is_cropable(time_needed) then
|
||||||
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)
|
|
||||||
|
|
||||||
-- Verify it is not over cropped.
|
|
||||||
local is_excessive = false
|
|
||||||
if is_effective and (meta.w < meta.min_w or meta.h < meta.min_h) then
|
|
||||||
mp.msg.info("The area to be cropped is too large.")
|
|
||||||
mp.msg.info("You might need to decrease detect_min_ratio.")
|
|
||||||
is_excessive = true
|
|
||||||
end
|
|
||||||
|
|
||||||
if not is_effective or is_excessive then
|
|
||||||
-- Clear any existing crop.
|
|
||||||
mp.command(string.format("%s set file-local-options/video-crop ''", command_prefix))
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Apply crop.
|
local hwdec_current = mp.get_property("hwdec-current")
|
||||||
mp.command(string.format("%s set file-local-options/video-crop %sx%s+%s+%s",
|
if hwdec_current:find("-copy$") == nil and hwdec_current ~= "no" and
|
||||||
command_prefix, meta.w, meta.h, meta.x, meta.y))
|
hwdec_current ~= "crystalhd" and hwdec_current ~= "rkmpp" then
|
||||||
|
hwdec_backup = mp.get_property("hwdec")
|
||||||
|
mp.set_property("hwdec", "no")
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Insert the cropdetect filter.
|
||||||
|
local limit = options.detect_limit
|
||||||
|
local round = options.detect_round
|
||||||
|
|
||||||
|
mp.command(
|
||||||
|
string.format(
|
||||||
|
'%s vf pre @%s:cropdetect=limit=%s:round=%d:reset=0',
|
||||||
|
command_prefix, cropdetect_label, limit, round
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
-- Wait to gather data.
|
||||||
|
timers.detect_crop = mp.add_timeout(time_needed, detect_end)
|
||||||
end
|
end
|
||||||
|
|
||||||
function on_start()
|
local function on_start()
|
||||||
|
|
||||||
-- Clean up at the beginning.
|
-- Clean up at the beginning.
|
||||||
cleanup()
|
cleanup()
|
||||||
@ -269,7 +266,7 @@ function on_start()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function on_toggle()
|
local function on_toggle()
|
||||||
|
|
||||||
-- If it is during auto_delay, kill the timer.
|
-- If it is during auto_delay, kill the timer.
|
||||||
if timers.auto_delay then
|
if timers.auto_delay then
|
||||||
|
Loading…
Reference in New Issue
Block a user