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
+ -- =============================================================================
46
31
auto = true , -- Automatically download subtitles, no hotkeys required
47
32
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
49
34
utf8 = true , -- Save all subtitle files as UTF-8
50
- }
51
- local excludes = {
35
+
36
+
52
37
-- Movies with a path containing any of these strings/paths
53
38
-- will be excluded from auto-downloading subtitles.
54
39
-- Full paths are also allowed, e.g.:
55
40
-- '/home/david/Videos',
56
- ' no-subs-dl' ,
57
- }
58
- local includes = {
41
+ excludes = ' no-subs-dl' ,
59
42
-- If anything is defined here, only the movies with a path
60
43
-- containing any of these strings/paths will auto-download subtitles.
61
44
-- Full paths are also allowed, e.g.:
62
- -- '/home/david/Videos',
45
+ includes = ' /home/david/Videos' ,
63
46
}
47
+
64
48
-- =============================================================================
49
+ local mp = require (' mp' )
65
50
local utils = require ' mp.utils'
51
+ local mpopt = require ' mp.options'
66
52
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
67
78
68
79
-- Download function: download the best subtitles in most preferred language
69
80
function download_subs (language )
@@ -72,28 +83,28 @@ function download_subs(language)
72
83
log (' No Language found\n ' )
73
84
return false
74
85
end
75
-
86
+
76
87
log (' Searching ' .. language [1 ] .. ' subtitles ...' , 30 )
77
88
78
89
-- Build the `subliminal` command, starting with the executable:
79
- local table = { args = { subliminal } }
90
+ local table = { args = { options . subliminal } }
80
91
local a = table .args
81
92
82
- for _ , login in ipairs (logins ) do
93
+ for _ , login in ipairs (options . logins ) do
83
94
a [# a + 1 ] = login [1 ]
84
95
a [# a + 1 ] = login [2 ]
85
96
a [# a + 1 ] = login [3 ]
86
97
end
87
- if bools .debug then
98
+ if options .debug then
88
99
-- To see `--debug` output start MPV from the terminal!
89
100
a [# a + 1 ] = ' --debug'
90
101
end
91
102
92
103
a [# a + 1 ] = ' download'
93
- if bools .force then
104
+ if options .force then
94
105
a [# a + 1 ] = ' -f'
95
106
end
96
- if bools .utf8 then
107
+ if options .utf8 then
97
108
a [# a + 1 ] = ' -e'
98
109
a [# a + 1 ] = ' utf-8'
99
110
end
@@ -145,7 +156,7 @@ function control_downloads()
145
156
sub_tracks [# sub_tracks + 1 ] = track
146
157
end
147
158
end
148
- if bools .debug then -- Log subtitle properties to terminal:
159
+ if options .debug then -- Log subtitle properties to terminal:
149
160
for _ , track in ipairs (sub_tracks ) do
150
161
mp .msg .warn (' Subtitle track' , track [' id' ], ' :\n {' )
151
162
for k , v in pairs (track ) do
@@ -159,7 +170,9 @@ function control_downloads()
159
170
for _ , language in ipairs (languages ) do
160
171
if should_download_subs_in (language ) then
161
172
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!
163
176
end
164
177
log (' No subtitles were found' )
165
178
end
@@ -174,7 +187,7 @@ function autosub_allowed()
174
187
return false
175
188
elseif duration < 900 then
176
189
mp .msg .warn (' Video is less than 15 minutes\n ' ..
177
- ' => NOT auto-downloading subtitles' )
190
+ ' => NOT auto-downloading subtitles' )
178
191
return false
179
192
elseif directory :find (' ^http' ) then
180
193
mp .msg .warn (' Automatic subtitle downloading is disabled for web streaming' )
@@ -183,7 +196,7 @@ function autosub_allowed()
183
196
mp .msg .warn (' Automatic subtitle downloading is disabled for cue files' )
184
197
return false
185
198
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' }
187
200
188
201
for _ , file_format in pairs (not_allowed ) do
189
202
if file_format == active_format then
@@ -193,7 +206,7 @@ function autosub_allowed()
193
206
end
194
207
195
208
for _ , exclude in pairs (excludes ) do
196
- local escaped_exclude = exclude :gsub (' %W' ,' %%%0' )
209
+ local escaped_exclude = exclude :gsub (' %W' , ' %%%0' )
197
210
local excluded = directory :find (escaped_exclude )
198
211
199
212
if excluded then
@@ -203,10 +216,11 @@ function autosub_allowed()
203
216
end
204
217
205
218
for i , include in ipairs (includes ) do
206
- local escaped_include = include :gsub (' %W' ,' %%%0' )
219
+ local escaped_include = include :gsub (' %W' , ' %%%0' )
207
220
local included = directory :find (escaped_include )
208
221
209
- if included then break
222
+ if included then
223
+ break
210
224
elseif i == # includes then
211
225
mp .msg .warn (' This path is not included for auto-downloading subs' )
212
226
return false
@@ -221,16 +235,16 @@ end
221
235
function should_download_subs_in (language )
222
236
for i , track in ipairs (sub_tracks ) do
223
237
local subtitles = track [' external' ] and
224
- ' subtitle file' or ' embedded subtitles'
238
+ ' subtitle file' or ' embedded subtitles'
225
239
226
240
if not track [' lang' ] and (track [' external' ] or not track [' title' ])
227
- and i == # sub_tracks then
241
+ and i == # sub_tracks then
228
242
local status = track [' selected' ] and ' active' or ' present'
229
243
log (' Unknown ' .. subtitles .. status )
230
244
mp .msg .warn (' => NOT downloading new subtitles' )
231
245
return false -- Don't download if 'lang' key is absent
232
246
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
234
248
if not track [' selected' ] then
235
249
mp .set_property (' sid' , track [' id' ])
236
250
log (' Enabled ' .. language [1 ] .. ' ' .. subtitles .. ' !' )
@@ -242,18 +256,17 @@ function should_download_subs_in(language)
242
256
end
243
257
end
244
258
mp .msg .warn (' No ' .. language [1 ] .. ' subtitles were detected\n ' ..
245
- ' => Proceeding to download:' )
259
+ ' => Proceeding to download:' )
246
260
return true
247
261
end
248
262
249
263
-- Log function: log to both terminal and MPV OSD (On-Screen Display)
250
264
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
252
266
mp .msg .warn (string ) -- This logs to the terminal
253
267
mp .osd_message (string , secs ) -- This logs to MPV screen
254
268
end
255
269
256
-
257
270
mp .add_key_binding (' b' , ' download_subs' , download_subs )
258
271
mp .add_key_binding (' n' , ' download_subs2' , download_subs2 )
259
272
mp .register_event (' file-loaded' , control_downloads )
0 commit comments