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-bilingual.lua
232 lines (186 loc) · 9.36 KB
/
sub-bilingual.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
-- Usage:
-- Shift + B - create bilingual subtitles (and automatically select as default subtitles with visibility set to true)
-- Note:
-- Requires the original subtitles and translated subtitles (*.srt) alongside with the video file.
-- Status:
-- Experimental & not interested.
------- Script Options -------
srt_original_file_extensions = {".srt", ".en.srt", ".eng.srt"}
srt_translated_file_extensions = {".ru.srt", ".rus.srt"}
------------------------------
function srt_time_to_seconds(time)
local major, minor = time:match("(%d%d:%d%d:%d%d),(%d%d%d)")
local hours, mins, secs = major:match("(%d%d):(%d%d):(%d%d)")
return hours * 3600 + mins * 60 + secs + minor / 1000
end
function seconds_to_ass_time(time)
local hours = math.floor(time / 3600)
local mins = math.floor(time / 60) % 60
local secs = math.floor(time % 60)
local milliseconds = (time * 1000) % 1000
return string.format("%d:%02d:%02d.%02d", hours, mins, secs, milliseconds / 10)
end
function open_subtitles_file(srt_file_exts)
local srt_base_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_base_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)
local subs = {}
local subs_start = {}
local subs_end = {}
if f then
local data = f:read("*all")
data = string.gsub(data, "\r\n", "\n")
f:close()
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
end
return subs, subs_start, subs_end
end
function convert_into_sentences(subs, subs_start, subs_end)
local sentences = {}
local sentences_start = {}
local sentences_end = {}
for i, sub_text in ipairs(subs) do
local sub_start = subs_start[i]
local sub_end = subs_end[i]
local 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
local prev_sub_start = sentences_start[#sentences]
local prev_sub_end = sentences_end[#sentences]
local prev_sub_text = sentences[#sentences]
if (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 = sentences[#sentences] .. " " .. 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
return sentences, sentences_start, sentences_end
end
function string.starts(s, substring)
return string.sub(s, 1, string.len(substring)) == substring
end
function write_bilingual_subtitles(subs_original, subs_original_start, subs_original_end, subs_translated, subs_translated_start, subs_translated_end, subtitles_filename)
if #subs_original == 0 or #subs_translated == 0 then
return false
end
local screenx, screeny, aspect = mp.get_osd_size()
mp.set_osd_ass(screenx, screeny, "{\\an9}● ")
local f = assert(io.open(subtitles_filename, "w"))
f:write("[Script Info]", "\n\n")
f:write("[V4+ Styles]", "\n")
f:write("Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding", "\n")
f:write("Style: English,Arial,20,&H00FFFFFF,&H000000FF,&H00000000,&H50000000,0,0,0,0,100,100,0,0,4,1,6.25,2,10,10,10,1", "\n")
f:write("[Events]", "\n")
f:write("Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text", "\n")
local translated_sub_lines = {}
local translates_sub_style = "\\N{\\fs2} \\N{\\fs14}{\\c&H009DDFF1&}"
for i, sub_text in ipairs(subs_original) do
local sub_start = subs_original_start[i]
local sub_end = subs_original_end[i]
sub_text = string.gsub(sub_text, "\n", " ")
local flag = false
local sub_line = ""
for j, second_sub_text in ipairs(subs_translated) do
local second_sub_start = subs_translated_start[j]
local second_sub_end = subs_translated_end[j]
second_sub_text = string.gsub(second_sub_text, "\n", " ")
second_sub_text = string.gsub(second_sub_text, "<[^>]+>", "")
if second_sub_end > sub_start and second_sub_start < sub_end then
local s_sub_start = second_sub_start > sub_start and second_sub_start or sub_start
local s_sub_end = second_sub_end > sub_end and sub_end or second_sub_end
local s_sub_length = s_sub_end - s_sub_start
if ((second_sub_start <= sub_start and second_sub_end >= sub_end) or (second_sub_start >= sub_start and second_sub_end <= sub_end)) or
(s_sub_length > 0.5) then
if flag then
sub_line = sub_line .. " "
end
sub_line = sub_line .. second_sub_text
flag = true
end
end
if second_sub_end > sub_end then
break
end
end
if flag then
table.insert(translated_sub_lines, sub_line)
else
table.insert(translated_sub_lines, "")
end
end
local sub_line = ""
local sub_line_start, sub_line_end
for i, sub_text in ipairs(subs_original) do
local sub_start = subs_original_start[i]
local sub_end = subs_original_end[i]
local sub_text = string.gsub(sub_text, "\n", " ")
if i == 1 then
sub_line = sub_text
sub_line_start = sub_start
sub_line_end = sub_end
else
if (translated_sub_lines[i] == translated_sub_lines[i-1] or string.starts(translated_sub_lines[i], translated_sub_lines[i-1])) and translated_sub_lines[i] ~= "" then
sub_line = sub_line .. " " .. sub_text
sub_line_end = sub_end
else
f:write(string.format("Dialogue: 0,%s,%s,English,,0,0,0,,%s\n", seconds_to_ass_time(sub_line_start), seconds_to_ass_time(sub_line_end), sub_line .. translates_sub_style .. translated_sub_lines[i-1]))
sub_line = sub_text
sub_line_start = sub_start
sub_line_end = sub_end
end
end
end
f:write(string.format("Dialogue: 0,%s,%s,English,,0,0,0,,%s\n", seconds_to_ass_time(sub_line_start), seconds_to_ass_time(sub_line_end), sub_line .. translates_sub_style .. translated_sub_lines[#translated_sub_lines]))
f:close()
mp.set_osd_ass(screenx, screeny, "")
return true
end
function create_bilingual_subtitles()
local subs_translated, subs_translated_start, subs_translated_end = read_subtitles(srt_translated_file_extensions)
local subs_original, subs_original_start, subs_original_end = read_subtitles(srt_original_file_extensions)
local sentences_original, sentences_original_start, sentences_original_end = convert_into_sentences(subs_original, subs_original_start, subs_original_end)
local subtitles_filename = mp.get_property("working-directory") .. "/" .. mp.get_property("filename/no-ext") .. ".ass"
local ret = write_bilingual_subtitles(sentences_original, sentences_original_start, sentences_original_end, subs_translated, subs_translated_start, subs_translated_end, subtitles_filename)
if ret then
mp.commandv("sub-add", subtitles_filename)
mp.set_property("sub-visibility", "yes")
mp.osd_message("Finished creating bilingual subtitles")
else
mp.osd_message("Failed to create bilingual subtitles")
end
end
mp.add_key_binding("B", "create-bilingual-subtitles", create_bilingual_subtitles)