-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpv-timer.lua
108 lines (93 loc) · 2.67 KB
/
mpv-timer.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
--
-- mpv-timer
-- By AD2
--
-- A simple plugin for mpv which allows the user to set a starting time and an
-- ending time and see the time elapsed between those points with millisecond
-- precision.
--
-- Adjust keybindings by changing the quoted characters at the bottom of this
-- document.
--
start_time = nil
end_time = nil
total_time = nil
function set_start_time()
-- Test that the file can be timed
-- Code taken from the encode.lua script
local path = mp.get_property("path")
if not path then
mp.osd_message("No file currently playing")
return
end
if not mp.get_property_bool("seekable") then
mp.osd_message("Cannot time non-seekable media")
return
end
start_time = mp.get_property_number("time-pos");
mp.osd_message(
string.format("Start time is now at %s seconds",
seconds_to_time_string(start_time)),
1
)
time()
end
function set_end_time()
-- Test that the file can be timed
-- Code taken from the encode.lua script
local path = mp.get_property("path")
if not path then
mp.osd_message("No file currently playing")
return
end
if not mp.get_property_bool("seekable") then
mp.osd_message("Cannot time non-seekable media")
return
end
end_time = mp.get_property_number("time-pos");
mp.osd_message(
string.format("End time is now at %s seconds",
seconds_to_time_string(end_time)),
1
)
time()
end
function time()
-- Only time if we have a start and end, otherwise return
if start_time == nil or end_time == nil then
return
end
-- Swap start and end if they are out of order
if end_time <= start_time then
mp.osd_message("Second timestamp is before the first")
return
end
total_time = end_time - start_time
-- Print the final time to the screen
print_time()
end
function print_time()
if total_time == nil then
mp.osd_message("Time not set")
return
end
mp.osd_message(
string.format("Total time is %s", seconds_to_time_string(total_time)),
2
)
end
-- Function taken from the encode.lua script
function seconds_to_time_string(seconds)
local ret = string.format("%02d:%02d.%03d"
, math.floor(seconds / 60) % 60
, math.floor(seconds) % 60
, seconds * 1000 % 1000
)
if seconds > 3600 then
ret = string.format("%d:%s", math.floor(seconds / 3600), ret)
end
return ret
end
mp.add_key_binding("Z", "set-start-time", set_start_time)
mp.add_key_binding("X", "set-end-time", set_end_time)
mp.add_key_binding("C", "print-time", print_time)