summaryrefslogtreecommitdiffstats
path: root/TOOLS/lua/autocrop.lua
blob: a06db7f7022376091a8d3d2048ab45cf700a455f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
--[[
This script uses the lavfi cropdetect filter to automatically
insert a crop filter with appropriate parameters for the
currently playing video.

It will automatically crop the video, when playback starts.

Also It registers the key-binding "C" (shift+c). You can manually
crop the video by pressing the "C" (shift+c) key.

If the "C" key is pressed again, the crop filter is removed
restoring playback to its original state.

The workflow is as follows: First, it inserts the filter
vf=lavfi=cropdetect. After <detect_seconds> (default is 1)
seconds, it then inserts the filter vf=crop=w:h:x:y, where
w,h,x,y are determined from the vf-metadata gathered by
cropdetect. The cropdetect filter is removed immediately after
the crop filter is inserted as it is no longer needed.

Since the crop parameters are determined from the 1 second of
video between inserting the cropdetect and crop filters, 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).

The default options can be overridden by adding
script-opts-append=autocrop-<parameter>=<value> into mpv.conf

List of available parameters (For default values, see <options>):

auto: bool - Whether to automatically apply crop at the start of
    playback. If you don't want to crop automatically, set it to
    false or add "script-opts-append=autocrop-auto=no" into
    mpv.conf.

auto_delay: seconds - Delay before starting crop in auto mode.
    You can try to increase this value to avoid dark scene or
    fade in at beginning. Automatic cropping will not occur if
    the value is larger than the remaining playback time.

detect_limit: number[0-255] - Black threshold for cropdetect.
    Smaller values will generally result in less cropping.
    See limit of https://ffmpeg.org/ffmpeg-filters.html#cropdetect

detect_round: number[2^n] -  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

detect_min_ratio: number[0.0-1.0] - The ratio of the minimum clip
    size to the original. If the picture is over cropped or under
    cropped, try adjusting this value.

detect_seconds: seconds - How long to gather cropdetect data.
    Increasing this may be desirable to allow cropdetect more
    time to collect data.
--]]

require "mp.msg"
require 'mp.options'

local options = {
    auto = true,
    auto_delay = 4,
    detect_limit = "24/255",
    detect_round = 2,
    detect_min_ratio = 0.5,
    detect_seconds = 1
}
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)
}

timers = {
    auto_delay = nil,
    detect_crop = nil
}

function is_filter_present(label)
    local filters = mp.get_property_native("vf")
    for index, filter in pairs(filters) do
        if filter["label"] == label then
            return true
        end
    end
    return false
end

function is_enough_time(seconds)

    -- 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

function is_cropable()
    for _, track in pairs(mp.get_property_native('track-list')) do
        if track.type == 'video' and track.selected then
            return not track.albumart
        end
    end

    return false
end

function remove_filter(label)
    if is_filter_present(label) then
        mp.command(string.format('vf remove @%s', label))
        return true
    end
    return false
end

function cleanup()

    -- Remove all existing filters.
    for key, value in pairs(labels) do
        remove_filter(value)
    end

    -- Kill all timers.
    for index, timer in pairs(timers) do
        if timer then
            timer:kill()
            timer = nil
        end
    end
end

function detect_crop()

    -- If it's not cropable, exit.
    if not is_cropable() then
        mp.msg.warn("autocrop only works for videos.")
        return
    end

    -- Verify if there is enough time to detect crop.
    local time_needed = options.detect_seconds

    if not is_enough_time(time_needed) then
        mp.msg.warn("Not enough time to detect crop.")
        return
    end

    -- Insert the cropdetect filter.
    local limit = options.detect_limit
    local round = options.detect_round

    mp.command(
        string.format(
            'vf pre @%s:cropdetect=limit=%s:round=%d:reset=0',
            labels.cropdetect, limit, round
        )
    )

    -- Wait to gather data.
    timers.detect_crop = mp.add_timeout(time_needed, detect_end)
end

function detect_end()

    -- Get the metadata and remove the cropdetect filter.
    local cropdetect_metadata =
        mp.get_property_native(
            string.format("vf-metadata/%s",
            labels.cropdetect
        )
    )
    remove_filter(labels.cropdetect)

    -- Remove the timer of detect crop.
    if timers.detect_crop then
        timers.detect_crop:kill()
        timers.detect_crop = nil
    end

    local meta = {}

    -- Verify the existence of metadata.
    if cropdetect_metadata then
        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"],
        }
    else
        mp.msg.error("No crop data.")
        mp.msg.info("Was the cropdetect filter successfully inserted?")
        mp.msg.info("Does your version of ffmpeg/libav support AVFrame metadata?")
        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.")
        return
    end

    apply_crop(meta)
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

    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

    -- Remove existing crop.
    remove_filter(labels.crop)

    -- Apply crop.
    mp.command(
        string.format("vf pre @%s:lavfi-crop=w=%s:h=%s:x=%s:y=%s",
            labels.crop, meta.w, meta.h, meta.x, meta.y
        )
    )
end

function on_start()

    -- 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

        if not is_enough_time(time_needed) then
            mp.msg.warn("Not enough time for autocrop.")
            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
        )
    else
        detect_crop()
    end
end

function on_toggle()

    -- 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.
    if remove_filter(labels.crop) then
        return
    end

    -- Detecting => Leave it.
    if timers.detect_crop then
        mp.msg.warn("Already cropdetecting!")
        return
    end

    -- Neither => Do delectcrop.
    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)