diff options
author | Kevin Mitchell <kevmitch@gmail.com> | 2014-04-29 04:07:25 -0700 |
---|---|---|
committer | Kevin Mitchell <kevmitch@gmail.com> | 2014-04-29 08:31:44 -0700 |
commit | 4b0a760d86381587eff3552c440fbe1bfbac03e5 (patch) | |
tree | f628200eea25e2e36fdac89c2e498b080c282a81 | |
parent | 479dab57186d60a69e18f96af9e4b2edaa565dcc (diff) | |
download | mpv-4b0a760d86381587eff3552c440fbe1bfbac03e5.tar.bz2 mpv-4b0a760d86381587eff3552c440fbe1bfbac03e5.tar.xz |
TOOLS: improve autocrop.lua
It now inserts no filters and does nothing until the hot-key is pressed.
This makes it more suitable to be put in ~/.mpv/lua.
When the hot-key is pressed, it now inserts the cropdetect filter and
waits 1 second (or a --lua-opts specified duration) before gathering
the cropdetect metadata and inserting the appropriate crop filter. A
second press of the hotkey removes the crop.
-rw-r--r-- | TOOLS/lua/autocrop.lua | 84 |
1 files changed, 75 insertions, 9 deletions
diff --git a/TOOLS/lua/autocrop.lua b/TOOLS/lua/autocrop.lua index bf608dc050..40dc6d1704 100644 --- a/TOOLS/lua/autocrop.lua +++ b/TOOLS/lua/autocrop.lua @@ -1,11 +1,77 @@ -mp.command('vf add @autocrop.cropdetect:lavfi=graph="cropdetect=limit=24:round=2:reset=0"') +script_name=string.gsub(mp.get_script_name(),"lua/","") +cropdetect_label=string.format("%s-cropdetect",script_name) +crop_label=string.format("%s-crop",script_name) -function update_crop_handler() - cropdetect_metadata=mp.get_property_native("vf-metadata/autocrop.cropdetect") - mp.command(string.format('vf add @autocrop.crop:crop=%s:%s:%s:%s', - cropdetect_metadata['lavfi.cropdetect.w'], - cropdetect_metadata['lavfi.cropdetect.h'], - cropdetect_metadata['lavfi.cropdetect.x'], - cropdetect_metadata['lavfi.cropdetect.y'])) +-- number of seconds to gather cropdetect data +detect_seconds = tonumber(mp.get_opt(string.format("%s.detect_seconds", script_name))) +if not detect_seconds then + detect_seconds = 1 end -mp.add_key_binding("C","update_crop",update_crop_handler) + +function del_filter_if_present(label) + -- necessary because mp.command('vf del @label:filter') raises an + -- error if the filter doesn't exist + local vfs = mp.get_property_native('vf') + for i,vf in pairs(vfs) do + if vf['label'] == label then + table.remove(vfs, i) + mp.set_property_native('vf', vfs) + return true + end + end + return false +end + +function autocrop_start() + -- if there's a crop filter, just remove it and exit + if del_filter_if_present(crop_label) then + return + end + -- insert the cropdetect filter + ret=mp.command( + string.format( + 'vf add @%s:lavfi=graph="cropdetect=limit=24:round=2:reset=0"', + cropdetect_label + ) + ) + -- wait to gather data + mp.add_timeout(detect_seconds, do_crop) +end + +function do_crop() + require 'mp.msg' + -- get the metadata + local cropdetect_metadata = mp.get_property_native( + string.format('vf-metadata/%s', cropdetect_label) + ) + -- use it to crop if its valid + if cropdetect_metadata then + if cropdetect_metadata['lavfi.cropdetect.w'] + and cropdetect_metadata['lavfi.cropdetect.h'] + and cropdetect_metadata['lavfi.cropdetect.x'] + and cropdetect_metadata['lavfi.cropdetect.y'] + then + mp.command(string.format('vf add @%s:crop=%s:%s:%s:%s', + crop_label, + cropdetect_metadata['lavfi.cropdetect.w'], + cropdetect_metadata['lavfi.cropdetect.h'], + cropdetect_metadata['lavfi.cropdetect.x'], + cropdetect_metadata['lavfi.cropdetect.y'])) + else + mp.msg.error( + "Got empty crop data. You might need to increase detect_seconds." + ) + end + else + mp.msg.error( + "No crop data. Was the cropdetect filter successfully inserted?" + ) + mp.msg.error( + "Does your version of ffmpeg/libav support AVFrame metadata?" + ) + end + -- remove the cropdetect filter + del_filter_if_present(cropdetect_label) +end + +mp.add_key_binding("C","auto_crop",autocrop_start) |