Skip to content

Instantly share code, notes, and snippets.

@markododa
Last active November 1, 2020 21:35
Show Gist options
  • Save markododa/6afe3c7a518654cef5fc112f5f9c5b8a to your computer and use it in GitHub Desktop.
Save markododa/6afe3c7a518654cef5fc112f5f9c5b8a to your computer and use it in GitHub Desktop.
-- requires subdl: https://github.com/akexakex/subdl
-- default keybinding: b
-- add the following to your input.conf to change the default keybinding:
-- keyname script_binding subdl_load_subs
local utils = require 'mp.utils'
local subdl = "/usr/bin/subdl" -- use 'which subdl' to find the real path
function subdl_load()
local video_name=mp.get_property("filename"):gsub(".[^.]+$", "")
sub_loaded=string.match(mp.get_property("track-list"), '"type":"sub"')
local substore = os.getenv ( "HOME" ) .. "/.local/share/mpv/"
local path_to_sub = substore .. video_name .. ".srt"
t = {}
local ssub_exists=io.open(path_to_sub,"r")
local filepath=io.open(mp.get_property("path"),"r")
if (mp.get_property("filename"):gsub("^.*[.]","") == "pls" or sub_loaded~=nil) then
mp.msg.warn("Subtitle loaded or playlist")
elseif (filepath==nil and ssub_exists==nil) then
t.args = {subdl, "--lang=eng", "--download=first", "--search", video_name, "--output", path_to_sub, "/dev/null"}
res = utils.subprocess(t)
if res.status == 0 then
mp.commandv("sub-add", path_to_sub)
else
mp.msg.warn("Subtitle download failed")
mp.osd_message("Subtitle download failed")
end
elseif (filepath==nil and ssub_exists~=nil) then
mp.commandv("sub-add", path_to_sub)
elseif (filepath~=nil and sub_loaded==nil) then
mp.osd_message("Searching subtitles...")
t.args = {subdl, "--lang=eng", "--download=first", mp.get_property("path")}
res = utils.subprocess(t)
mp.msg.warn(res.status)
if res.status == 0 then
mp.commandv("rescan_external_files", "reselect")
elseif res.status == 1 then
mp.msg.warn("Subtitle download failed")
mp.osd_message("Subtitle download failed")
end
else
print("no need for download")
end
end
mp.register_event("file-loaded", subdl_load)
-- mp.add_key_binding("b", "subdl_load_subs", subdl_load)
@markododa
Copy link
Author

autosub.lua fork that downloads subtitles for streams aside of files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment