dotfiles/multimedia/.config/mpv/scripts/copy_videotime.lua

84 lines
1.9 KiB
Lua
Raw Normal View History

2023-06-15 08:12:30 +00:00
local mp = require("mp")
require("mp.msg")
2023-05-23 13:31:17 +00:00
-- Copy the current time of the video to clipboard.
WINDOWS = 2
UNIX = 3
KEY_BIND = "y"
local function platform_type()
2023-06-15 08:12:30 +00:00
local utils = require("mp.utils")
local workdir = utils.to_string(mp.get_property_native("working-directory"))
if string.find(workdir, "\\") then
return WINDOWS
else
return UNIX
end
2023-05-23 13:31:17 +00:00
end
local function command_exists(cmd)
2023-06-15 08:12:30 +00:00
local pipe = io.popen("type " .. cmd .. ' > /dev/null 2> /dev/null; printf "$?"', "r")
if not pipe then
return
end
local exists = pipe:read() == "0"
pipe:close()
return exists
2023-05-23 13:31:17 +00:00
end
local function get_clipboard_cmd()
2023-06-15 08:12:30 +00:00
if command_exists("xclip") then
return "xclip -silent -in -selection clipboard"
elseif command_exists("wl-copy") then
return "wl-copy"
elseif command_exists("pbcopy") then
return "pbcopy"
else
mp.msg.error("No supported clipboard command found")
return false
end
2023-05-23 13:31:17 +00:00
end
local function divmod(a, b)
2023-06-15 08:12:30 +00:00
return a / b, a % b
2023-05-23 13:31:17 +00:00
end
local function set_clipboard(text)
2023-06-15 08:12:30 +00:00
if platform == WINDOWS then
mp.commandv("run", "powershell", "set-clipboard", text)
return true
elseif platform == UNIX and clipboard_cmd then
local pipe = io.popen(clipboard_cmd, "w")
if not pipe then
return
end
pipe:write(text)
pipe:close()
return true
else
mp.msg.error("Set_clipboard error")
return false
end
2023-05-23 13:31:17 +00:00
end
local function copyTime()
2023-06-15 08:12:30 +00:00
local time_pos = mp.get_property_number("time-pos")
local minutes, remainder = divmod(time_pos, 60)
local hours, minutes = divmod(minutes, 60)
local seconds = math.floor(remainder)
local milliseconds = math.floor((remainder - seconds) * 1000)
local time = string.format("%02d:%02d:%02d.%03d", hours, minutes, seconds, milliseconds)
if set_clipboard(time) then
mp.osd_message(string.format("[copytime] %s", time))
else
mp.osd_message("[copytime] failed")
end
2023-05-23 13:31:17 +00:00
end
platform = platform_type()
if platform == UNIX then
2023-06-15 08:12:30 +00:00
clipboard_cmd = get_clipboard_cmd()
2023-05-23 13:31:17 +00:00
end
mp.add_key_binding(KEY_BIND, "copyTime", copyTime)