Skip to content

Instantly share code, notes, and snippets.

@fullmetalsheep
Forked from selsta/autosub.lua
Last active June 26, 2023 12:52
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fullmetalsheep/28c397b200a7348027d983f31a7eddfa to your computer and use it in GitHub Desktop.
Save fullmetalsheep/28c397b200a7348027d983f31a7eddfa 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
local utils = require 'mp.utils'
--sleep function, so that subs aren't downloaded the instant a file is loaded
--taken from:http://lua-users.org/wiki/SleepFunction
function sleep(s)
local ntime = os.time() + s
repeat until os.time() > ntime
end
--original function, pressing b will trigger this and download the subtitle manually
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", "-f", "-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
--added function, auto search for subs and download if not present, the way god intended :P
function autosubs()
sleep(10)
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)
mp.register_event("file-loaded", autosubs)
@fullmetalsheep
Copy link
Author

fullmetalsheep commented Nov 24, 2016

True automatic subtitle downloading for mpv

Make sure you have subliminal installed

Usage:

  1. Download trueautosub.lua
  2. Copy to ~/.config/mpv/scripts/
  3. Open a video file using mpv, the subtitles will automatically get downloaded
  4. If you got a wrongly synced sub, press b or your customised shortcut to get another subtitle.Rinse and repeat until you find the proper sub 🎉

Note: only tested on OSX, this should technically work with linux as well

this is simply a lazy hack to make the script work the way its meant to be, all credits should go to the original author: selsta.Absolutely no programming skills were involved in modifying this script.enjoy :)

@macmeister1967
Copy link

Need to save subtitles without ".en" in filename to match perfectly with movie filename...

Do I need to do this via subliminal or can you add a patch/line in your script???

@fernandog
Copy link

fernandog commented Aug 14, 2017

Use this param:

  -s, --single                    Save subtitle without language code in the
                                  file name, i.e. use .srt extension. Do not
                                  use this unless your media player requires
                                  it.

I saw you call subliminal with "-s" but somehow is not working in your script
try calling subliminal manually with "-s" and you will see it works

@fullmetalsheep
Copy link
Author

@macmeister1967 @fernandog

try now, it should not save .en files at all. line 12 had the trick. However, as a negative consequence of this, the same .srt file will be replaced every time rather than downloading multiple files.

@pnyiaart
Copy link

pnyiaart commented Apr 20, 2018

works flawlessly with gnome-mpv too in lubuntu , just had to make a scrips folder in /home/user/.config/gnome-mpv/ folder and put the script there .

any shortcuts to delay or hasten the subtitles a bit ?

@fullmetalsheep
Copy link
Author

works flawlessly with gnome-mpv too in lubuntu , just had to make a scrips folder in /home/user/.config/gnome-mpv/ folder and put the script there .

Extremely sorry about the late reply, i don't check my gists page often. And I'm glad, I'll update the mpv script wiki to say that it does. Thanks 👍

any shortcuts to delay or hasten the subtitles a bit ?

I have added a timer function to the script and a default wait of 10 second before searching for subs. You can customize the script to wait any number of seconds you want :)

@fullmetalsheep
Copy link
Author

I have now pushed this script to a separate repo where i'll maintain it. please refer here if you have more queries, suggestions, fixes: https://github.com/fullmetalsheep/mpv-iina-scripts

thanks!

@davidde
Copy link

davidde commented May 11, 2019

I have made an improved version that does not download subs for short videos or movies that already have subs.
You might find it interesting: https://github.com/davidde/mpv-autosub

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