This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
sub-voracious.lua
269 lines (218 loc) · 7.48 KB
/
sub-voracious.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
-- Usage:
-- i - subtitle mode on/off
-- ↑, ↓ (Space) - mode specific shortcuts
-- Note:
-- The script is inspired by voracious.app (https://github.com/rsimmons/voracious).
--
-- - Listening Practice:
-- "Subtitles are initially hidden. At the end of each subtitle, the video will pause automatically.
-- Could you hear what was said? Press ↑ to replay, if necessary.
-- Then press ↓ to reveal the subs, and check if you heard correctly.
-- Then press ↓ to unpause the video."
--
-- - Reading Practice:
-- "At the start of each new subtitle, the video will pause automatically.
-- Try reading the sub. Then press ↓ to unpause the video, and hear it spoken.
-- Did you read it correctly?"
--
----------------------------------
--------- Script Options ---------
srt_file_extensions = {".srt", ".en.srt", ".eng.srt"}
----------------------------------
sub_pad_start = 0.25
sub_pad_end = 0.2
----------------------------------
function srt_time_to_seconds(time)
major, minor = time:match("(%d%d:%d%d:%d%d),(%d%d%d)")
hours, mins, secs = major:match("(%d%d):(%d%d):(%d%d)")
return hours * 3600 + mins * 60 + secs + minor / 1000
end
function seconds_to_srt_time(time)
hours = math.floor(time / 3600)
mins = math.floor(time / 60) % 60
secs = math.floor(time % 60)
milliseconds = (time * 1000) % 1000
return string.format("%02d:%02d:%02d,%03d", hours, mins, secs, milliseconds)
end
function open_subtitles_file()
local video_path = mp.get_property("path")
local srt_filename = video_path:gsub('\\','/'):match("^(.+)/.+$") .. "/" .. mp.get_property("filename/no-ext")
for i, ext in ipairs(srt_file_extensions) do
local f, err = io.open(srt_filename .. ext, "r")
if f then
return f
end
end
return false
end
function read_subtitles()
local f = open_subtitles_file()
if not f then
return false
end
local data = f:read("*all")
data = string.gsub(data, "\r\n", "\n")
f:close()
subs = {}
subs_start = {}
subs_end = {}
for start_time, end_time, text in string.gmatch(data, "(%d%d:%d%d:%d%d,%d%d%d) %-%-> (%d%d:%d%d:%d%d,%d%d%d)\n(.-)\n\n") do
if not filter_subtitles(text) then
table.insert(subs, text)
table.insert(subs_start, srt_time_to_seconds(start_time))
table.insert(subs_end, srt_time_to_seconds(end_time))
end
end
return true
end
function filter_subtitles(text)
if string.match(text, "^%[(.*)%]$") then
return true
end
return false
end
function adjust_subtitles()
sub_id = 1
while sub_id < #subs do
if subs_start[sub_id + 1] - subs_end[sub_id] > sub_pad_end then
subs_end[sub_id] = subs_end[sub_id] + sub_pad_end
elseif subs_start[sub_id + 1] - subs_end[sub_id] > 0 then
subs_end[sub_id] = subs_end[sub_id] + (subs_start[sub_id + 1] - subs_end[sub_id]) / 2
end
if subs_start[sub_id + 1] - subs_end[sub_id] > sub_pad_start then
subs_start[sub_id + 1] = subs_start[sub_id + 1] - sub_pad_start
elseif subs_start[sub_id + 1] - subs_end[sub_id] > 0 then
subs_start[sub_id + 1] = subs_start[sub_id + 1] - (subs_start[sub_id + 1] - subs_end[sub_id]) / 2
end
sub_id = sub_id + 1
end
subs_start[1] = subs_start[1] - sub_pad_start
subs_end[#subs] = subs_end[#subs] + sub_pad_end
end
function update_sub_id()
local pos = mp.get_property_number("time-pos")
if pos == nil then
sub_id = nil
return
end
sub_id = 1
while sub_id < #subs and subs_end[sub_id] < pos do
sub_id = sub_id + 1
end
if subtitle_mode == "Reading Practice" and sub_id < #subs then
sub_id = sub_id + 1
end
end
function replay_sub_without_subtitles()
if sub_id ~= nil then
player_state = "replay"
mp.set_property("sub-visibility", "no")
mp.commandv("seek", subs_start[sub_id], "absolute+exact")
end
end
function on_seek()
if player_state ~= "replay" then
update_sub_id()
end
end
function on_playback_restart()
if player_state == "replay" then
mp.set_property("pause", "no")
player_state = nil
end
end
function on_up_arrow_key()
if subtitle_mode == "Listening Practice" then
replay_sub_without_subtitles()
end
end
function on_down_arrow_key()
if player_state == "pause" then
if mp.get_property("sub-visibility") == "no" then
mp.set_property("sub-visibility", "yes")
else
mp.set_property("sub-visibility", "no")
mp.set_property("pause", "no")
player_state = nil
if sub_id < #subs then
sub_id = sub_id + 1
else
sub_id = nil
end
end
end
end
function on_space_key()
if player_state == "pause" then
on_down_arrow_key()
else
mp.set_property_bool("pause", not mp.get_property_bool("pause"))
end
end
function subtitle_mode_timer()
local pos = mp.get_property_number("time-pos")
if pos ~= nil then
if sub_id ~= nil and mp.get_property("pause") ~= "yes" then
if subtitle_mode == "Listening Practice" and pos >= subs_end[sub_id] then
mp.set_property("pause", "yes")
player_state = "pause"
elseif subtitle_mode == "Reading Practice" then
if (pos + 0.25) >= subs_start[sub_id] then
mp.set_property("sub-visibility", "yes")
end
if pos >= subs_start[sub_id] then
mp.set_property("pause", "yes")
player_state = "pause"
end
end
end
end
end
function init_subtitle_mode()
default_sub_visibility = mp.get_property("sub-visibility")
mp.set_property("sub-visibility", "no")
timer = mp.add_periodic_timer(0.05, subtitle_mode_timer)
update_sub_id()
mp.add_key_binding("up", "up-arrow-key", on_up_arrow_key)
mp.add_key_binding("down", "down-arrow-key", on_down_arrow_key)
mp.add_key_binding("space", "space-arrow-key", on_space_key)
mp.register_event("seek", on_seek)
mp.register_event("playback-restart", on_playback_restart)
end
function release_subtitle_mode()
mp.set_property("sub-visibility", default_sub_visibility)
timer:kill()
mp.remove_key_binding("up-arrow-key")
mp.remove_key_binding("down-arrow-key")
mp.remove_key_binding("space-arrow-key")
mp.unregister_event(on_seek)
mp.unregister_event(on_playback_restart)
end
function toggle_subtitle_mode()
if subtitle_mode == nil then
subtitle_mode = "Listening Practice"
mp.set_property("sub-delay", sub_pad_end + 0.25)
elseif subtitle_mode == "Listening Practice" then
subtitle_mode = "Reading Practice"
mp.set_property("sub-delay", -sub_pad_start - 0.25)
else
mp.set_property("sub-delay", 0)
subtitle_mode = nil
end
if subtitle_mode ~= nil then
mp.osd_message("Subtitle Mode: " .. subtitle_mode)
init_subtitle_mode()
else
mp.osd_message("Subtitle Mode: " .. "Off")
release_subtitle_mode()
end
end
function init()
local ret = read_subtitles()
if ret == false or #subs == 0 then
return
end
adjust_subtitles()
mp.add_key_binding("i", "toggle-interactive-mode", toggle_subtitle_mode)
end
mp.register_event("file-loaded", init)