forked from Arieleg/mpv-copyTime
-
Notifications
You must be signed in to change notification settings - Fork 1
/
copy_time.lua
90 lines (72 loc) · 2.44 KB
/
copy_time.lua
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
require 'mp'
require 'mp.msg'
-- Copy the current time of the video to clipboard.
local function platform_type()
-- based on https://stackoverflow.com/a/30960054
-- cannot distinguish between Linux and MacOS reliably
local BinaryFormat = package.cpath:match("%p[\\|/]?%p(%a+)")
if BinaryFormat == "dll" then
return "windows"
else
return "unix"
end
end
local function command_exists(cmd)
local pipe = io.popen("type " .. cmd .. " > /dev/null 2> /dev/null; printf \"$?\"", "r")
return pipe:read() == "0"
end
local function divmod(a, b)
return a / b, a % b
end
local function set_clipboard(text)
local platform = platform_type()
if platform == "windows" then
mp.commandv("run", "powershell", "set-clipboard", text)
return true
elseif platform == "unix" then
-- copy using all available commands (e.g. on Linux, might have xclip AND wl-copy installed)
local found_any = false
if command_exists("xclip") then
-- linux + X11
local pipe = io.popen("xclip -silent -in -selection clipboard", "w")
pipe:write(text)
pipe:close()
found_any = true
end
if command_exists("wl-copy") then
-- linux + Wayland
local pipe = io.popen("wl-copy", "w")
pipe:write(text)
pipe:close()
found_any = true
end
if command_exists("pbcopy") then
-- MacOS
local pipe = io.popen("pbcopy", "w")
pipe:write(text)
pipe:close()
found_any = true
end
if not found_any then
mp.msg.error("no supported clipboard command found")
end
return found_any
else
mp.msg.error("unknown platform " .. platform)
return false
end
end
local function copy_time()
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("Copied to Clipboard: %s", time))
else
mp.osd_message("Failed to copy time to clipboard")
end
end
mp.add_key_binding("Ctrl+c", "copy_time", copy_time)