Skip to content

Instantly share code, notes, and snippets.

@Realiserad
Last active August 12, 2024 20:13
Show Gist options
  • Select an option

  • Save Realiserad/74f5b172148a5e3b1e3a6b8ffdb97cd9 to your computer and use it in GitHub Desktop.

Select an option

Save Realiserad/74f5b172148a5e3b1e3a6b8ffdb97cd9 to your computer and use it in GitHub Desktop.
--[[
Download subtitles from opensubtitles.com
Based on vayan/autosub-mpv
Instructions:
1. Create an acccount on opensubtitles.com and
register an API consumer: https://www.opensubtitles.com/en/consumers
2. Install opensubtitles-com:
pip install git+ssh://[email protected]/dusking/opensubtitles-com.git
3. Create a configuration file with your username, password and API key:
ost set-cred
4. Copy and paste this file to ~/.config/mpv/scripts (create the directory
if it does not exist).
5. Open a movie in mpv and press 'a' to download subtitles.
]]
local utils = require 'mp.utils'
function file_exists(name)
local f = io.open(name, "r")
if f ~= nil then
io.close(f)
return true
end
return false
end
function sub_from_file_hash(srt_path)
local t = {
args = { "ost", "download",
"--file", mp.get_property("path"),
"--output", srt_path
}
}
utils.subprocess(t)
return file_exists(srt_path)
end
function sub_from_filename(srt_path)
local t = {
args = { "ost", "download",
"--query", mp.get_property("path"),
"--output", srt_path
}
}
utils.subprocess(t)
return file_exists(srt_path)
end
function load_sub_fn()
srt_path = string.gsub(mp.get_property("path"), "%.%w+$", ".srt")
if file_exists(srt_path) then
mp.osd_message("Subtitle already exists!", 5)
return
end
mp.osd_message("Searching subtitle...")
if sub_from_file_hash(srt_path) then
if mp.commandv("sub_add", srt_path) then
mp.msg.warn("Subtitle downloaded from file hash")
mp.osd_message("Subtitle downloaded from file hash", 5)
return
end
end
if sub_from_filename(srt_path) then
if mp.commandv("sub_add", srt_path) then
mp.msg.warn("Subtitle downloaded from filename")
mp.osd_message("Subtitle downloaded from filename", 5)
return
end
end
mp.msg.warn("Subtitle download failed")
mp.osd_message("Subtitle download failed", 5)
end
mp.add_key_binding("a", "auto_load_subs", load_sub_fn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment