diff --git a/README.md b/README.md index 97f105f..67fb9db 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ Right now it only lists subtitles from OpenSubtitles, but has the ability to sea Copy subselect.lua and subselect.py into your script folder ### Configuration +Changing the configuration is optional. Options: * *down_dir*: set the download path for the subtitles * *subselect_path*: set the subselect.py path * *sub_language*: set language for subtitles [default english]; value is a 3-letter ISO-639-3 language code @@ -40,7 +41,7 @@ Copy subselect.lua and subselect.py into your script folder Per default the script tries to download the subtitles into the folder from where the video is being played. Is that not possible it downloads them into your HOME folder, or in Windows into your Downloads folder. You may have to set the subselect.py path manually if the script guesses the wrong mpv configuration directory. If the script is somehow not working as expected, it is recommended to set both *down_dir* and *subselect_path* and make sure they are absolute paths and do exist. The option to be changed can be put inside a `subselect.conf` file in the lua-settings folder. Create them if they don't exist. -Sample `subselect.conf` if you want to change both options: +Sample `subselect.conf` if you want to change all options: ``` down_dir=C:\Users\\subtitles subselect_path=C:\Users\\python_scripts\subselect.py diff --git a/subselect.lua b/subselect.lua index 40c8373..df32ea7 100644 --- a/subselect.lua +++ b/subselect.lua @@ -6,99 +6,99 @@ options.down_dir = "" options.sub_language = "eng" if package.config:sub(1,1) == "/" then - options.subselect_path = utils.join_path(os.getenv("HOME"), ".config/mpv/scripts/subselect.py") - ops = "unix" + options.subselect_path = utils.join_path(os.getenv("HOME"), ".config/mpv/scripts/subselect.py") + ops = "unix" else - options.subselect_path = utils.join_path(os.getenv("APPDATA"), "mpv\\scripts\\subselect.py") - ops = "win" + options.subselect_path = utils.join_path(os.getenv("APPDATA"), "mpv\\scripts\\subselect.py") + ops = "win" end function fixsub(path) - f = io.open(path, "r") - if f == nil then - return - end - content = f:read("*all") - f:close() - write = false - content = content:gsub("%s*%-%->%s*", - function(w) - if w:len() < 5 then - write = true - return " --> " - end - end, 1) - if not write then - return - end - f = io.open(path, "w") - f:write(content) - f:flush() - f:close() + f = io.open(path, "r") + if f == nil then + return + end + content = f:read("*all") + f:close() + write = false + content = content:gsub("%s*%-%->%s*", + function(w) + if w:len() < 5 then + write = true + return " --> " + end + end, 1) + if not write then + return + end + f = io.open(path, "w") + f:write(content) + f:flush() + f:close() end function set_down_dir(ddir) - if ddir == "" then - if mp.get_property_native("path", ""):find("://") ~= nil then - if ops == "win" then - ddir = utils.join_path(os.getenv("USERPROFILE"), "Downloads") - else - ddir = os.getenv("HOME") - end - else - ddir = utils.split_path(mp.get_property_native("path", "")) - if ops == "win" then - if ddir:find("^%a:") == nil then - ddir = utils.join_path(os.getenv("USERPROFILE"), "Downloads") - end - else - if ddir:find("^/") == nil then - ddir = os.getenv("HOME") - end - end - end - end - return ddir + if ddir == "" then + if mp.get_property_native("path", ""):find("://") ~= nil then + if ops == "win" then + ddir = utils.join_path(os.getenv("USERPROFILE"), "Downloads") + else + ddir = os.getenv("HOME") + end + else + ddir = utils.split_path(mp.get_property_native("path", "")) + if ops == "win" then + if ddir:find("^%a:") == nil then + ddir = utils.join_path(os.getenv("USERPROFILE"), "Downloads") + end + else + if ddir:find("^/") == nil then + ddir = os.getenv("HOME") + end + end + end + end + return ddir end function get_python_binary() - python = nil - python_version = utils.subprocess({ args = { "python", "--version" }}) - if python_version.status < 0 then - mp.osd_message("python not found") - else - if python_version.stdout:find("3%.") ~= nil then - python = "python" - else - python_version = utils.subprocess({ args = { "python3", "--version" }}) - if python_version.status < 0 then - mp.osd_message("python3 not installed") - else - python = "python3" - end - end - end - return python + python = nil + python_version = utils.subprocess({ args = { "python", "--version" }}) + if python_version.status < 0 then + mp.osd_message("python not found") + else + if python_version.stdout:find("3%.") ~= nil then + python = "python" + else + python_version = utils.subprocess({ args = { "python3", "--version" }}) + if python_version.status < 0 then + mp.osd_message("python3 not installed") + else + python = "python3" + end + end + end + return python end function search_subs() - read_options(options) - ddir = set_down_dir(options.down_dir) - video = mp.get_property_native("media-title", "") - python = get_python_binary() - if python ~= nil then - ret = utils.subprocess({ args = { python, options.subselect_path, video, ddir, options.sub_language }}) - else - return - end - if string.find(ret.stdout, ".") ~= nil then - mp.osd_message("loading subtitle: "..ret.stdout) - subtitle_path = utils.join_path(ddir, ret.stdout) - fixsub(subtitle_path) - mp.commandv("sub-add", subtitle_path) - else - mp.osd_message("No subtitles found") - end + read_options(options) + ddir = set_down_dir(options.down_dir) + video = mp.get_property_native("media-title", "") + python = get_python_binary() + if python ~= nil then + ret = utils.subprocess({ args = { python, options.subselect_path, video, ddir, options.sub_language }}) + else + return + end + if string.find(ret.stdout, ".") ~= nil then + mp.osd_message("loading subtitle: "..ret.stdout) + subtitle_path = utils.join_path(ddir, ret.stdout) + fixsub(subtitle_path) + mp.commandv("sub-add", subtitle_path) + else + mp.osd_message("No subtitles found") + end end mp.add_key_binding("alt+u", "subselect", search_subs) \ No newline at end of file