2020-05-25 04:06:24 +00:00
|
|
|
--[[
|
2023-10-06 07:00:23 +00:00
|
|
|
This script uses the lavfi cropdetect filter and the video-crop property to
|
|
|
|
automatically crop the currently playing video with appropriate parameters.
|
2020-05-25 04:06:24 +00:00
|
|
|
|
2023-10-06 07:00:23 +00:00
|
|
|
It automatically crops the video when playback starts.
|
2020-05-25 04:06:24 +00:00
|
|
|
|
2023-10-06 07:00:23 +00:00
|
|
|
You can also manually crop the video by pressing the "C" (shift+c) key.
|
|
|
|
Pressing it again undoes the crop.
|
2020-05-25 04:06:24 +00:00
|
|
|
|
2023-10-06 07:00:23 +00:00
|
|
|
The workflow is as follows: First, it inserts the cropdetect filter. After
|
|
|
|
<detect_seconds> (default is 1) seconds, it then sets video-crop based on the
|
|
|
|
vf-metadata values gathered by cropdetect. The cropdetect filter is removed
|
|
|
|
after video-crop is set as it is no longer needed.
|
2020-05-25 04:06:24 +00:00
|
|
|
|
2023-10-06 07:00:23 +00:00
|
|
|
Since the crop parameters are determined from the 1 second of video between
|
|
|
|
inserting the cropdetect filter and setting video-crop, the "C" key should be
|
|
|
|
pressed at a position in the video where the crop region is unambiguous (i.e.,
|
|
|
|
not a black frame, black background title card, or dark scene).
|
2020-05-25 04:06:24 +00:00
|
|
|
|
2023-10-08 13:31:40 +00:00
|
|
|
If non-copy-back hardware decoding is in use, hwdec is temporarily disabled for
|
|
|
|
the duration of cropdetect as the filter would fail otherwise.
|
2020-05-25 04:06:24 +00:00
|
|
|
|
2023-10-06 07:00:23 +00:00
|
|
|
These are the default options. They can be overridden by adding
|
|
|
|
script-opts-append=autocrop-<parameter>=<value> to mpv.conf.
|
2020-05-25 04:06:24 +00:00
|
|
|
--]]
|
|
|
|
local options = {
|
2023-10-06 07:00:23 +00:00
|
|
|
-- Whether to automatically apply crop at the start of playback. If you
|
|
|
|
-- don't want to crop automatically, add
|
|
|
|
-- script-opts-append=autocrop-auto=no to mpv.conf.
|
2020-05-25 04:06:24 +00:00
|
|
|
auto = true,
|
2023-10-06 07:00:23 +00:00
|
|
|
-- Delay before starting crop in auto mode. You can try to increase this
|
|
|
|
-- value to avoid dark scenes or fade ins at beginning. Automatic cropping
|
|
|
|
-- will not occur if the value is larger than the remaining playback time.
|
2020-05-25 04:06:24 +00:00
|
|
|
auto_delay = 4,
|
2023-10-06 07:00:23 +00:00
|
|
|
-- Black threshold for cropdetect. Smaller values will generally result in
|
|
|
|
-- less cropping. See limit of
|
|
|
|
-- https://ffmpeg.org/ffmpeg-filters.html#cropdetect
|
2020-05-25 04:06:24 +00:00
|
|
|
detect_limit = "24/255",
|
2023-10-06 07:00:23 +00:00
|
|
|
-- The value which the width/height should be divisible by. Smaller
|
|
|
|
-- values have better detection accuracy. If you have problems with
|
|
|
|
-- other filters, you can try to set it to 4 or 16. See round of
|
|
|
|
-- https://ffmpeg.org/ffmpeg-filters.html#cropdetect
|
2020-05-25 04:06:24 +00:00
|
|
|
detect_round = 2,
|
2023-10-06 07:00:23 +00:00
|
|
|
-- The ratio of the minimum clip size to the original. A number from 0 to
|
|
|
|
-- 1. If the picture is over cropped, try adjusting this value.
|
2020-05-25 04:06:24 +00:00
|
|
|
detect_min_ratio = 0.5,
|
2023-10-06 07:00:23 +00:00
|
|
|
-- How long to gather cropdetect data. Increasing this may be desirable to
|
|
|
|
-- allow cropdetect more time to collect data.
|
2021-07-27 12:14:24 +00:00
|
|
|
detect_seconds = 1,
|
2023-10-06 07:00:23 +00:00
|
|
|
-- Whether the OSD shouldn't be used when cropdetect and video-crop are
|
|
|
|
-- applied and removed.
|
2021-07-27 12:14:24 +00:00
|
|
|
suppress_osd = false,
|
2020-05-25 04:06:24 +00:00
|
|
|
}
|
2023-10-06 07:00:23 +00:00
|
|
|
|
|
|
|
require "mp.options".read_options(options)
|
2020-05-25 04:06:24 +00:00
|
|
|
|
2023-10-06 07:47:51 +00:00
|
|
|
local cropdetect_label = mp.get_script_name() .. "-cropdetect"
|
2020-05-25 04:06:24 +00:00
|
|
|
|
2024-05-27 20:54:00 +00:00
|
|
|
local timers = {
|
2020-05-25 04:06:24 +00:00
|
|
|
auto_delay = nil,
|
|
|
|
detect_crop = nil
|
|
|
|
}
|
2014-04-29 11:07:25 +00:00
|
|
|
|
2023-09-21 09:48:22 +00:00
|
|
|
local hwdec_backup
|
|
|
|
|
2021-07-27 12:14:24 +00:00
|
|
|
local command_prefix = options.suppress_osd and 'no-osd' or ''
|
|
|
|
|
2024-05-27 20:54:00 +00:00
|
|
|
local function is_enough_time(seconds)
|
2020-05-25 04:06:24 +00:00
|
|
|
-- Plus 1 second for deviation.
|
|
|
|
local time_needed = seconds + 1
|
|
|
|
local playtime_remaining = mp.get_property_native("playtime-remaining")
|
|
|
|
|
|
|
|
return playtime_remaining and time_needed < playtime_remaining
|
|
|
|
end
|
|
|
|
|
2024-05-27 20:54:00 +00:00
|
|
|
local function is_cropable(time_needed)
|
2021-10-15 15:54:09 +00:00
|
|
|
if mp.get_property_native('current-tracks/video/image') ~= false then
|
|
|
|
mp.msg.warn("autocrop only works for videos.")
|
|
|
|
return false
|
2021-07-14 07:23:01 +00:00
|
|
|
end
|
2020-05-25 04:06:24 +00:00
|
|
|
|
2021-10-15 15:54:09 +00:00
|
|
|
if not is_enough_time(time_needed) then
|
|
|
|
mp.msg.warn("Not enough time to detect crop.")
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
2020-05-25 04:06:24 +00:00
|
|
|
end
|
|
|
|
|
2024-05-27 20:54:00 +00:00
|
|
|
local function remove_cropdetect()
|
2023-10-06 07:47:51 +00:00
|
|
|
for _, filter in pairs(mp.get_property_native("vf")) do
|
|
|
|
if filter.label == cropdetect_label then
|
|
|
|
mp.command(
|
|
|
|
string.format("%s vf remove @%s", command_prefix, filter.label))
|
|
|
|
|
|
|
|
return
|
|
|
|
end
|
2020-05-25 04:06:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-05-27 20:54:00 +00:00
|
|
|
local function restore_hwdec()
|
2023-09-21 09:48:22 +00:00
|
|
|
if hwdec_backup then
|
|
|
|
mp.set_property("hwdec", hwdec_backup)
|
|
|
|
hwdec_backup = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-05-27 20:54:00 +00:00
|
|
|
local function cleanup()
|
2023-10-06 07:47:51 +00:00
|
|
|
remove_cropdetect()
|
2020-05-25 04:06:24 +00:00
|
|
|
|
|
|
|
-- Kill all timers.
|
|
|
|
for index, timer in pairs(timers) do
|
|
|
|
if timer then
|
|
|
|
timer:kill()
|
2023-08-31 20:07:26 +00:00
|
|
|
timers[index] = nil
|
2020-05-25 04:06:24 +00:00
|
|
|
end
|
|
|
|
end
|
2023-09-21 09:48:22 +00:00
|
|
|
|
|
|
|
restore_hwdec()
|
2020-05-25 04:06:24 +00:00
|
|
|
end
|
|
|
|
|
2024-05-27 20:54:00 +00:00
|
|
|
local function apply_crop(meta)
|
|
|
|
-- 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)
|
2020-05-25 04:06:24 +00:00
|
|
|
|
2024-05-27 20:54:00 +00:00
|
|
|
-- 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
|
2014-04-29 11:07:25 +00:00
|
|
|
end
|
2014-05-04 19:54:03 +00:00
|
|
|
|
2024-05-27 20:54:00 +00:00
|
|
|
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
|
2023-09-21 09:48:22 +00:00
|
|
|
end
|
|
|
|
|
2024-05-27 20:54:00 +00:00
|
|
|
-- Apply crop.
|
|
|
|
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))
|
2014-04-29 11:07:25 +00:00
|
|
|
end
|
|
|
|
|
2024-05-27 20:54:00 +00:00
|
|
|
local function detect_end()
|
2020-05-25 04:06:24 +00:00
|
|
|
-- Get the metadata and remove the cropdetect filter.
|
2023-10-06 07:47:51 +00:00
|
|
|
local cropdetect_metadata = mp.get_property_native(
|
|
|
|
"vf-metadata/" .. cropdetect_label)
|
|
|
|
remove_cropdetect()
|
2020-05-25 04:06:24 +00:00
|
|
|
|
|
|
|
-- Remove the timer of detect crop.
|
|
|
|
if timers.detect_crop then
|
|
|
|
timers.detect_crop:kill()
|
|
|
|
timers.detect_crop = nil
|
|
|
|
end
|
2017-08-17 19:43:25 +00:00
|
|
|
|
2023-09-21 09:48:22 +00:00
|
|
|
restore_hwdec()
|
|
|
|
|
2024-05-12 01:43:59 +00:00
|
|
|
local meta
|
2020-05-25 04:06:24 +00:00
|
|
|
|
|
|
|
-- Verify the existence of metadata.
|
2014-04-29 11:07:25 +00:00
|
|
|
if cropdetect_metadata then
|
2020-05-25 04:06:24 +00:00
|
|
|
meta = {
|
|
|
|
w = cropdetect_metadata["lavfi.cropdetect.w"],
|
|
|
|
h = cropdetect_metadata["lavfi.cropdetect.h"],
|
|
|
|
x = cropdetect_metadata["lavfi.cropdetect.x"],
|
|
|
|
y = cropdetect_metadata["lavfi.cropdetect.y"],
|
|
|
|
}
|
2014-04-29 11:07:25 +00:00
|
|
|
else
|
2020-05-25 04:06:24 +00:00
|
|
|
mp.msg.error("No crop data.")
|
|
|
|
mp.msg.info("Was the cropdetect filter successfully inserted?")
|
2024-01-19 18:42:17 +00:00
|
|
|
mp.msg.info("Does your version of FFmpeg support AVFrame metadata?")
|
2020-05-25 04:06:24 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Verify that the metadata meets the requirements and convert it.
|
|
|
|
if meta.w and meta.h and meta.x and meta.y then
|
|
|
|
local width = mp.get_property_native("width")
|
|
|
|
local height = mp.get_property_native("height")
|
|
|
|
|
|
|
|
meta = {
|
|
|
|
w = tonumber(meta.w),
|
|
|
|
h = tonumber(meta.h),
|
|
|
|
x = tonumber(meta.x),
|
|
|
|
y = tonumber(meta.y),
|
|
|
|
min_w = width * options.detect_min_ratio,
|
|
|
|
min_h = height * options.detect_min_ratio,
|
|
|
|
max_w = width,
|
|
|
|
max_h = height
|
|
|
|
}
|
|
|
|
else
|
|
|
|
mp.msg.error("Got empty crop data.")
|
|
|
|
mp.msg.info("You might need to increase detect_seconds.")
|
|
|
|
end
|
|
|
|
|
|
|
|
apply_crop(meta)
|
|
|
|
end
|
|
|
|
|
2024-05-27 20:54:00 +00:00
|
|
|
local function detect_crop()
|
|
|
|
local time_needed = options.detect_seconds
|
2020-05-25 04:06:24 +00:00
|
|
|
|
2024-05-27 20:54:00 +00:00
|
|
|
if not is_cropable(time_needed) then
|
|
|
|
return
|
2023-10-05 18:31:08 +00:00
|
|
|
end
|
|
|
|
|
2024-05-27 20:54:00 +00:00
|
|
|
local hwdec_current = mp.get_property("hwdec-current")
|
|
|
|
if hwdec_current:find("-copy$") == nil and hwdec_current ~= "no" and
|
|
|
|
hwdec_current ~= "crystalhd" and hwdec_current ~= "rkmpp" then
|
|
|
|
hwdec_backup = mp.get_property("hwdec")
|
|
|
|
mp.set_property("hwdec", "no")
|
2023-08-25 17:22:53 +00:00
|
|
|
end
|
|
|
|
|
2024-05-27 20:54:00 +00:00
|
|
|
-- 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)
|
2020-05-25 04:06:24 +00:00
|
|
|
end
|
|
|
|
|
2024-05-27 20:54:00 +00:00
|
|
|
local function on_start()
|
2020-05-25 04:06:24 +00:00
|
|
|
|
|
|
|
-- Clean up at the beginning.
|
|
|
|
cleanup()
|
|
|
|
|
|
|
|
-- If auto is not true, exit.
|
|
|
|
if not options.auto then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- If it is the beginning, wait for detect_crop
|
|
|
|
-- after auto_delay seconds, otherwise immediately.
|
|
|
|
local playback_time = mp.get_property_native("playback-time")
|
|
|
|
local is_delay_needed = playback_time
|
|
|
|
and options.auto_delay > playback_time
|
|
|
|
|
|
|
|
if is_delay_needed then
|
|
|
|
|
|
|
|
-- Verify if there is enough time for autocrop.
|
|
|
|
local time_needed = options.auto_delay + options.detect_seconds
|
|
|
|
|
2021-10-15 15:54:09 +00:00
|
|
|
if not is_cropable(time_needed) then
|
2020-05-25 04:06:24 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
timers.auto_delay = mp.add_timeout(time_needed,
|
|
|
|
function()
|
|
|
|
detect_crop()
|
|
|
|
|
|
|
|
-- Remove the timer of auto delay.
|
|
|
|
timers.auto_delay:kill()
|
|
|
|
timers.auto_delay = nil
|
|
|
|
end
|
2014-04-29 11:07:25 +00:00
|
|
|
)
|
2020-05-25 04:06:24 +00:00
|
|
|
else
|
|
|
|
detect_crop()
|
2014-04-29 11:07:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-05-27 20:54:00 +00:00
|
|
|
local function on_toggle()
|
2020-05-25 04:06:24 +00:00
|
|
|
|
|
|
|
-- If it is during auto_delay, kill the timer.
|
|
|
|
if timers.auto_delay then
|
|
|
|
timers.auto_delay:kill()
|
|
|
|
timers.auto_delay = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Cropped => Remove it.
|
2023-09-09 22:49:34 +00:00
|
|
|
if mp.get_property("video-crop") ~= "" then
|
|
|
|
mp.command(string.format("%s set file-local-options/video-crop ''", command_prefix))
|
2020-05-25 04:06:24 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Detecting => Leave it.
|
|
|
|
if timers.detect_crop then
|
|
|
|
mp.msg.warn("Already cropdetecting!")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2023-09-09 22:49:34 +00:00
|
|
|
-- Neither => Detect crop.
|
2020-05-25 04:06:24 +00:00
|
|
|
detect_crop()
|
|
|
|
end
|
|
|
|
|
|
|
|
mp.add_key_binding("C", "toggle_crop", on_toggle)
|
|
|
|
mp.register_event("end-file", cleanup)
|
|
|
|
mp.register_event("file-loaded", on_start)
|