Skip to content

Commit

Permalink
Improve multi-language support to include sub check
Browse files Browse the repository at this point in the history
  • Loading branch information
davidde committed Aug 23, 2019
1 parent e1a538a commit 19d1337
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 34 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
[Use `i` to modify, then `CTRL+SHIFT+V` to paste inside vi]
[Use `ESC`, then `:wq` to write the changes and exit]
```
4. Optionally change the subtitle languages / [ISO codes](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) on line 6. Be sure to put your preferred language at the top of the list.
5. Optionally specify your login for your preferred subtitle provider, if you have one.
4. Optionally change the subtitle languages /
[ISO codes](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) on line 6.
Be sure to put your preferred language at the top of the list.
5. Optionally specify the login of your preferred subtitle provider, if you have one.
6. Enjoy automatically downloaded subtitles the next time you open MPV!
(If necessary, you can manually trigger the download by pressing `b`.)
(If necessary, you can manually trigger downloading your first choice language by pressing `b`,
or your second choice language by pressing `n`.)
## Docs
If you wish to modify or adapt this script to your needs,
Expand Down
77 changes: 46 additions & 31 deletions autosub.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,46 +23,48 @@ function log(string, secs)
end

-- Download function: download the best subtitles in most preferred language
function download_subs()
function download_subs(language)
language = language or languages[1]
log('Searching ' .. language[1] .. ' subtitles ...', 30)

directory, filename = utils.split_path(mp.get_property('path'))
if login[1] then
mp.msg.warn('Using ' .. login[1] .. ' login')
table = {
args = { -- To see --debug output start mpv from terminal!
subliminal, login[1], login[2], login[3], '--debug', 'download',
'-s', '-f', '-l', 'language', '-d', directory, filename
'-s', '-f', '-l', language[2], '-d', directory, filename
}
}
lang = 10
else
mp.msg.warn('Not using any provider login')
table = {
args = { -- To see --debug output start mpv from terminal!
subliminal, '--debug', 'download', '-s', '-f',
'-l', 'language', '-d', directory, filename
'-l', language[2], '-d', directory, filename
}
}
lang = 7
end
result = utils.subprocess(table)

for _, language in pairs(languages) do
log('Searching ' .. language[1] .. ' subtitles ...', 30)
table.args[lang] = language[2]
result = utils.subprocess(table)

if string.find(result.stdout, 'Downloaded 1 subtitle') then
-- Subtitles are downloaded successfully, so rescan to activate them:
mp.commandv('rescan_external_files')
log(language[1] .. ' subtitles ready!')
return
end
mp.msg.warn('No ' .. language[1] .. ' subtitles found')
if string.find(result.stdout, 'Downloaded 1 subtitle') then
-- Subtitles are downloaded successfully, so rescan to activate them:
mp.commandv('rescan_external_files')
log(language[1] .. ' subtitles ready!')
return true
else
log('No ' .. language[1] .. ' subtitles found\n')
return false
end
log('No subtitles were found')
end

-- Manually download second language subs by pressing 'n':
function download_subs2()
download_subs(languages[2])
end

-- Control function: only download if necessary
function control_download()
function control_downloads()
duration = tonumber(mp.get_property('duration'))
if duration < 900 then
mp.msg.warn('Video is less than 15 minutes\n' ..
Expand All @@ -71,30 +73,43 @@ function control_download()
end
track_list = mp.get_property_native('track-list')
-- mp.msg.warn('track_list = ', mp.get_property('track-list'), '\n')
for _, language in pairs(languages) do
if should_download_subs_in(language, track_list) then
if download_subs(language) == true then
return
end
else
return
end
end
log('No subtitles were found')
end

-- Check for subs already present (either embedded in video or external subtitle files):
function should_download_subs_in(language, track_list)
for _, track in pairs(track_list) do
if track['type'] == 'sub' then
if track['lang'] == languages[1][3] or track['lang'] == languages[1][2]
or (track['title'] and track['title']:lower():find(languages[1][3])) then
mp.msg.warn('Embedded ' .. languages[1][1] ..
' subtitles are already present:\n' ..
'=> NOT downloading new subtitles')
if track['lang'] == language[3] or track['lang'] == language[2]
or (track['title'] and track['title']:lower():find(language[3])) then
log('Embedded ' .. language[1] .. ' subtitles are present')
mp.msg.warn('=> NOT downloading new subtitles')
if not track['selected'] then
mp.msg.warn('=> Enabling embedded ' .. languages[1][1] .. ' subtitles')
mp.msg.warn('=> Enabling embedded ' .. language[1] .. ' subtitles:')
mp.set_property('sid', track['id'])
end
return
return false
elseif track['external'] == true then
mp.msg.warn('A matching external subtitle file is present\n' ..
'=> NOT downloading other subtitles')
return
return false
end
end
end
mp.msg.warn('No ' .. languages[1][1] .. ' subtitles were detected\n' ..
mp.msg.warn('No ' .. language[1] .. ' subtitles were detected\n' ..
'=> Proceeding to download:')
download_subs()
return true
end

mp.register_event('file-loaded', control_download)
mp.register_event('file-loaded', control_downloads)
mp.add_key_binding('b', 'download_subs', download_subs)

mp.add_key_binding('n', 'download_subs2', download_subs2)

0 comments on commit 19d1337

Please sign in to comment.