-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdetectdualsubs.lua
169 lines (142 loc) · 5.87 KB
/
detectdualsubs.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
--[[
detectdualsubs.lua by zydezu
(https://github.com/zydezu/mpvconfig/blob/main/scripts/detectdualsubs.lua)
Detects if there are two existing subtitles, one being an original
script and the other being a translation
(eg: English and Japanese subtitles) and displays them both on screen
Add secondary-sub-pos to mpv.conf to set the screen position -
https://mpv.io/manual/master/#options-secondary-sub-pos
Add the language tags for original subs you want a translated sub
to show up for in options.original_sub, and add possible translated
subs to options.translated_sub
Here "[ja]" and "[en]" represents files such as:
"01. Beautiful World [ja].lrc" and "01. Beautiful World [en].lrc"
the script detects if a sub has "[ja]" or "[en]" in it's filename to
determine whether to show them bot has primary and secondary subtitles
options.auto_set_non_forced_subs if true, set subtitles to a
non-forced sub track as defined by the options --slang, as sometimes
videos may force unneeded tracks
--]]
mp.utils = require("mp.utils")
local options = {
original_sub = {"[ja]"},
translated_sub = {"[en]"},
auto_set_non_forced_subs = true
}
(require "mp.options").read_options(options)
local subtitle_filenames = {}
local subtitle_ids = {}
local function get_subtitle_count()
local track_list = mp.get_property_native("track-list", {})
local subtitle_count = 0
for _, track in ipairs(track_list) do
if track["type"] == "sub" then
subtitle_count = subtitle_count + 1
table.insert(subtitle_filenames, track["external-filename"])
table.insert(subtitle_ids, track["id"])
end
end
return subtitle_count
end
local function check_for_dual_subs()
local subtitle_count = get_subtitle_count()
if subtitle_count > 0 then
mp.commandv("set", "sub", "1")
if not mp.get_property("current-tracks/sub/external-filename") then
return
end
local _, filename = mp.utils.split_path(mp.get_property("current-tracks/sub/external-filename"))
local ext = filename:match("^.+(%..+)$")
if not ext then
return
end
local filename_noext = filename:gsub(ext, "")
local original = true
local tag_to_use, primary_track_id, secondary_track_id
-- check if sub is original
for i, lang in ipairs(options.original_sub) do
local pattern = lang:gsub("[%[%]]", "%%%1") -- Escape [ and ]
if string.find(filename_noext, pattern) then
tag_to_use = lang
original = true
primary_track_id = mp.get_property_number("sid")
break
end
end
-- check if sub is translated
for i, lang in ipairs(options.translated_sub) do
local pattern = lang:gsub("[%[%]]", "%%%1") -- Escape [ and ]
if string.find(filename_noext, pattern) then
tag_to_use = lang
original = false
secondary_track_id = mp.get_property_number("sid")
break
end
end
if tag_to_use then
for i, sub_filename in ipairs(subtitle_filenames) do
_, sub_filename = mp.utils.split_path(sub_filename)
local sub_ext = sub_filename:match("^.+(%..+)$")
local sub_filename_noext = sub_filename:gsub(sub_ext, "")
if ext == sub_ext then
if filename_noext ~= sub_filename_noext then
for j, lang in ipairs(original and options.translated_sub or options.original_sub) do
local pattern = lang:gsub("[%[%]]", "%%%1") -- Escape [ and ]
if string.find(sub_filename_noext, pattern) then
if original then
secondary_track_id = subtitle_ids[i]
end
primary_track_id = subtitle_ids[i]
else
break
end
end
end
else
-- not same file ext so skip file
end
end
if primary_track_id and secondary_track_id then
print("Found two subtitles - showing dual subtitles")
mp.set_property_number("sid", primary_track_id)
mp.set_property_number("secondary-sid", secondary_track_id)
return true
else
-- no dual subtitles detected
end
else
return false -- nothing detected
end
end
return false
end
local function osd_msg(message)
print(message)
mp.osd_message(message)
end
local function set_non_forced_subs()
local track_list = mp.get_property_native("track-list")
local slang_list = mp.get_property_native("slang")
for _, value in ipairs(slang_list) do
for i = 1, #track_list do
if track_list[i].type == "sub" and track_list[i].lang == value and track_list[i].forced == false then
mp.set_property("sid", track_list[i].id)
print("Setting non-forced sub "..track_list[i].id)
return
end
end
end
end
local function auto_check_for_dual_subs()
local result = check_for_dual_subs()
if not result and options.auto_set_non_forced_subs then
set_non_forced_subs()
end
end
local function key_bind_check_for_dual_subs()
osd_msg("Checking for dual subs...")
local result = check_for_dual_subs()
osd_msg(result and "Applied dual subs" or "Couldn't find dual subs")
end
mp.register_event("file-loaded", auto_check_for_dual_subs)
mp.add_key_binding("ctrl+b", "key_bind_check_for_dual_subs", key_bind_check_for_dual_subs)