videotools/clip_gif.lua

74 lines
1.6 KiB
Lua

local mp = require "mp"
local utils = require "mp.utils"
local nextfile = require "nextfile"
local config = {
directory = "/home/viciouscirce/pix/testshots/" -- Change this to your target directory
}
local function get_loop_timestamps()
local a = mp.get_property_number("ab-loop-a")
local b = mp.get_property_number("ab-loop-b")
return a, b
end
local function clear_loop()
mp.set_property("ab-loop-a", "no")
mp.set_property("ab-loop-b", "no")
end
local function get_video_filename()
local path = mp.get_property("path")
if path[1] == "/" then
return path
end
local workdir = mp.get_property("working-directory")
return workdir .. "/" .. path
end
local function get_output_filename(prompt)
local suggested_filename = nextfile.nextfile(config.directory, "gif")
if not prompt then
return config.directory .. suggested_filename
end
local cmd = "echo " .. suggested_filename .. " | dmenu -p Filename:"
local name = string.gsub(io.popen(cmd):read('*a'), '\n$', '')
if name == nil or #name == 0 then
return config.directory .. suggested_filename
end
return config.directory .. name
end
local function clip(prompt)
local a, b = get_loop_timestamps()
if a == nil or b == nil or (a == 0 and b == 0) then
mp.osd_message("AB Loop not set.")
return
end
local video_filename = get_video_filename()
local output_filename = get_output_filename(prompt)
io.popen("gifify " .. a .. " " .. b .. " " .. video_filename .. " " .. output_filename)
clear_loop()
end
local function clip_prompt()
clip(true)
end
local function clip_noprompt()
clip(false)
end
mp.add_key_binding(':', clip_prompt)
mp.add_key_binding(';', clip_noprompt)