-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShow-Local-Subts.lua
329 lines (224 loc) · 9.02 KB
/
Show-Local-Subts.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
------ Mpv Show Subs Local Subs v. 0.01
------ cassio018@hotmail.com
------ I just copied and pasted code and create this script :)
----- to do
------ ( ) show local and target subs in the same time.
------ ( ) show subs in a box, is possible ?
------ ( ) Anki integratioons:
-------------( ) export notes with local and target subs.
------ How to use:
------ Copy this script for Mpv Script folder
------ Set .srt subs in the local language
SRT_FILE_EXTENSIONS = {".pt-BR.srt"}
------ Press Ctrl+S
---------------------------------------------------------------
utils = require 'mp.utils'
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_time(time, delimiter)
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%s%03d", hours, mins, secs, delimiter, milliseconds)
end
function seconds_to_ffmpeg_time(time)
return seconds_to_time(time, '.')
end
function set_start_timestamp()
start_timestamp = mp.get_property_number("time-pos") + AUDIO_CLIP_PADDING
if end_timestamp == nil then
local sub_start, sub_end = get_current_sub_timing()
end_timestamp = sub_end + AUDIO_CLIP_PADDING
end
end
function set_end_timestamp()
end_timestamp = mp.get_property_number("time-pos") - AUDIO_CLIP_PADDING
if start_timestamp == nil then
local sub_start, sub_end = get_current_sub_timing()
start_timestamp = sub_start - AUDIO_CLIP_PADDING
end
end
function get_current_sub_timing()
local time_pos = mp.get_property_number("time-pos")
local sub_start, sub_end
for i, sub_text in ipairs(sentences) do
sub_start = sentences_start[i]
sub_end = sentences_end[i]
if sub_start <= time_pos and time_pos <= sub_end then
break
end
if sentences_start[i] > time_pos then
break
end
end
return sub_start, sub_end
end
function set_start_end_timestamps()
local time_pos = mp.get_property_number("time-pos")
for i, sub_text in ipairs(sentences) do
start_timestamp = sentences_start[i]
end_timestamp = sentences_end[i]
if i < #sentences and sentences_start[i+1] > time_pos then
break
end
end
mp.osd_message(string.format("%s - %s", seconds_to_ffmpeg_time(start_timestamp), seconds_to_ffmpeg_time(end_timestamp)))
end
function pause_player()
mp.set_property("pause", "yes")
end
function on_playback_restart()
if timer ~= nil then
timer:kill()
end
if player_state == "replay from the end" then
timer = mp.add_timeout(REPLAY_TIME + AUDIO_CLIP_PADDING, pause_player)
player_state = nil
elseif player_state == "replay from the start" then
timer = mp.add_timeout(end_timestamp - start_timestamp + 2 * AUDIO_CLIP_PADDING, pause_player)
player_state = nil
end
end
function open_subtitles_file(srt_file_exts)
local srt_filename = mp.get_property("working-directory") .. "/" .. mp.get_property("filename/no-ext")
for i, ext in ipairs(srt_file_exts) do
local f, err = io.open(srt_filename .. ext, "r")
if f then
return f
end
end
return false
end
function read_subtitles(srt_file_exts)
local f = open_subtitles_file(srt_file_exts)
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
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
return true
end
function convert_into_sentences()
sentences = {}
sentences_start = {}
sentences_end = {}
for i, sub_text in ipairs(subs) do
sub_start = subs_start[i]
sub_end = subs_end[i]
sub_text = string.gsub(sub_text, "<[^>]+>", "")
if sub_text:find("^- ") ~= nil and sub_text:sub(3,3) ~= sub_text:sub(3,3):upper() then
sub_text = string.gsub(sub_text, "^- ", "")
end
if #sentences > 0 then
prev_sub_start = sentences_start[#sentences]
prev_sub_end = sentences_end[#sentences]
prev_sub_text = sentences[#sentences]
if JOIN_SUBTITLES_INTO_SENTENCES and (sub_start - prev_sub_end) <= 2 and sub_text:sub(1,1) ~= '-' and
sub_text:sub(1,1) ~= '"' and sub_text:sub(1,1) ~= "'" and sub_text:sub(1,1) ~= '(' and
(prev_sub_text:sub(prev_sub_text:len()) ~= "." or prev_sub_text:sub(prev_sub_text:len()-2) == "...") and
prev_sub_text:sub(prev_sub_text:len()) ~= "?" and prev_sub_text:sub(prev_sub_text:len()) ~= "!" and
(sub_text:sub(1,1) == sub_text:sub(1,1):lower() or prev_sub_text:sub(prev_sub_text:len()) == ",") then
local text = prev_sub_text .. " " .. sub_text
text = string.gsub(text, "\n", "#")
text = string.gsub(text, "%.%.%. %.%.%.", " ")
text = string.gsub(text, "#%-", "\n-")
text = string.gsub(text, "#", " ")
if text:match("\n%-") ~= nil and text:match("^%-") == nil then
text = "- " .. text
end
sentences[#sentences] = text
sentences_end[#sentences] = sub_end
else
table.insert(sentences, sub_text)
table.insert(sentences_start, sub_start)
table.insert(sentences_end, sub_end)
end
else
table.insert(sentences, sub_text)
table.insert(sentences_start, sub_start)
table.insert(sentences_end, sub_end)
end
end
end
function get_subtitles_fragment(subtitles, subtitles_start, subtitles_end)
local time_pos = mp.get_property_number("time-pos")
local subtitles_fragment = {}
local subtitles_fragment_start_time = nil
local subtitles_fragment_end_time = nil
local subtitles_fragment_prev = " "
local subtitles_fragment_next = " "
if start_timestamp ~= nil then
start_timestamp = start_timestamp - AUDIO_CLIP_PADDING
end_timestamp = end_timestamp + AUDIO_CLIP_PADDING
end
for i, sub_text in ipairs(subtitles) do
local sub_start = subtitles_start[i]
local sub_end = subtitles_end[i]
if (start_timestamp ~= nil and (start_timestamp <= sub_start and sub_end <= end_timestamp)) or
(start_timestamp == nil and (sub_start <= time_pos and time_pos <= sub_end))
then
table.insert(subtitles_fragment, sub_text)
if subtitles_fragment_start_time == nil then
subtitles_fragment_start_time = sub_start
if i ~= 1 then
subtitles_fragment_prev = subtitles[i-1]
end
end
subtitles_fragment_end_time = sub_end
if i ~= #subtitles then
subtitles_fragment_next = subtitles[i+1]
end
end
if end_timestamp ~= nil then
if sub_start > end_timestamp then
break
end
elseif sub_start > time_pos then
break
end
end
if start_timestamp ~= nil then
start_timestamp = start_timestamp + AUDIO_CLIP_PADDING
end_timestamp = end_timestamp - AUDIO_CLIP_PADDING
end
if start_timestamp ~= nil then
subtitles_fragment_start_time = start_timestamp
subtitles_fragment_end_time = end_timestamp
end
return table.concat(subtitles_fragment, "<br />"), subtitles_fragment_start_time, subtitles_fragment_end_time, subtitles_fragment_prev, subtitles_fragment_next
end
function show_subs()
local subtitles_fragment, subtitles_fragment_start_time, subtitles_fragment_end_time, subtitles_fragment_prev, subtitles_fragment_next = get_subtitles_fragment(sentences, sentences_start, sentences_end)
if subtitles_fragment == "" then
mp.osd_message("no text")
return
end
local video_filename = mp.get_property("filename/no-ext")
local time = seconds_to_ffmpeg_time(subtitles_fragment_start_time)
local target_line = subtitles_fragment
local target_line_before = subtitles_fragment_prev
local target_line_after = subtitles_fragment_next
mp.osd_message(subtitles_fragment)
end
function init()
ret = read_subtitles(SRT_FILE_EXTENSIONS)
if ret == false or #subs == 0 then
return
end
convert_into_sentences()
mp.add_key_binding("ctrl+s", "show-subs", show_subs)
end
mp.register_event("file-loaded", init)