aboutsummaryrefslogtreecommitdiff
path: root/main.lua
diff options
context:
space:
mode:
authorsinanmohd <sinan@sinanmohd.com>2024-03-04 14:38:48 +0530
committersinanmohd <sinan@sinanmohd.com>2024-03-04 15:45:45 +0530
commitd6540310e4cd12cba67b0c2e6c666d94dd91f367 (patch)
treefb85a78451acf228f136e3de392e616679f1efd3 /main.lua
parent8982ec02694f925ebb311b386dcd06063e5e55fe (diff)
mpv: respect slang var
Diffstat (limited to 'main.lua')
-rw-r--r--main.lua52
1 files changed, 43 insertions, 9 deletions
diff --git a/main.lua b/main.lua
index 1b1eeb5..b7c7b3c 100644
--- a/main.lua
+++ b/main.lua
@@ -1,7 +1,14 @@
local mutil = require 'mp.utils'
local util = require 'lib.util'
+local iso639 = require 'lib.iso639'
local opensubtitles = require 'server.opensubtitles'
+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
@@ -37,10 +44,29 @@ local sub_needed = function ()
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, rc, filesize
+ local out, name, path, r, filesize, slangs, i
- mp.osd_message('fetching subtitle')
+ note('fetching subtitle')
out = mp.get_property_native('sub-file-paths')[1]
if out then
@@ -59,17 +85,25 @@ local sub_setup = function ()
end
filesize = mp.get_property_native('file-size')
- rc = opensubtitles.search(path, out, {
- name = name,
- filesize = filesize
- })
+ 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 rc then
+ if r then
mp.commandv('rescan_external_files')
mp.set_property('sid', 1)
- mp.osd_message('fetch success')
+
+ mp.osd_message('fetched ' .. slangs[i - 1] .. ' subtitles')
else
- mp.osd_message('fetch failure')
+ note('failed to fetch subtitles')
end
end