Skip to content

Commit 9198272

Browse files
committed
move configuration to script-opts
1 parent 3511535 commit 9198272

File tree

1 file changed

+84
-71
lines changed

1 file changed

+84
-71
lines changed

autosub.lua

+84-71
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,80 @@
1-
--=============================================================================
2-
-->> SUBLIMINAL PATH:
3-
--=============================================================================
4-
-- This script uses Subliminal to download subtitles,
5-
-- so make sure to specify your system's Subliminal location below:
6-
local subliminal = '/home/david/.local/bin/subliminal'
7-
--=============================================================================
8-
-->> SUBTITLE LANGUAGE:
9-
--=============================================================================
10-
-- Specify languages in this order:
11-
-- { 'language name', 'ISO-639-1', 'ISO-639-2' } !
12-
-- (See: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes)
13-
local languages = {
14-
-- If subtitles are found for the first language,
15-
-- other languages will NOT be downloaded,
16-
-- so put your preferred language first:
17-
{ 'English', 'en', 'eng' },
18-
{ 'Dutch', 'nl', 'dut' },
19-
-- { 'Spanish', 'es', 'spa' },
20-
-- { 'French', 'fr', 'fre' },
21-
-- { 'German', 'de', 'ger' },
22-
-- { 'Italian', 'it', 'ita' },
23-
-- { 'Portuguese', 'pt', 'por' },
24-
-- { 'Polish', 'pl', 'pol' },
25-
-- { 'Russian', 'ru', 'rus' },
26-
-- { 'Chinese', 'zh', 'chi' },
27-
-- { 'Arabic', 'ar', 'ara' },
28-
}
29-
--=============================================================================
30-
-->> PROVIDER LOGINS:
31-
--=============================================================================
32-
-- These are completely optional and not required
33-
-- for the functioning of the script!
34-
-- If you use any of these services, simply uncomment it
35-
-- and replace 'USERNAME' and 'PASSWORD' with your own:
36-
local logins = {
37-
-- { '--addic7ed', 'USERNAME', 'PASSWORD' },
38-
-- { '--legendastv', 'USERNAME', 'PASSWORD' },
39-
-- { '--opensubtitles', 'USERNAME', 'PASSWORD' },
40-
-- { '--subscenter', 'USERNAME', 'PASSWORD' },
41-
}
42-
--=============================================================================
43-
-->> ADDITIONAL OPTIONS:
44-
--=============================================================================
45-
local bools = {
1+
local options = {
2+
--=============================================================================
3+
-->> SUBLIMINAL PATH:
4+
--=============================================================================
5+
-- This script uses Subliminal to download subtitles,
6+
-- so make sure to specify your system's Subliminal location below:
7+
subliminal = '/home/david/.local/bin/subliminal',
8+
--=============================================================================
9+
-->> SUBTITLE LANGUAGE:
10+
--=============================================================================
11+
-- Specify languages in this order:
12+
-- 'language name 1,ISO-639-1,ISO-639-2;language name 2,ISO-639-1,ISO-639-2'
13+
-- (See: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes)
14+
languages = 'English,en,eng;eng;Dutch,nl,dut',
15+
--=============================================================================
16+
-->> PROVIDER LOGINS:
17+
--=============================================================================
18+
-- These are completely optional and not required
19+
-- for the functioning of the script!
20+
-- If you use any of these services, simply uncomment it
21+
-- and replace 'USERNAME' and 'PASSWORD' with your own:
22+
logins = {
23+
-- { '--addic7ed', 'USERNAME', 'PASSWORD' },
24+
-- { '--legendastv', 'USERNAME', 'PASSWORD' },
25+
-- { '--opensubtitles', 'USERNAME', 'PASSWORD' },
26+
-- { '--subscenter', 'USERNAME', 'PASSWORD' },
27+
},
28+
--=============================================================================
29+
-->> ADDITIONAL OPTIONS:
30+
--=============================================================================
4631
auto = true, -- Automatically download subtitles, no hotkeys required
4732
debug = false, -- Use `--debug` in subliminal command for debug output
48-
force = true, -- Force download; will overwrite existing subtitle files
33+
force = false, -- Force download; will overwrite existing subtitle files
4934
utf8 = true, -- Save all subtitle files as UTF-8
50-
}
51-
local excludes = {
35+
36+
5237
-- Movies with a path containing any of these strings/paths
5338
-- will be excluded from auto-downloading subtitles.
5439
-- Full paths are also allowed, e.g.:
5540
-- '/home/david/Videos',
56-
'no-subs-dl',
57-
}
58-
local includes = {
41+
excludes = 'no-subs-dl',
5942
-- If anything is defined here, only the movies with a path
6043
-- containing any of these strings/paths will auto-download subtitles.
6144
-- Full paths are also allowed, e.g.:
62-
-- '/home/david/Videos',
45+
includes = '/home/david/Videos',
6346
}
47+
6448
--=============================================================================
49+
local mp = require('mp')
6550
local utils = require 'mp.utils'
51+
local mpopt = require 'mp.options'
6652

53+
mpopt.read_options(options, "autosub")
54+
55+
local function split_with_semi(str)
56+
local fields = {}
57+
for field in str:gmatch('([^;]+)') do
58+
fields[#fields + 1] = field
59+
end
60+
return fields
61+
end
62+
63+
local function split_with_comma(str)
64+
local fields = {}
65+
for field in str:gmatch('([^,]+)') do
66+
fields[#fields + 1] = field
67+
end
68+
return fields
69+
end
70+
71+
local includes = split_with_comma(options.includes)
72+
local excludes = split_with_comma(options.excludes)
73+
local languages = split_with_semi(options.languages)
74+
75+
for i, language in ipairs(languages) do
76+
languages[i] = split_with_comma(language)
77+
end
6778

6879
-- Download function: download the best subtitles in most preferred language
6980
function download_subs(language)
@@ -72,28 +83,28 @@ function download_subs(language)
7283
log('No Language found\n')
7384
return false
7485
end
75-
86+
7687
log('Searching ' .. language[1] .. ' subtitles ...', 30)
7788

7889
-- Build the `subliminal` command, starting with the executable:
79-
local table = { args = { subliminal } }
90+
local table = { args = { options.subliminal } }
8091
local a = table.args
8192

82-
for _, login in ipairs(logins) do
93+
for _, login in ipairs(options.logins) do
8394
a[#a + 1] = login[1]
8495
a[#a + 1] = login[2]
8596
a[#a + 1] = login[3]
8697
end
87-
if bools.debug then
98+
if options.debug then
8899
-- To see `--debug` output start MPV from the terminal!
89100
a[#a + 1] = '--debug'
90101
end
91102

92103
a[#a + 1] = 'download'
93-
if bools.force then
104+
if options.force then
94105
a[#a + 1] = '-f'
95106
end
96-
if bools.utf8 then
107+
if options.utf8 then
97108
a[#a + 1] = '-e'
98109
a[#a + 1] = 'utf-8'
99110
end
@@ -145,7 +156,7 @@ function control_downloads()
145156
sub_tracks[#sub_tracks + 1] = track
146157
end
147158
end
148-
if bools.debug then -- Log subtitle properties to terminal:
159+
if options.debug then -- Log subtitle properties to terminal:
149160
for _, track in ipairs(sub_tracks) do
150161
mp.msg.warn('Subtitle track', track['id'], ':\n{')
151162
for k, v in pairs(track) do
@@ -159,7 +170,9 @@ function control_downloads()
159170
for _, language in ipairs(languages) do
160171
if should_download_subs_in(language) then
161172
if download_subs(language) then return end -- Download successful!
162-
else return end -- No need to download!
173+
else
174+
return
175+
end -- No need to download!
163176
end
164177
log('No subtitles were found')
165178
end
@@ -174,7 +187,7 @@ function autosub_allowed()
174187
return false
175188
elseif duration < 900 then
176189
mp.msg.warn('Video is less than 15 minutes\n' ..
177-
'=> NOT auto-downloading subtitles')
190+
'=> NOT auto-downloading subtitles')
178191
return false
179192
elseif directory:find('^http') then
180193
mp.msg.warn('Automatic subtitle downloading is disabled for web streaming')
@@ -183,7 +196,7 @@ function autosub_allowed()
183196
mp.msg.warn('Automatic subtitle downloading is disabled for cue files')
184197
return false
185198
else
186-
local not_allowed = {'aiff', 'ape', 'flac', 'mp3', 'ogg', 'wav', 'wv', 'tta'}
199+
local not_allowed = { 'aiff', 'ape', 'flac', 'mp3', 'ogg', 'wav', 'wv', 'tta' }
187200

188201
for _, file_format in pairs(not_allowed) do
189202
if file_format == active_format then
@@ -193,7 +206,7 @@ function autosub_allowed()
193206
end
194207

195208
for _, exclude in pairs(excludes) do
196-
local escaped_exclude = exclude:gsub('%W','%%%0')
209+
local escaped_exclude = exclude:gsub('%W', '%%%0')
197210
local excluded = directory:find(escaped_exclude)
198211

199212
if excluded then
@@ -203,10 +216,11 @@ function autosub_allowed()
203216
end
204217

205218
for i, include in ipairs(includes) do
206-
local escaped_include = include:gsub('%W','%%%0')
219+
local escaped_include = include:gsub('%W', '%%%0')
207220
local included = directory:find(escaped_include)
208221

209-
if included then break
222+
if included then
223+
break
210224
elseif i == #includes then
211225
mp.msg.warn('This path is not included for auto-downloading subs')
212226
return false
@@ -221,16 +235,16 @@ end
221235
function should_download_subs_in(language)
222236
for i, track in ipairs(sub_tracks) do
223237
local subtitles = track['external'] and
224-
'subtitle file' or 'embedded subtitles'
238+
'subtitle file' or 'embedded subtitles'
225239

226240
if not track['lang'] and (track['external'] or not track['title'])
227-
and i == #sub_tracks then
241+
and i == #sub_tracks then
228242
local status = track['selected'] and ' active' or ' present'
229243
log('Unknown ' .. subtitles .. status)
230244
mp.msg.warn('=> NOT downloading new subtitles')
231245
return false -- Don't download if 'lang' key is absent
232246
elseif track['lang'] == language[3] or track['lang'] == language[2] or
233-
(track['title'] and track['title']:lower():find(language[3])) then
247+
(track['title'] and track['title']:lower():find(language[3])) then
234248
if not track['selected'] then
235249
mp.set_property('sid', track['id'])
236250
log('Enabled ' .. language[1] .. ' ' .. subtitles .. '!')
@@ -242,18 +256,17 @@ function should_download_subs_in(language)
242256
end
243257
end
244258
mp.msg.warn('No ' .. language[1] .. ' subtitles were detected\n' ..
245-
'=> Proceeding to download:')
259+
'=> Proceeding to download:')
246260
return true
247261
end
248262

249263
-- Log function: log to both terminal and MPV OSD (On-Screen Display)
250264
function log(string, secs)
251-
secs = secs or 2.5 -- secs defaults to 2.5 when secs parameter is absent
265+
secs = secs or 2.5 -- secs defaults to 2.5 when secs parameter is absent
252266
mp.msg.warn(string) -- This logs to the terminal
253267
mp.osd_message(string, secs) -- This logs to MPV screen
254268
end
255269

256-
257270
mp.add_key_binding('b', 'download_subs', download_subs)
258271
mp.add_key_binding('n', 'download_subs2', download_subs2)
259272
mp.register_event('file-loaded', control_downloads)

0 commit comments

Comments
 (0)