Skip to content

Instantly share code, notes, and snippets.

@selsta
Last active October 8, 2023 11:28
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save selsta/ce3fb37e775dbd15c698 to your computer and use it in GitHub Desktop.
Save selsta/ce3fb37e775dbd15c698 to your computer and use it in GitHub Desktop.
Automatically download subtitles in mpv using subliminal.
-- requires subliminal, version 1.0 or newer
-- default keybinding: b
-- add the following to your input.conf to change the default keybinding:
-- keyname script_binding auto_load_subs
local utils = require 'mp.utils'
function load_sub_fn()
subl = "/usr/local/bin/subliminal" -- use 'which subliminal' to find the path
mp.msg.info("Searching subtitle")
mp.osd_message("Searching subtitle")
t = {}
t.args = {subl, "download", "-s", "-l", "en", mp.get_property("path")}
res = utils.subprocess(t)
if res.status == 0 then
mp.commandv("rescan_external_files", "reselect")
mp.msg.info("Subtitle download succeeded")
mp.osd_message("Subtitle download succeeded")
else
mp.msg.warn("Subtitle download failed")
mp.osd_message("Subtitle download failed")
end
end
mp.add_key_binding("b", "auto_load_subs", load_sub_fn)
@racuna
Copy link

racuna commented Aug 11, 2016

My distro does no have subliminal on the repos, but your script can use qnapi too instead of subliminal, the only difference would be the arguments:
t.args = {subl, "-q", "-c", "-l", "en", mp.get_property("path")}

and the binary path, of course:
subl = "/usr/bin/qnapi"

But they are pretty similar. I used it and works well. Thanks.

@sunrisewestern
Copy link

It's not working for me...

@CatTail
Copy link

CatTail commented Jan 5, 2017

@sunrisewestern Change invocation to

    t.args = {subl, "download", "-l", "zh", "--", path}

@tactidact
Copy link

Works great with qnapi on Windows with @racuna's modification.
With path in line 7 as
subl = "C:\\Program Files (x86)\\QNapi\\qnapi.exe"

And line 11 as
t.args = {subl, "-q", "-c", "-l", "en", mp.get_property("path")}

@habibun
Copy link

habibun commented Mar 9, 2019

I am using Ubuntu 18.04. when i am pressing b. I am getting subtitle download failed.

@davidde
Copy link

davidde commented May 11, 2019

If you're still in need, I made an improved version of this script with clear setup instructions:
https://github.com/davidde/mpv-autosub

@nsirolli
Copy link

nsirolli commented Jan 6, 2022

Could you make it work when playing videos streamed by webtorrent? Thanks!

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