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
|
local iso639 = require("lib.iso639")
local mutil = require("mp.utils")
local opensubtitles = require("server.opensubtitles")
local util = require("lib.util")
local default_lang = "eng"
local note = function(str)
mp.osd_message("mpvsub: " .. str)
end
local mkdir = function(path)
local info = mutil.file_info(path)
if info and not info.is_dir then
os.remove(path)
end
return util.run({ "mkdir", "-p", path })
end
local sub_needed = function()
local duration, isvideo, name
name = mp.get_property_native("path")
if
name:find("https?://www.youtube.com/")
or name:find("https?://youtu.be/")
then
return false
end
duration = mp.get_property("duration")
if not duration or tonumber(duration) < 900 then
return false
end -- ensure duration is more than 15 minutes
for _, v in pairs(mp.get_property_native("track-list")) do
if v["type"] == "sub" then
return false
end
if v["type"] == "video" then
isvideo = true
end
end
return isvideo
end
local getslang = function()
local sslang = {}
local slang = mp.get_property_native("slang")
for i = 1, #slang do
local lang = iso639.toset2(slang[i])
if lang then
table.insert(sslang, lang)
end
end
if #sslang < 1 then
slang.insert(default_lang)
end
return sslang
end
local sub_setup = function()
local out, name, path, r, filesize, slangs, i
note("fetching subtitle")
out = mp.get_property_native("sub-file-paths")[1]
if out then
out = out:gsub("^~/", os.getenv("HOME") .. "/")
else
out = os.getenv("HOME") .. "/.local/share/mpv/subs"
mp.set_property("sub-file-paths", out)
end
mkdir(out)
path = mp.get_property_native("path")
out = out .. "/" .. util.string_vid_path_to_name(path) .. ".srt"
if not util.file_exists(path) then
name = mp.get_property_native("media-title")
end
filesize = mp.get_property_native("file-size")
i = 1
slangs = getslang()
repeat
r = opensubtitles.search(path, out, {
name = name,
filesize = filesize,
iso639_2_lang = slangs[i],
})
i = i + 1
until r or i > #slangs
if r then
mp.commandv("rescan_external_files")
mp.osd_message("fetched " .. slangs[i - 1] .. " subtitles")
else
note("failed to fetch subtitles")
end
end
local file_listener = function()
if sub_needed() then
sub_setup()
end
end
mp.register_event("file-loaded", file_listener)
mp.add_key_binding("b", "mpvsub", sub_setup)
|