-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfind_subtitles.lua
64 lines (55 loc) · 2.39 KB
/
find_subtitles.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
require "os"
require "io"
require "string"
mputils = require "mp.utils"
-- "find_subtitles" tries to do two simple things:
-- 1. use python's subliminal for downloading subtitles
-- 2. load any subtitles found within movie folder
-- (this script binds letter "s")
-- helper function for capturing cli response
-- Reference: http://stackoverflow.com/questions/132397
function os.capture(cmd, raw)
local f = assert(io.popen(cmd, "r"))
local s = assert(f:read("*a"))
f:close()
return string.sub(s, 0, -2)
end
-- helper function for getting file (adress) info
-- (works regardless cwd path)
function fileadressinfo()
local fpath = mp.get_property("path", " ")
local dir = mputils.split_path(fpath)
if dir == "." then
dir = mp.get_property("working-directory")
end
return fpath, dir
end
function find_subtitles()
local fp, dr = fileadressinfo()
-- use subliminal to fetch english subtitles from all providers (change en to your preference if needed)
mp.msg.info("Searching for subtitles..")
local ss =
os.capture(
"subliminal download --provider opensubtitles --provider podnapisi --provider tvsubtitles -l en -v '" ..
fp .. "'"
)
-- you can also use this if you join addict7d (replace user and pass with yours; use no quotes just the strings)
-- local ss = os.capture("subliminal --addic7ed user pass download --provider addic7ed --provider opensubtitles --provider podnapisi --provider thesubdb --provider tvsubtitles -l en -v '" .. fp .. "'")
-- strip unnecessary info (as of subliminal version 1.0.1 the string matchings below are working ... dont know for how long though
ss = string.gsub(ss, "(Collecting videos)", "")
ss = string.gsub(ss, "(Downloading subtitles)", "")
ss = string.gsub(ss, "(1 video collected / 0 video ignored / 0 error)", ".")
ss = string.gsub(ss, "\n", "")
if string.match(ss, "0 video collected / 1 video ignored / 0 error") then
ss = "Subtitle already in path.. "
end
-- Possible messages to display:
-- "1 subtitle downloaded for <FILENAME>", if subliminal returned without errors
-- "Subtitle already in path..", if there are already subtitles in the folder
-- <captured errors>, if something has gone wrong
mp.osd_message(string.format(" %s", ss), 4)
-- no matter what happened, try to load *.srt files (if any)
mp.commandv("rescan_external_files", "reselect")
end
mp.add_key_binding("s", "subtitles", find_subtitles)
mp.register_event("start-file", find_subtitles)